Archived

This topic is now archived and is closed to further replies.

Vectors And Angles And Frustration, Oh My!

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

Hey all, I''m working on a small particle engine, a hacked apart version of NeHe''s OpenGL Particle Engine. I figured this would be a good way to get me rolling into some 3D Math, which I haven''t relly done before. ANYWAYS... I''ve got me a CParticle class, and in that I have the following 3 vectors: m_Origin, m_Velocity, m_Angles So I have my origin set to the middle of the screen, and I can apply some velocities and watch the particles go flying across the screen. Now the hard part, how do I make it so that my particles go flying across according to their Angles? I don''t even know how to represent m_Angles, is it like.. from -360 to +360 or am I way off the mark here? (the vectors are 3D Vectors, from a custom CVector3 class I''ve written). So uhh.. can anyone give me a nice healthy shove into the right direction?

Share this post


Link to post
Share on other sites
if velocity is a vector, what do you need angles for? *confused* it already contains the direction where your particles are supposed to fly as well as the speed.

Share this post


Link to post
Share on other sites
if i remember NeHe''s tut correctly, m_Angles is the rotation of the particle sprite itself ...

it is a 3d vector, containing 3 rotations, in the x,y,z axes respectively.

Share this post


Link to post
Share on other sites
Alright, I suppose since the sprite is always going to be facing the screen, it doesn''t really need an angles.

So lets redo this...

I have:
m_Origin as a 3D point, m_Direction as a 3D Vector and m_iVelocity as a constant unit per frame to travel by.

How exactly do I represent m_Direction so that it in can move in any direction of 3D Space?

I really just don''t think -360 to +360 is correct.

And I''m pretty sure when I want to move the particle, I do something like:
m_Origin += m_Direction * m_iVelocity

(Ignoring Timed Movement for the time being)

But maybe thats all wrong and I really just don''t have a freaking clue =P

Please help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should *really* read a bunch of documents about maths and then 3D maths.

angles are expressed in radians. Radian is an angle unit defined by (bla bla, read doc) from 0 to 2pi.

For your problem, I guess (i dont know this tutorial), you don''t know anything about vectors and nobody can help you here.

Share this post


Link to post
Share on other sites
I understand Radians, and I''ve taken Advanced Algebra in high school. I know that 2pi Radians is 360 degrees, that 1pi Radians is 180, that 1/2pi Radians is 90 degrees.

What I''m struggling on, is how to actually represent that in a variable and be able to move an object by a constant velocity, in the direction that the vector points too.

And no, I''m not a fantastic math student. I struggle a lot in math, but I understand some basic concepts behind it. I''m just struggling here on how to actually represent it and do it from a programmer''s pov.

Share this post


Link to post
Share on other sites
Okay, so if I understand you correctly you have a vector in the direction you want to move and you have a speed (scalar). If the vector is a unit vector then you just have to multiply each element of the vector by the speed. If it is not a unit vector, then you must normalize it first, which involves dividing each element of the vector by it''s magnitude. The magnitude is given by the square root of the sum of the squares of the elements (sqrt(i^2 + j^2 + k^2...)). Once it has been normalized you have a unit vector in the appropriate direction and you can then multiply it by the speed.

Once you have multiplied the directional vector by the speed you get the velocity vector. It is important to note that velocity and speed are vastly different things, as speed is a scalar while velocity has a direction (which is why it''s expressed as a vector).

Share this post


Link to post
Share on other sites
Ah yes, I remember that speed versus velocity section in Algebra now =P

Alright, so my question more or less falls back down to how can I store these values for the direction?

Like I know that a vector of (3, 4, 5) will travel 3 units on the X, 4 on the Y and 5 on the Z. But.. how can I make that more "logical" to store for an object in a game?

Like say I have 1000 particles that I want to shove out in a perfect circular explosion, what exactly would be the key to making something like that happen using a direction vector stored as a solid number? Is it even possible? Once again, am I storing the direction vector properly or am I just missing something that should be painfully obvious?

I just have problems thinking in 3D terms I think. I mean, if I had a vector of (9, 6) as a 2D vector, I know that forms a triangle of some sort, so you grab the hypotenuse and theres you direction vector, which you can then multiply by the speed. But even then, I really wouldn't know what to store that as either... Like, if I want something to move 30o NE, how would I get a 2D vector out of that?

Once again, I'm not a fantastic math student.

[edited by - Estauns on November 27, 2003 10:53:47 PM]

Share this post


Link to post
Share on other sites
This helped me out...

Sin and Cos - The Programmer''s Friend

...but I''m only interested in 2 dimensions.

I generally have a scalar speed and scalar angle (heading). Then using my vector class I make a Cartesian vector from a Polar vector.

Vector3 velocity = Vector3:olar(heading, speed);
position += velocity;

Simple trig to convert polar vector to cartesian vector (it''s in the article).

If you are really interested in 3 dimensions then you probably need some kind of matrix transformations.

Cheers,
Paul Cunningham

Share this post


Link to post
Share on other sites
why would you want to work with angles at all? for the initial direction? just create 1 angle 0-360 (around global y) and one from -90 to 90 (around local x). might not result in an even distribution though. once you have your direction and initial speed i wouldnt keep them seperate. that way adding wind or gravity wont require multiplying and normalizing all the time (instead the question is if you can live with linear updates that work fine for high speeds or want a correct calculation).

Share this post


Link to post
Share on other sites
I used to do as you''re doing, have an angular vector with a scalar. After seeing how many times I converted back to cartesian coords I realized that just storing a vector of the speeds along each axis is far more preferable. Stick with the way it was done in your original example: Cartesian Coords for your origin, cartesian coords for your velocity, and either radians or degrees for angles. Just make certain that when you send your angles to the renderer or into a sin/cos/tan function that the angles are in radians (if you store in degrees--as I do, from 0-360--then multiply by pi/180).

Share this post


Link to post
Share on other sites