Welcome on MasterOf13FPS! MasterOf13FPS

Register today or sign up if you are already a member and never miss any cool content again :)

Tutorial Strafe

Red

New member
Joined
May 16, 2022
Messages
9
Reaction score
4
Points
3
Strafe is a module that allows us to change our direction using our movement keys (WASD) without losing our movement speed.
The direction in which we move is determined by the motionX/Y/Z variables, which means we have to change these values accordingly to change our direction.

But how does it work?
Our motion values are based on our current yaw and can be calculated using trigeometric functions (sine, cosine, tangent). Here is a gif showing the player from above to illustrate how our yaw affects our keyboard input (WASD).
REm6zFS.gif

To make our movement direction solely dependent on the motion input, we only need to adjust our current yaw accordingly and recalculate our motion values. For example, if our yaw is set to 0° and we want to strafe to the left, we have to adjust our motion values as if our yaw would be -90°.

To do this we first create a method that returns the yaw we need.
Java:
public static float getPlayerDirection() {
    // start with our current yaw
    float yaw = mc.thePlayer.rotationYaw;
    return yaw;
}

To find out which movement keys I press, here I use mc.thePlayer.moveStrafing to which
1 is added when we press A(Left), or 1 is subtracted when we press D(Right).
Accordingly, I simply add or subtract 90 to our current yaw movement
Java:
public static float getPlayerDirection() {
    // start with our current yaw
    float yaw = mc.thePlayer.rotationYaw;
    if (mc.thePlayer.moveStrafing > 0) {
        // subtract 90 to strafe to the left
        yaw -= 90;
    } else if (mc.thePlayer.moveStrafing < 0) {
        // add 90 to strafe to the right
        yaw += 90;
    }
    return yaw;
}
Now we have the right yaw we need for sideways strafing

But as we can see in the GIF above, when we press W(Forward) and A/D(Left/Right) we only have to subtract 45° from our yaw. If we press W(Forward) and A/D(Left/Right), which we find out with mc.thePlayer.moveForward (this variable works similar to moveStrafing, i.e. it adds 1 when we press W(Forward) and subtracts 1 when we press S(Backward)), we only have to add/subtract 45°
Java:
public static float getPlayerDirection() {
    // start with our current yaw
    float yaw = mc.thePlayer.rotationYaw;
    if (mc.thePlayer.moveStrafing > 0) {
        // subtract 45 to strafe left forward
        yaw -= 45;
        // subtract an additional 45 if we do not press W in order to get to -90
        if (mc.thePlayer.moveForward == 0) {
            yaw -= 45;
        }
    } else if (mc.thePlayer.moveStrafing < 0) {
        // add 45 to strafe right forward
        yaw += 45;
        // add 45 if we do not press W in order to get to 90
        if (mc.thePlayer.moveForward == 0) {
            yaw += 45;
        }
    }
    return yaw;
}

This code does not consider S(Backwards) so far for that we just have to add 180° to the current yaw. However, this breaks our current strafing because if we press W(Forward) and D(Right) we get:
yaw = 0
yaw += 180 | yaw is 180
yaw += 45 | yaw is 225
with this we would strafe to the left. To fix this we have to invert our 45° when we run backwards
Java:
public static float getPlayerDirection() {
    // start with our current yaw
    float yaw = mc.thePlayer.rotationYaw;
    float strafe = 45;
    // add 180 to the yaw to strafe backwards
    if(mc.thePlayer.moveForward < 0){
        // invert our strafe to -45
        strafe = -45;
        yaw += 180;
    }
    if (mc.thePlayer.moveStrafing > 0) {
        // subtract 45 to strafe left forward
        yaw -= strafe;
        // subtract an additional 45 if we do not press W in order to get to -90
        if (mc.thePlayer.moveForward == 0) {
            yaw -= 45;
        }
    } else if (mc.thePlayer.moveStrafing < 0) {
        // add 45 to strafe right forward
        yaw += strafe;
        // add 45 if we do not press W in order to get to 90
        if (mc.thePlayer.moveForward == 0) {
            yaw += 45;
        }
    }
    return yaw;
}
This method can be greatly shortened which I will not do here now

All we have to do now for the strafe is to recalculate our motion values with the yaw returned by the method
Java:
if (isPlayerMoving()) {
    double currentPlayerSpeed = Math.sqrt(mc.thePlayer.motionX * mc.thePlayer.motionX + mc.thePlayer.motionZ * mc.thePlayer.motionZ);
    double yaw = Math.toRadians(getPlayerDirection());
    mc.thePlayer.motionZ = Math.cos(yaw) * currentPlayerSpeed;
    mc.thePlayer.motionX = -Math.sin(yaw) * currentPlayerSpeed;
}

A Like would be nice if it has helped you
Let me know if you have any suggestions for improvement

~ Red
 
helping people on a forum who doesn't even know the basics of java

:poop:

anyway good post xd
 
thanks for helping the skids make assclient b1.0!
 
shape1
shape2
shape3
shape4
shape5
shape6
Back
Top