strange behaviour of my cuboids

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: strange behaviour of my cuboids

Postby Yustme » Sun Aug 17, 2008 9:52 am

Hi,

I show the debug lines like this:

Code: Select all
DebugRenderDebugInfo();
         DebugRenderWorldCollision(newtonPtr->getWorld());

         for(int i = 0; i < NewtonWorldGetBodyCount(newtonPtr->getWorld()); i++)
         {
            NewtonBody* body0 = NewtonWorldGetFirstBody(newtonPtr->getWorld());
            NewtonCollision* collision = NewtonBodyGetCollision(body0);
            dMatrix matrix;
            NewtonBodyGetMatrix(body0, &matrix[0][0]);
            DebugDrawCollision(collision, matrix, dVector (1.0f, 1.0f, 1.0f, 1.0f));

            DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_front, dVector (1, 0, 0));
            DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_up,     dVector (0, 1, 0));
            DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_right, dVector (0, 0, 1));
         }



If thats not the way of doing it for all the bodies, please show me how its done! Because as far as i know, i don't apply any transformations to the lines.

@Julio,

I changed the transform matrix as you suggested, i'll also change the convexhull for normal primitives, like the cube and sphere.

About the mesh that might be parented, the meshes are created like this:

Code: Select all
IAnimatedMesh* mesh = irrPtr->getSceneManager()->getMesh(filename);
ISceneNode* node = irrPtr->getSceneManager()->addAnimatedMeshSceneNode(mesh);
node->setMaterialFlag(EMF_LIGHTING, false);

IMeshManipulator* mm = irrPtr->getSceneManager()->getMeshManipulator();
mm->scaleMesh(mesh, vector3df(20.0, 20.0, 20.0));


The mesh and the node are at all times at the same position (as far as i can see in the demo).

This seriously needs to be fixed.

Any idea's where i need to look into, for example, the matrixes or something?



[EDIT]

It looks like there is not collision between the bodies when i use the normal primitives for collision. However, the primitives do collide against the floor collision which mismatches the position of the graphics object of the floor.

[/EDIT]
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: strange behaviour of my cuboids

Postby Julio Jerez » Sun Aug 17, 2008 12:10 pm

I do not know where you get the ideas to write your function, some simple do not make sense to me
The debug display should looks like this:

Code: Select all
void DebugRenderDebugInfo()
{
   for (NewtonBody* body0 = NewtonWorldGetFirstBody(newtonPtr->getWorld()); body; body0 = NewtonWorldGetNextBody(newtonPtr->getWorld(), body0)
   {
      dMatrix matrix;
      NewtonBodyGetMatrix(body0, &matrix[0][0]);

      NewtonCollision* collision = NewtonBodyGetCollision(body0);
      DebugDrawCollision(collision, matrix);
   }
}


I do not now what the other things means.
I do not get why thsi si so compleicate to you.
That mean I am failling big time.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: strange behaviour of my cuboids

Postby JernejL » Sun Aug 17, 2008 12:17 pm

when do you call DebugRenderDebugInfo? there is obviously some transformation done before you call it, you need to call it without any transformations before it (modelview matrix should be a identity matrix).

what graphics library does irrlicht use?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: strange behaviour of my cuboids

Postby Yustme » Sun Aug 17, 2008 12:46 pm

Hi,

@Julio,

I went through your entire demo looking for debug display functions on how to use them. I copied it from there :P

@Delfi,

Irrlicht uses directx and opengl. You can choose between them on creating the device. I am using the Opengl one.



[EDIT]

Is this the transformation you are talking about Delfi:

Code: Select all
// Set the position of the body
matrix4 mat;
// this makes the body of the object start from the pos of the camera
mat.setTranslation(irrPtr->getCamera()->getPosition());
NewtonBodySetMatrix(body, mat.pointer());
            
// this makes the node start where the body starts too (synchronized)
newtonPtr->PhysicsSetTransform(body, mat.pointer(), 0);

// direction in which the object will be shot to
vector3df direction = (irrPtr->getCamera()->getTarget() - irrPtr->getCamera()->getPosition()).normalize() * 1800;
            
vector3df nVeloc(direction);
NewtonBodySetVelocity(body, &nVeloc.X);


Because this happens "randomly" when i press a corresponding button that launches either a cylinder or a sphere.

[/EDIT]
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: strange behaviour of my cuboids

Postby JernejL » Sun Aug 17, 2008 2:01 pm

No, i mean when you render the debug display..
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: strange behaviour of my cuboids

Postby Yustme » Sun Aug 17, 2008 2:09 pm

It's between the begin and endScene functions of irrlicht:

Code: Select all
irrPtr->getDriver()->beginScene(true, true, video::SColor(255,0,0,255));
         DebugRenderDebugInfo();
         DebugRenderWorldCollision(newtonPtr->getWorld());

         for(NewtonBody* body0 = NewtonWorldGetFirstBody(newtonPtr->getWorld()); body0; body0 = NewtonWorldGetNextBody(newtonPtr->getWorld(), body0))
        {
            dMatrix matrix;
            NewtonBodyGetMatrix(body0, &matrix[0][0]);

            NewtonCollision* collision = NewtonBodyGetCollision(body0);
            DebugDrawCollision(collision, matrix);
        }

         irrPtr->getSceneManager()->drawAll();

         irrPtr->getDriver()->endScene();
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: strange behaviour of my cuboids

Postby Andy Price » Wed Aug 20, 2008 1:53 pm

Hello YustMe,

Hopefully I can help here, I think you're having a problem with incorrectly calculated transformation matrices - it appears to be associative/commutative multiplication holding you back, (assuming I'm right).

