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! ------------------ |
|
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.
Anyway, hope it helps ------------------ 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: 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). 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. ------------------ |
|
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, 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 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. Now, to actually accomplish a rotation once you know the angle you want, there's a formula (feel free to ask for an explanation): 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. Anyway, that's all I got for words.
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. ------------------ |