PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

3D real world trajectories, balistics etc

Does anyone have any links to good articles covering movement in 3D. Up till now I have been moving objects (player's ship moving through a planetary system) under player control & this works fine but I just coded a space probe moving in an arc and though the end result looks fine it's just a 'hatchet job' of hard coded values which get the probe to move as required. As I have to program the 'AI' of alien ships moving around in space I need to learn more than I know.

thanks in advance
Well, basically you need to keep a velocity vector for each object you want to move. Each frame you apply that vector to the position of the object. Don't forget to do it time-based, or else the faster the framerate the faster your object will move.

If your object can rotate, then you will have to transform your velocity vector by the rotation.

Each frame apply some gravity to the Y velocity (again time-based), and don't allow it to drop below some minimal negative value (terminal velocity).

So if you want to lob an object, just give it a forward velocity (x=250), an upward velocity (y=250), and gravity will make it follow a realistic parabolic arc.

Now collision detection is a whole different story. I never did find anything to exactly meet my needs after a vast amount of searching. So I ended up getting down and dirty with the math myself to get the poly level collision detection. It was much more difficult to implement than what I anticipated, and of course being limited to fixed point math doesn't help.

Dan East
Conan · 12 May 2003, 16:59 UTC — thanks for the pointers
Thanks Dan, I will have a go as suggested.

On collision detection I don't have the need for polygon level in this game so I'm just testing the xyz of objects for distance ( eg: ship too close to the sun or near planet for orbiting )

(Some time in the next few years these devices will get floating point & we will be able to use big PC engines with everything built in)
angedelamort · 12 May 2003, 19:23 UTC
Dan, did you try the V-Clip algorith.

http://www.merl.com/projects/vclip/

It's a really interesting way of collision detection.
Conan · 15 May 2003, 10:13 UTC — vector search gave results
Dan, thanks to your pointers I searched in a different 'direction' using 3d vector & found a complete OpenGL worked example on NeHe.

As my wife is a maths teacher she will be able to tell me what quaternians are.

(I'm pretty sure a number of the PocketGL demos come from these OpenGL demos as the structure is so similar )
Quaternions use a 4th dimension to represent a 3D transformation (position and rotation). They are often used because Euler rotations (just rotations around the 3 axis) are prone to Gimbal Lock, where Quaternions are not.

Typically you only need to use something like Quaternions for CGI-type applications, where you have multiple joints, each affected by the transformation of the parent joint. Regular Euler math is fine for all the normal types of movement you would apply to individual objects.

I read an article some time ago stating that Quaternions are overrated and overused, and that Gimbal Lock can be avoided with regular matrices with better performance than Quaternions.

Dan East
ChezDoodles · 16 May 2003, 12:57 UTC
Quaternions are still not to be dismissed, though.

They are more compact, using just four values, where as a 3x3 matrix takes nine values to store the same information - an important factor for low-bandwidth network traffic - or if you want to store a large number of animation data on a limited device like phone.

Also, some operations (not all!) are just more easy to do with Quaternions.
Eberly sums up the performance issues surrounding matrix vs. quaternion pretty well in this paper in my opinion -
http://www.magic-software.com/Documentation/RotationIssues.pdf

The executive summary is that quaternions are good for interpolation, matrices are good for transforming vectors. You have to convert a quaternion to a matrix in order to transform a vector anyhow, so having a matrix around is going to win in that case. However, if you're in a situation where you need to concatenate multiple orientations in order to get to the matrix you need, then quaternions are going to be a big win since they typically (unless you're on a nice simd processor) take significantly fewer operations to interpolate than matrices.

As far as general physics goes, I thought this was a pretty good book to get started with - http://www.oreilly.com/catalog/physicsgame/
Conan · 17 May 2003, 07:23 UTC — thanks for the link
I will read the sample chapter on projectiles before I think about the book. I have too many books which do nothing on my shelf & they are not cheap in the UK.

Wonder what I would get for 'Super VGA Graphics' on e-bay. Tells you all about the state of the art 10 years ago.
I'd suggest quaternions if you are looking for realistic physics, and matrices if you are looking to 'fake' it. Keep in mind that most physics engines don't do real world physics, the fake it. I've written a few of different engines in everything from C++ to Prolog, and what I discovered is that anything above and beyond angular momentum gets very hairy very fast.

There are a lot of good books out there, but nothing that will help more than a college level Newtonian Physics book. I have to side with Dan here and say that I found that doing it myself, while a major pain in the ass, was no more difficult than trying to interpret what someone else did in a book.
Quaternions and Matrices are two different methods of accomplishing the same functionality - transforming vertices. Matrices have advantages because the rest of your 3D world is represented in Euler coords and rotations, which correlate directly to what a matrix is. Quaternions are more abstract. Matrices are not limited to simple transformations, although that is what they are primarily used for.

A very good example is how you would represent a line mathematically. There are many different methods. Typically for software development one would use euler coords and explicitly specify the start and endpoints of the line. However in mathematics the slope and Y intercept formula is typically used to represent a line. The latter is a bit more abstract, and typically not as useful in computer science. However both have advantages depending on how you want to interact with or manipulate that line. You can represent a line as a start point, a direction vector and a length. You get the idea. Which one is “best” depends on what you want to do with the line.

I look at quaternions and matrices in the same way.

Dan East
That's an interesting way of looking at it.

I think a lot of this depends on what you are looking at for optimizations and the number of times a 'thing' is going to pass through the engine per cycle. I suppose that in the end it is going to come down to what you need to do with the information anyway, since you are not going to want to translate from one to the other and back again every clock tick.
Here is the article I was talking about, "Do We Really Need Quaternions?"

http://my.dreamwiz.com/neojijon/text/Quaternions.htm

Oh, and in case anyone thinks this 3D math is new, cutting edge stuff:

"Believe it or not, the quaternion vs. vector debate goes back more than 100 years. Quaternions were developed by William Rowan Hamilton around 1843 and immediately took their place in mathematical history as a solution in search of a problem. In 1901, just two years before his death, a Yale professor named Josiah Willard Gibbs published a treatise on vector algebra which included a definition of the vector dot product and vector cross product. Vector algebra was put forth as a simple, robust alternative to quaternions."

Even BSP is almost half a century old.

Dan East
<a href="http://www.gamedev.net/reference/articles/article1199.asp">Same page, no Korean, working piccies, and an interesting "new" proviso at the top</a> :)
Thanks for the link. I was going through my (long, cluttered and old) list of favorites yesterday and found I had saved the url of that article.

It looks like it raised a pretty large debate over at GameDev.

Dan East
Brilliant article, thanks. I flirted briefly with quaternions for my gx library after problems with Euler angles, but ripped them out and replaced them with axis/angle representations because of the headaches (my maths is very rusty) and a drunken discussion with a friend of mine who's a maths academic.
... and I could never get them to slerp nicely (wobbly legs all over the place!)

I now feel all nice and smug :)
I wish I had stuck in at maths, anyway I'm making some headway in that I can now get my space ship to correctly rotate on the x,y,z and go off at the correct angle & direction but I really need to study more to understand all this martix stuff that's going on in PocketGL as I'm just working on trial and error.