A simple way to test for it is - do the wireframe debug shapes rotate? If they don't then I'm right, if they do then ignore my post - I may be wrong but it's totally impossible to tell from a screenshot.

The solution is normally to multiply the matrices correctly, Translation * Rotation OR Rotation * Translation - depending on what you want. Some people want this functionality for example: orbiting bodies and attaching things together, you often see it in matrix stacks.

Another suggestion would be to check the rotations are being applied correctly in an orthogonal matrix and if not, you need to adjust the internal configuration of the 3x3 rotation matrix that's inside your 4x4 standards, it's the top left corner for both OpenGL and DirectX BUT, be aware that Directx is row major and OpenGL is column major.

Just as a last reference:

Rotation(Roll (z), Pitch(x), then Yaw(y)) * Translation

is most likely what you want and is logical if you think about it. You basically want; rotate object then move it, the opposite would be to move it and then rotate - around the 0,0,0 position so we'd end up orbiting. Our coordinate system is cartesian with the center at 0,0,0 - translation does not change the center of the world, only the center of anything it is applied to (the mesh geometry usually).

You can get a lot of examples of transformation and rotation in Euclidean space, cartesian coordinates by googling for those terms.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: strange behaviour of my cuboids

Postby Yustme » Thu Aug 21, 2008 12:56 pm

Hi Andy Price,

The wireframes did rotate whenever I created an object by pressing a button, they also kind of rotate and moved whenever i moved my mouse.

It seemed that i had to do some 'reset' to the camera's view (or something). That fixed the problem with the debug display lines. It looks like the debug lines stay at place now and move with the graphics object.

However, I am still not very happy about how the objects are behaving. Because it really doesn't look realistic.

I made the modification you suggested about first rotating the object then repositioning it. That didn't solve the (not sure how to call it) strange behavior.
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: strange behaviour of my cuboids

Postby Andy Price » Thu Aug 21, 2008 1:30 pm

Hi,
sorry to hear it didn't fix it. Could you clarify what you mean when you say objects rotate when you "moved my mouse" - is that meant to happen?, (ie do you have some torque applied from mouse movement?) I can't see why something like moving the mouse would randomly cause objects to rotate.

Could you perhaps tell us what exactly you did to 'reset' the camera?

You'll have to forgive me that I haven't read every post on this thread but - you say objects behave 'unrealistically', the initial problem was that they flew out of control, is this still happenning or is there some other behaviour you wanted to change?

Just to clarify, by rotate I meant around their own local center so, say you created one at the far corner of your world, it could still spin freely about it's central axis. The suggestion about rotate then translate was to do with how you 'should' multiply matrices but I see you're using the Irrlicht engine so I would image something like that would include working methods for matrix operations and I wouldn't expect it to be the problem I suggested.

I now see after checking back that you have
mesh->setPosition(mat.getTranslation());
mesh->setRotation(mat.getRotationDegrees());


That's the kind of thing I meant to check - I would think it'll work as expected but it was just something I thought could be checked easily. (You might also check that 'setRotation' is expecting rotation in degrees.)

