1
\$\begingroup\$

Using Java/Slick 2D, I'm using the mouse to rotate a sprite on the screen and the directional keys (in this case, WASD) to move the spite. Forwards and backwards is easy, just position += cos(ang)*speed or position -= cos(ang)*speed. But how do I get the sprite to move left or right? I'm thinking it has something to do with adding 90 degrees to the angle or something. Any ideas?

Rotation code:

int mX = Mouse.getX();
        int mY = HEIGHT - Mouse.getY();
        int pX = sprite.x+sprite.image.getWidth()/2;
        int pY = sprite.y+sprite.image.getHeight()/2;
        double mAng;

        if(mX!=pX){
            mAng = Math.toDegrees(Math.atan2(mY - pY, mX - pX));
            if(mAng==0 && mX<=pX)
                mAng=180;
        }
        else{
            if(mY>pY)
                mAng=90;
            else
                mAng=270;
        }

        sprite.angle = mAng;
        sprite.image.setRotation((float) mAng);         

And the movement code (delta is change in time):

Input input = gc.getInput();

                Vector2f direction = new Vector2f();
                Vector2f velocity = new Vector2f();

                direction.x = (float) Math.cos(Math.toRadians(sprite.angle));
                direction.y = (float) Math.sin(Math.toRadians(sprite.angle));
                if(direction.length()>0)
                    direction = direction.normalise(); //On a separate note, what does this line of code do?

                velocity.x = (float) (direction.x * sprite.moveSpeed);
                velocity.y = (float) (direction.y * sprite.moveSpeed);          

                if(input.isKeyDown(sprite.up)){
                    sprite.x += velocity.x*delta;
                    sprite.y += velocity.y*delta;
                }if (input.isKeyDown(sprite.down)){
                    sprite.x -= velocity.x*delta;
                    sprite.y -= velocity.y*delta;
                }if (input.isKeyDown(sprite.left)){
                    //???
                }if (input.isKeyDown(sprite.right)){
                    //???
                }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If you want the sprite to face the right or left you can add or subtract rotation and move forward. So I'd move your input checking for left/right to somewhere before you calculate the direction:

if (input.isKeyDown(sprite.left)){
     sprite.angle -= turnSpeed;
}if (input.isKeyDown(sprite.right)){
     sprite.angle += turnSpeed;
}

Though, you'll have to change some things with your code, because this will likely get reset as soon as the mouse moves or you update from the mouse position again.

However, if you want more of a strafe action (facing forward and "side-stepping"), you'll want to find the vector that's perpendicular to your forward vector and move along that.

moveDirLeft = new Vector2f(-direction.y, direction.x)
moveDirRight = new Vector2f(direction.y, -direction.x)

Swap the x and y, then choose one to be negative depending on which direction you want to go.

This new direction should be calculated before you update the velocity. Use the new direction to update the velocity if the new direction has been updated, otherwise just use the current heading as you are now.

\$\endgroup\$
2
  • \$\begingroup\$ btw, that's something like Cross Product in 3D. \$\endgroup\$ Oct 2, 2012 at 23:57
  • \$\begingroup\$ @GustavoMaciel Correct. The cross product will result in the perpendicular vector of two vectors. \$\endgroup\$
    – House
    Oct 3, 2012 at 2:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .