Public Group

# 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.

## 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 on other sites
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 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 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 on other sites

And then:

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

void Accelerate(ship, accel)
{
}

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.

Vec would be a

struct Vec
{
float x;
float y;
float z;
};

?

##### Share on other sites
Yes, or even just:

struct Vec
{
float x[3];
};

depending on how you like to work.

##### 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

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 13
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011515
• ### Who's Online (See full list)

There are no registered users currently online

×