I also notice you're doing things like:
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_front, dVector (1, 0, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_up, dVector (0, 1, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_right, dVector (0, 0, 1));

In my implementation 'front' would be (0,0,1) and 'right' would be (1,0,0) byt again, this might be correct for you, it was just something to check.

Like I said last time, I might be on the wrong lines but if you can explain what behaviour you're currently having trouble with I'll try to help.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: strange behaviour of my cuboids

Postby Yustme » Thu Aug 21, 2008 2:06 pm

Andy Price wrote:Hi,
sorry to hear it didn't fix it. Could you clarify what you mean when you say objects rotate when you "moved my mouse" - is that meant to happen?, (ie do you have some torque applied from mouse movement?) I can't see why something like moving the mouse would randomly cause objects to rotate.

Could you perhaps tell us what exactly you did to 'reset' the camera?

You'll have to forgive me that I haven't read every post on this thread but - you say objects behave 'unrealistically', the initial problem was that they flew out of control, is this still happenning or is there some other behaviour you wanted to change?

Just to clarify, by rotate I meant around their own local center so, say you created one at the far corner of your world, it could still spin freely about it's central axis. The suggestion about rotate then translate was to do with how you 'should' multiply matrices but I see you're using the Irrlicht engine so I would image something like that would include working methods for matrix operations and I wouldn't expect it to be the problem I suggested.

I now see after checking back that you have
mesh->setPosition(mat.getTranslation());
mesh->setRotation(mat.getRotationDegrees());


That's the kind of thing I meant to check - I would think it'll work as expected but it was just something I thought could be checked easily. (You might also check that 'setRotation' is expecting rotation in degrees.)

I also notice you're doing things like:
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_front, dVector (1, 0, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_up, dVector (0, 1, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_right, dVector (0, 0, 1));

In my implementation 'front' would be (0,0,1) and 'right' would be (1,0,0) byt again, this might be correct for you, it was just something to check.

Like I said last time, I might be on the wrong lines but if you can explain what behaviour you're currently having trouble with I'll try to help.


Hi,

About your first three lines, i fixed that problem. Moving my mouse is actually moving the camera. The debug display is a class that shows the collision objects with lines. Its a useful class to check weather the physics and graphics object match each other in position and rotation.

The problem i was having with the debug lines was that they totally looked messed up (see screenshots page 1/2).

I fixed this problem by adding this line in the beginSceneI() and endScene() functions of irrlicht:

Code: Select all
driver->setTransform(ETS_WORLD, matrix4());


This it resets the world transformation needed for drawing lines, etc.

Ok that being said, the way the graphics object are positioned and rotated is changed from:

Code: Select all
mesh->setPosition(mat.getTranslation());     
mesh->setRotation(mat.getRotationDegrees());


To:

Code: Select all
vector3df rotation;
NewtonGetEulerAngle(mat.pointer(), &rotation.X);
rotation = rotation * (180.0f/3.1416f);

node->setRotation(rotation);
node->setPosition(mat.getTranslation());


And these lines:

Code: Select all
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_front, dVector (1, 0, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_up,     dVector (0, 1, 0));
DebugDrawLine (matrix.m_posit, matrix.m_posit + matrix.m_right, dVector (0, 0, 1));



No longer play any 'role' in this matter, they have been replaced with what Julio suggested.

The only problem i am having is how the cube/cuboid is 'acting'. As far as I can see, this only happens on those 2 objects. Its hard to see if the sphere is 'acting' in the same way.

Ok the behavior of the cube/cuboid is as follow:

First, if the cuboid gets launched into the sky, it comes down towards the floor with a force. Then it hits the floor, makes a (approx.) 45 degrees rotation in one direction, then rotates back to the original position (original pos is the pos it had when it landed on the floor the first time) and go's up into the sky again. Then lands again and everything is ok again.

This behavior is depending on how high the cube/cuboid go's into the sky. The higher the worse the behavior is when it comes down.

That is one of the behavior the cube/cuboid is showing. There are several which i don't know how to describe.

My guess is this may be caused by updating the objects positions and rotations. Because if i turn on the vsync param of the device which i create in irrlicht, i don't see this kind of behavior.

But this reduces the fps drastically. From 500 to 85. The vsync makes the application synchronize with the hertz of my monitor which is 85 hertz.
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: strange behaviour of my cuboids

Postby Andy Price » Fri Aug 22, 2008 8:13 am

That's certainly very strange behaviour. I know what vsync is and you should really have it on - or have a mechanism to copy that functionality. You seem very upset to be dropping from 500 to 85 frames per second but the truth is that 500 is a waste of 415 frames per second and just wastes the programs time rendering frames that won't be visible. A good solution is to render whatever is needed per second, (in your case 85) and then use the remaining time for physics or AI, game world updates etc.

Newton clamps the timestep it receives from something like 60 to 120 frames per second (I think) so, calling newton with a 500fps timestep is probably way beyond it's tolerance (may be wrong here). The usual timestep for 60 updates per second is about (1/60)=0.016 whereas your timestep at 500fps is (1/500)=0.002. This might not be the entire cause of the problem but it certainly won't be helping. Just a note; don't code (1/85) into your program as different monitors support different refresh rates - you should query the device to find the current presentation interval - (monitor refresh rate in hz) and then use that. DirectX and OpenGL too I'd assume should both give you this information if you query the parameters used to create the current rendering device. It's always a good idea to decouple game logic from rendering times as the game logic usually requires a larger time share.

You said the problem goes away when you synchronise the game fps to the monitors refresh rate so, I'd suggest that you are perhaps updating the game logic (including Newton) at the same rate as your rendering - which you have at 500fps which is way too fast. I can't see why this would make objects; hit the floor, rotate slowly, rotate back, fly into the air. It's very odd but, as lowering the game speed solves it it does seem a prime candidate.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: strange behaviour of my cuboids

Postby Yustme » Sat Aug 23, 2008 6:05 am

Hi Andy,

Everything is much faster with the vsync off. Thats why i was kind of upset about it. Now its moving very slow.

But o well, i found the cause of the problem.

Thanks for your time and help Andy!
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 19 guests

cron