Advertisement Jump to content
Sign in to follow this  
Carbon101

Moving in the direction of rotation?

This topic is 1851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

 

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

Edited by ryutenchi

Share this post


Link to post
Share on other sites

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)));

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!