Jump to content
  • Advertisement
Sign in to follow this  
walle

How to make a triangle move in the direction it's facing [open gl]

This topic is 4867 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

Yes, I'm really new to Open gl, I've been working with SDL before this so I'm not new to game programming... Well here comes my question: How do I move a triangle in the direction it is facing...say the top is facing 0° (the right) and I want to apply acceleration to this triangle (thous moving it to the right)... Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You need to get information on the triangle (or have it already). What you need to know is the triangle's normal. There are simple algorithms out there to calculate the normal from the three verts it has (google, I forget where to get them. Rather, I know where, but now you have to pay). Once you know this, you simply need to add xyz values proporional to the ones that make up the normal to the triangle's current xyz values.

Example. If the normal is [1,1,.5] and you want to increase the x position by 5, but want to go in the direction of the normal, you would take your current position, and add [5,5,2.5] to it (nice and proportional to the normal).

Share this post


Link to post
Share on other sites
Triangle is at X,Y.
Traingle is rotated to face an angle A counter clockwise from the X axis.
Triangle wishes to move at speed S

Required velocity, Vx and Vy, is:

Vx = S * cos(A);
Vy = S * sin(A);

Then the new position is:

X = X + Vx * time
Y = Y + Vy * time

However, you are likely to find angles are a pain in the neck... using matrices and vectors is a better bet in the long term.

Share this post


Link to post
Share on other sites
Well I've heard that matrices and vectors require alot of math...and I haven't read that much math. Angles I know how they work...would apprisiate some info on matrices and vectors... thank you for the answer

Share this post


Link to post
Share on other sites
read this: http://web.ukonline.co.uk/andrew.newman2/stuff/vectors.html

And then:

struct ship
{
Vec position;
Vec velocity;
float angle;
};

void Accelerate(ship, accel)
{
Vec heading(cos(ship.angle), sin(ship.angle));
ship.velocity += heading * thrust;
}

void Update()
{
ship.position += ship.velocity;
}


Using matrices would be nice, since with matrices you can get rid of the cos and sin, but it is not neccesary. It also isn't complicated, however.

Share this post


Link to post
Share on other sites
Tried to implement both angle and vector based movment...didn't get it to work proporly...

there isn't someone who have an example program in opengl that shows this...it's much easier to read source for me for some reason...would appriciate if someone could show me this...anything from a app with a triangle moving to a astriods clone?

Thanks

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!