Moving in the direction of rotation?

Started by
23 comments, last by Carbon101 10 years, 4 months ago

I am working on a 2D project that requires rotation and moving in the direction of rotation. I have managed to get the image to rotate but I am not able to move the image in the direction of rotation. I have had this problem for a couple of days now. Here is a snippet of the source code:

Object Info


static float shipx =  300f;

static float shipy =  300f;

static int shipspeed = 20;

Update Method


if(input.isKeyDown(Input.KEY_UP))

            {

                
                     // What I am using to move in the direction of rotation, but I'm not having any luck.
                     shipx +=  (float) (shipspeed * Math.toRadians(Math.cos(angle)));    

                     shipy +=  (float) (shipspeed * Math.toRadians(Math.sin(angle)));

                  

                     System.out.println("X: " + shipx + " " + "Y: " + shipy);

       

            }

            else if(input.isKeyDown(Input.KEY_DOWN))

            {

                shipy += shipspeed;

            }

            else if(input.isKeyDown(Input.KEY_RIGHT))

            {

                angle += rotation;

                Ship.setCenterOfRotation(Ship.getWidth() / 2, Ship.getHeight() / 2);

                Ship.setRotation((float) angle);

            }

            else if(input.isKeyDown(Input.KEY_LEFT))

            {

                angle -= rotation;

                Ship.setCenterOfRotation(Ship.getWidth() / 2, Ship.getHeight() / 2);

                Ship.setRotation((float) angle);

            }

           

I am not using any vector math or anything, just simple trig. Feel free to ask for more information if needed. Thanks.

Advertisement

Math.toRadians(Math.cos(angle)))

It looks like toRadians() should be inside cos(), not outside. The same for sinus.

shipx += (float) (shipspeed * Math.toRadians(Math.cos(angle)));

shipy
+= (float) (shipspeed * Math.toRadians(Math.sin(angle)));

should probably be:


 shipx +=  (float) (shipspeed * Math.cos(Math.toRadians(angle)));    

 shipy +=  (float) (shipspeed * Math.sin(Math.toRadians(angle)));

EDIT: lol great minds. just a moment to late to be first with it =P

Hello guys, thanks for the quick responses I really appreciate it. I implemented what you guys suggested and I am still not getting the desired result. Here is extra information.

double rotation = 1.570796;
static double angle = Math.toRadians(Math.atan(shipx / shipy));

shipx +=  (float) (shipspeed * Math.cos(Math.toRadians(angle)));    
shipy +=  (float) (shipspeed * Math.sin(Math.toRadians(angle)));

I am not using any vector math or anything, just simple trig. Feel free to ask for more information if needed. Thanks.

Atan2 is not simple trig. Then you take the angle returned from atan2 and use it to call sin, cos.

This is one of my pet peeves.

USE VECTOR MATHS.

You turn a direction into an angle, then turn your angle into a direction. Is there any need for that? No USE VECTORS.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

What Paradigm Shifter said. Also, if you ever use angles, stick to radians. You seem to make lots of mistakes in converting between radians and degrees (the one pointed out by the first two responses, plus another one in converting the result of Math.atan to radians, when I am sure it was in radians to begin with). There is just no need to do any of those conversions.

If you don't know how to do something with vectors, ask.

Alright so I am trying to utilize vectors math more in my projects. This is what I have so far...

Variables Data:

    // Ship;
        static float shipx =  300f;
        static float shipy =  300f;
        static float shipspeedx = 1f;
        static float shipspeedy = 1f;
        Vector2f Current = new Vector2f(shipx, shipy);
        Vector2f Velocity = new Vector2f(shipspeedx, shipspeedy);

Update Method:

if(input.isKeyDown(Input.KEY_UP))
            { 
                     Current.add(Velocity);                
            }

Problem is though the ship is moving diagonally. My plan is to make my resultant vector vertical so the ship moves up, but I think I was once told that resultant vectors can't be vertical.

Vectors can be vertical you only get problems with tangents as they approach infinity, which vectors avoid. Vector away, hip hip array.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Yeah, you want some sort of vector that represents direction, preferably normalized, then you can just do things like

if(input.isKeyDown(Input.KEY_UP))
{
this.velocity += this.direction * this.thrustAmt;
}


Though technically it would probably work better if you were adjusting the acceleration, and you probably want a deltaTime in there, and a cap to the velocity.

@ferrous, thanks for the reply.

I am confused at what the

this
keyword does in Java. From my understanding its similar to C++ reference operator (&) ? Is this correct?

This topic is closed to new replies.

Advertisement