Extracting a rotation from 3D points

Blitz3D Forums/Blitz3D Programming/Extracting a rotation from 3D points

John J.(Posted 2006) [#1]
I'm working on a verlet physics engine. A rigid body is represented by a list of verlette particles (points in 3D space), which are physically simulated realistically. Now, to draw a cube, for example, would require the position and rotation to be extracted from these points (so PositionEntity and RotateEntity can be called appropriately).

Getting the position is easy - simply calculate the average (center) position of all the points. But I can't think of a way to extract the rotation from the points. How can this be done?


Stevie G(Posted 2006) [#2]
I use align to vector on the x and z axis. A quick code snippet.

b\Mesh is your visible box, p[0] is your center vertet and p[5] .. p[8] are the top verlets starting from front left moving clockwise to rear left.

PositionEntity b\Mesh , EntityX( b\p[0]\pivot ) , EntityY( b\p[0]\pivot ) , EntityZ( b\p[0]\pivot )
x# = EntityX( b\p[6]\pivot ) - EntityX( b\p[5]\pivot ) + EntityX( b\p[7]\pivot ) - EntityX( b\p[8]\pivot )
y# = EntityY( b\p[6]\pivot ) - EntityY( b\p[5]\pivot ) + EntityY( b\p[7]\pivot ) - EntityY( b\p[8]\pivot )
z# = EntityZ( b\p[6]\pivot ) - EntityZ( b\p[5]\pivot ) + EntityZ( b\p[7]\pivot ) - EntityZ( b\p[8]\pivot )
AlignToVector b\Mesh, x,y,z,1 , 1
x# = EntityX( b\p[6]\pivot ) - EntityX( b\p[7]\pivot ) + EntityX( b\p[5]\pivot ) - EntityX( b\p[8]\pivot )
y# = EntityY( b\p[6]\pivot ) - EntityY( b\p[7]\pivot ) + EntityY( b\p[5]\pivot ) - EntityY( b\p[8]\pivot )
z# = EntityZ( b\p[6]\pivot ) - EntityZ( b\p[7]\pivot ) + EntityZ( b\p[5]\pivot ) - EntityZ( b\p[8]\pivot )
AlignToVector b\Mesh, x,y,z, 3 , 1


Stevie


John J.(Posted 2006) [#3]
Thanks! Now all I need to do is figure out how to make it work with any number of vertices (my engine will hopefully support more than square-shaped objects), and how AlignToVector works (I'm making the engine as a plug-in DLL with C++) :(.


Stevie G(Posted 2006) [#4]
No probs, I find it much easier to determine body / body collisions when using a cube .. it's also stable and easy to detect when it's "broken" and needs some correction.

Mind you .. you'll be able to use more iterations of the relaxation phase if your going for raw c++ speed. Are you planning on simulating vehicles too?

Good luck with it.

[EDIT] Oh, and for multiple pointmasses .... all you need to do is store a pointer to 1 constraint which is used to align on the x-axis and another to align on the z-axis. You know the vector direction of this constraint due to the position of the pointmasses connecting it. Hope that makes sense.

Stevie


John J.(Posted 2006) [#5]
Are you planning on simulating vehicles too?

I'm making the engine mainly for the next version of my tank game, so it will at least support tank physics. I'm also going to try to make simulate buildings and other objects being blown up/smashed through :)

Oh, and for multiple pointmasses .... all you need to do is store a pointer to 1 constraint which is used to align on the x-axis and another to align on the z-axis. You know the vector direction of this constraint due to the position of the pointmasses connecting it. Hope that makes sense.

That makes sense. But how would I connect these ghost "pointmasses" to the actual object? Would a constraint need to be added connecting each "pointmass" in the object to the ghost "pointmasses". Hopefully only a few constraints would need to be added, or else things may get slow.

P.S. I discovered something very interesting about using square root estimation to improve speed - for some reason using C++ sqrt() is actually faster to a noticeable degree than using estimation! I can't understand it - the estimation I used took only about 3 multiplications, 1 division, and 2 additions, and yet calculating the exact square root is faster??


Stevie G(Posted 2006) [#6]
Tanks are actually pretty easy ... I did something a while back where I could use the left / right analogues to control the power to each track. Basically it could turn on the spot by locking them in opposite directions ... worked well :) You will need to use a few tricks with very bumpy terrain though.

You don't need to add any new constraints/pointmasses ... just store references to ones already there. Using a box, for example, for the roll-axis alignment you will already create a constraint which connects the front lower left to the front lower right verlet. For each rigid body, simply store this same constraint in Roll-Axis_Align.constraint.

Then get the vector by subtracting the x,y,z positions of the two verlets which are connected by the constraint and aligntovector using this.

Make sense?

Stevie


John J.(Posted 2006) [#7]
Yeah that makes sense - I like that idea. But what I mean is, what if there are no existing constraints along the x and z axis?


Stevie G(Posted 2006) [#8]
There will be other ways .. what type of objects are you intending to make ... other than buildings and tanks which should work fine using this method?

Stevie


John J.(Posted 2006) [#9]
I hope to simulate broken segments of building walls when you blow up or smash through houses.


Stevie G(Posted 2006) [#10]
Now that's gonna be tough ... I've tried to simulate a destructable brick wall using verlet. Getting blocks to rest on each other is very tough. I gave up in the end. I'd imagine arbitrary blocks will be a nightmare to simulate.

You'd probably have to construct each piece in such a way that they have additional constraints which can be used to align the axis'.

Personally, if you're going down this route, I think you should consider using one of the 3rd party physics libs which have these features inbuilt and may save you a few headaches in the long run.

Sorry I can't be of more help.
Stevie


John J.(Posted 2006) [#11]
I plan to disable physics on building fragments (so they'll remain stationary) until an active physics object comes in contact with it. Otherwise, I'm sure it would be a nightmare, as you said.

I may try a 3rd part physics lib as a last resort, although I have some ideas which may get my current system to work. Thanks for all your help :)


Stevie G(Posted 2006) [#12]
No problem John ... I'm happy to share my experiences of using verlet. Just give me a shout if you think I can help.

Stevie