About Store Forum Documentation Contact



Post Reply 
matrix rotation & physics
Author Message
yvanvds Offline
Member

Post: #1
matrix rotation & physics
Hey. I'm trying to rotate an object like this:

PHP Code:
Bool object::blueprint::update() { // derived from game::static
    
super::update();
    
Matrix m T.matrix();
    
m.move(-T.matrix().pos);
    
m.rotateX(2*(Time.ad()));
    
m.rotateY(Time.ad());
    
m.rotateZ(Time.ad()*2);
    
m.move(T.matrix().pos);
    
T.matrix(m);
    return 
true;


The rotation looks ok, but if I draw the physics they get way off. The physics object here is just a box. At first the rotation starts to go wrong, and a bit later the object starts to grow.

I've recreated this with tutorial 19 from game basics (included as attachment). Apart from rotating the barrels i have made these changes to the code:

- added physics.draw
- removed D.sync(true) to make the error more visible

The physics object of the barrel consists of 3 boxes, whereas my object only had one. All boxes are affected in a different way here. Very strange.

So either I'm not supposed to rotate an object this way, or there is something wrong with physics. What will it be??? smile

regards,

yvan


Attached File(s)
.txt  19 - Small Overlays.txt (Size: 6.63 KB / Downloads: 8)
04-26-2012 07:35 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: matrix rotation & physics
Hi,

This is caused probably because of numerical precision issues.

You may want to normalize the matrix before applying it - T.matrix(m.normalize());
or instead use normalization plus vector realignment (better) :
Code:
OrientP orn(m.pos, m.z, m.y); // convert matrix to positioned orientation
orn.fixPerp(); // normalize and realign
T.matrix(Matrix(orn)); // set matrix from positioned orientation

for next SDK I'll add OrientP constructor from Matrix so you can do OrientP orn=m; instead of OrientP orn(m.pos, m.z, m.y);
04-30-2012 05:39 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #3
RE: matrix rotation & physics
I had a similar problem too. The scale of the physics actors would randomly grow, especially when rotating the actor. It seems that just setting the actor's matrix directly like this was causing the problem.

Code:
actor.matrix(m);

I found that if I set the actor's position first, then its orientation like this, the problem went away.

Code:
actor.matrix(m.pos);
actor.orn(m.orn());
04-30-2012 07:20 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #4
RE: matrix rotation & physics
Thanks for the suggestions, but they don't solve the problem. I tried all of them, but still got this scaling error.

But yes, it's almost certain a numerical precision issue. If I just rotate by 0.1 or something like that, all is ok. When Time.d() is involved it, rotation and scaling go wrong.

Still, it would be nice if fps could be taken into account for a rotation.

fyi, i tried this:

PHP Code:
OrientP orn(matrix().posmatrix().zmatrix().y);
orn.rotateCross(Time.ad());
orn.rotateDir(Time.ad() * 0.8);
orn.rotatePerp(Time.ad() * 1.4);
orn.fixPerp();
T.matrix(Matrix(orn)); 

And also:
PHP Code:
Matrix m T.matrix();
m.move(-T.matrix().pos);
m.rotateX(2*(Time.rd()));
m.rotateY(Time.rd());
m.rotateZ(Time.rd()*1.5);
m.move(T.matrix().pos);
T.matrix(m.normalize()); 

Also tried fatcoder's suggestion but altering the actor's matrix instead of the object matrix gives me some other issues, like no rotating mesh.
(This post was last modified: 04-30-2012 04:19 PM by yvanvds.)
04-30-2012 04:16 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #5
RE: matrix rotation & physics
sorry I forgot 'fixPerp' does not do 'direction' normalization, can you call 'fix' instead of 'fixPerp'? (fix does dir normalization and vector realignment)
05-01-2012 05:13 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #6
RE: matrix rotation & physics
Still the same issue. As for now, it's not important to me anymore. I got around it by overwriting the actor matrix at every update, so that only the mesh rotates and the actor stays where it was at load time. I only use the physics object in this case to select the object, so it was not that important to rotate it with the mesh.

Still, i think this is a recent error. The code was there for more than 4 months, and I never had any problem selecting that kind of object. Only the last few weeks I noticed that I could not always select it, and then I found out that the physics actor was "acting up".

So while we can close this topic, you still _might_ have a small irregularity there.
05-01-2012 11:42 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #7
RE: matrix rotation & physics
codes for Actor::matrix(C Matrix &matrix) remain the same since very very long time.
Are you sure the issue is not caused by some of your other codes? (like you're setting actor matrix somewhere else)

if not then maybe the issue is inside physx.

this is completely valid code:
Code:
OrientP orn(matrix().pos, matrix().z, matrix().y);
orn.rotateCross(Time.ad());
orn.rotateDir(Time.ad() * 0.8);
orn.rotatePerp(Time.ad() * 1.4);
orn.fix();
T.matrix(Matrix(orn));
as 'fix' guarantees normalized scale, and correct perpendicular vectors.

I've tested "01 - Physics.cpp" tutorial, with following additions:

InitPre:
Code:
D.sync(false);

Update:
Code:
box.gravity(false);
OrientP orn(box.matrix().pos, box.matrix().z, box.matrix().y);
orn.rotateCross(Time.ad());
orn.rotateDir(Time.ad() * 0.8);
orn.rotatePerp(Time.ad() * 1.4);
orn.fix();
box.matrix(Matrix(orn));

Draw:
Code:
D.text(0,0,S+box.matrix().maxScale());
and it works as expected
05-01-2012 11:58 AM
Find all posts by this user Quote this message in a reply
Post Reply