Game Programming and Development Tools

Mathematics In Regards to Graphics – Xian_Lee

Xian_Lee

Member

Posts: 345
From:
Registered: 03-15-2006
Hey everyone,

I never have been a mathematical genius, so I was hoping that I might find a little bit of help here. Basically, there are two things I really need to figure out.

First, I need to figure out how to calculate angles (or would that be a vector?). I have two objects that have points on a two-dimensional plane. I then want a projectile to leave one object and target the present position of the other. Is this something where I can simply calculate a slope (y_initial - y_target / x_initial - x_target) and use it for my direction?

Secondly, I need to figure out how to compute angles as they relate to object rotation. If I know the current angle, how do I compute the amount of rotation I need to do in order for the projectile object to look like it is facing its present direction?

I'm sorry if I'm not describing these things well, or with the right terminology. As I said, my mathematical competency is lacking. All the same, it seems a thread for questions like these would be helpful (I just hope there wasn't one from three years ago ).

Thanks!

------------------
Portal with information on my programming projects and links to my other work

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
This might be like the function you're looking for as for calculating angles. its in BB I think, it might be in BM.

Function Ang(x1#,y1#,x2#,y2#)

a# = x1# - x2#
b# = y1# - y2#

length# = Sqr#((a#^2) + (b#^2))

c# = (a# / length#)
d# = (b# / length#)

angle# = ACos(c#)
angle# = ASin(d#)

Return angle#

End Function

Anyway, hope it helps

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

I reserve the full right to change my views/theories at any time.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
quote:


First, I need to figure out how to calculate angles (or would that be a vector?). I have two objects that have points on a two-dimensional plane. I then want a projectile to leave one object and target the present position of the other. Is this something where I can simply calculate a slope (y_initial - y_target / x_initial - x_target) and use it for my direction?

Angles on computers are generally fairly slow in comparison with using say a vector. If it were me doing this (and i was at one point), i'd suggest using some ratio magic mixed with the slope. Take the maximum distance that the projectile is able to move in one "tick" and divide it by the total distance between the two points (Use Pythagoras to get the total distance between the two points).
Now to find the rise and run per tick/move/etc. just multiply that ratio by the horizontal (x) and vertical (y) differences.
That's your new, fancy vector.
Sorry if that didn't make sense, and even sorrier if i'm wrong/mixed up

Edit: man, i need to learn how to speel (that one was done on purpose)

[This message has been edited by jestermax (edited May 25, 2007).]

Xian_Lee

Member

Posts: 345
From:
Registered: 03-15-2006
jestermax, thanks for that. I'm just trying to figure out what it would look like in my code. M^2, that was helpful as well. Unfortunately, I don't exactly know how to translate that function into speech so I can understand it. I'm a word guy, as you know, so I need to understand the ideas as well as the execution. Still, I think I can figure it out.

Thanks for the help guys.

------------------
Portal with information on my programming projects and links to my other work

Nomad
Member

Posts: 63
From:
Registered: 06-29-2004
I'll try to do this with a funky combination of words, code, incoherent grunts.

JesterMax is on the mark when he said to do things just with vectors, rather than worrying too much about calculating the slopes of lines (imagine if you want a vertical line; computers aren't too friendly with infinite slopes, when x1 - x0 = 0, right?).

Let's say you know a target position (which is what it sounds like), we'll call that point (x1, y1), and you of course know where the object is already at, (x0,y0). The vector between these is just [x1-x0, y1-y0]. In order to make the motion continuous, it is best to turn the vector into a unit vector, which JesterMax referred to with the Pythagorean theorem; just divide both components of the vector by the length,
L = sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
Now that you have the direction, you can come up with some smooth "ticking" parameter to move the object continuously along that direction, until it lands where you want it to.

This is all even easier if you open up to a little physics; instead of necessarily caring where the object will end up, you can just use a velocity vector [vx, vy], and use a small time parameter (I usually call it dt). Each frame, just add the velocity * time to the current position, so that
x += vx*dt; y += vy*dt; // (in C++ code)

Now, for rotations, not to bash anyone but notice that M^2 has unfortunately set his angle# variable redundantly (probably just highlighting that either ASin or ACos would work with the appropriate argument, "sometimes"). I'm assuming you mean to have some solid body rotating about its center of mass (rather than some funny orbiting motion about some random origin point).

It works well to think of things as picking some point, probably on your object's edge, and keep track of the vector that goes from the object's center point to that edge point (call this [rx, ry], which would equal [x_edge - x_center, y_edge - y_center]). If you know your final orientation and you want to know how to rotate into that orientation from some initial orientation, similar to Mene-Mene's code you want to use a dot-product, then an ArcCos.
dot_prod = (rx_final * rx_start) + (ry_final * ry_start);
// have the lengths of both vectors calculated; since I'm thinking you want
// rigid-body motion, we can safely say that the lengths of r_final and
// r_start should be the same, I'll call it r_length
dot_prod /= (r_length)*(r_length); // not the dot-product anymore, but
// the computer doesn't mind
angle = ArcCos(dot_prod);
This is now the angle between your initial vector and your final vector.

Now, to actually accomplish a rotation once you know the angle you want, there's a formula (feel free to ask for an explanation):
rx_final = rx_start*cos(angle) + ry_start*sin(angle);
ry_final = -1*rx_start*sin(angle) + ry_start*cos(angle);
(basically, the x coordinate just follows the parametric equation for a circle, and then the y coordinate is forced to be 90 degrees away from it).

I restricted the math here to what works in 2D, since that seemed to be what your question was concerning. This is a good start for 3D, but certainly not quite enough. A good math resource is mathworld.wolfram.com , where you can find a little bit of background about rotations (in matrix form, if you're comfortable with that, and I would highly recommend becoming comfortable with that) even in 3D.

Let me know what needs more explaining; I'm in school for physics, so the math might be a little heavier than you were hoping for. (of course, the math IS necessary for getting these programs done well!)

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
I kinda got my code from somebody else asking the same question, so I don't really know my stuff well, I'll however quote the person who gave it to me, and give you a couple links.

Link 1, RM's trouble with same equation.
RM's trouble
Link 2, My work on a game which requires equation.
My trouble/Cool game

Anyway, that's all I got for words.


------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

I reserve the full right to change my views/theories at any time.

[This message has been edited by Mene-Mene (edited May 26, 2007).]

Xian_Lee

Member

Posts: 345
From:
Registered: 03-15-2006
Nomad, that was exceptionally helpful.

My physics professor would probably beat me over the head if he knew that I had forgotten how to do this sort of thing. I'm working in Torque Game Builder right now, and it actually has a moveTo function that does what I need it to, but all of this is very helpful.

M^2, thanks for the links. I'll have to check them out as well.

------------------
Portal with information on my programming projects and links to my other work