Rotating Objects on creation

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Rotating Objects on creation

Postby lovatwest » Sun Oct 30, 2011 7:46 pm

I am attempting to figure out how to rotate an object on creation. For example, I read in a .OBJ file that has a simple polygon shape (representing a plane boundary) that is oriented with the major dimensions in the x-z plane. (thickness is very small and in the "y" direction). To build a more complete shape, I want to use the same object, but orient it 90 degrees so that the plane is now in the x-y plane. I have tried every combination of "m_matrix" to no avail. I have attempted to set the m_curRotation to use a Quaterinian, but not much luck there either.
Ultimately, I want to be able to glue these planes to the outside of a cylinder, but they need to be rotated so that the plane is tangential to the surface. If I can figure out how to rotate it 90 degrees, any other rotation should be easy. Any advice would be appreciated.
I can likely rotate the object in blender and dump a new file name, but I would rather used the same object. If I need to re-scale the object, I would only need to do it once. Thanks.
lovatwest
 
Posts: 20
Joined: Tue Jul 19, 2011 9:06 pm
Location: Canada

Re: Rotating Objects on creation

Postby JernejL » Sun Oct 30, 2011 9:35 pm

You can set its body matrix so it fits the way you want it in newton.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Rotating Objects on creation

Postby lovatwest » Mon Oct 31, 2011 11:00 am

Ok, so I won't claim to be clear on the concept just yet. I am using the tutorials for 2.33 as a base using the entity class. So the following chunk of code might help to point out where I am going wrong.

Code: Select all
void MakeTestPipe( NewtonWorld* world, SceneManager* sceneManager, dVector pipe_location, dFloat Roll )
{
  NewtonCollision* collision;

  Entity* pipe_b;      //  pipe boundary planes
  Entity* pipe_t;
  Entity* pipe_l;
  Entity* pipe_r;

  NewtonBody* the_pipe_b;
  NewtonBody* the_pipe_t;
  NewtonBody* the_pipe_l;
  NewtonBody* the_pipe_r;

  dFloat radRoll = ( (3.1415926535f * Roll)/ 180.0f );
  dMatrix mRoll( dRollMatrix( radRoll ) );

  //
  // Set up an offset to define where to put the pipe walls.
  // add some margin to allow easy movement.  (tolerance)
  dFloat xy_offset = 0.5f*( cyl_size.m_x + wall_size.m_x + 0.05f );

  pipe_b = sceneManager->CreateEntity();
  pipe_b->LoadMesh( "Pipewall.dat" );
  pipe_b->m_curPosition = pipe_location;
  pipe_b->m_matrix = mRoll;
   
  pipe_b->m_prevPosition = pipe_b->m_curPosition;
  pipe_b->m_prevRotation = pipe_b->m_curRotation;

  collision = CreateNewtonBox( world, pipe_b, 0 );
  the_pipe_b = CreateRigidBody( world, pipe_b, collision, 1.0f );
  NewtonBodySetMaterialGroupID( the_pipe_b, g_metalMaterial );
//  Transpose the OpenGl matrix to Newton
//  mRoll.Transpose();
//  NewtonBodySetMatrix( the_pipe_b, &mRoll[0][0] );
  NewtonBodyGetMatrix( the_pipe_b, &mRoll[0][0] );
  mRoll = dRollMatrix( radRoll );
  mRoll.Transpose();
  NewtonReleaseCollision( world, collision );


What this code is attempting to do is to read in the object, (in this case a .DAT format), for an object that is sized as 1.0x, 0.05y, 10.0z. Once the object is read in and configured, then the idea is to rotate (re-orient) the object, (in this case Roll), around the z-axis. Adjusting the "m_matrix" of the entity does not do it, nor does GET/SET of the NewtonBody matrix, or seemingly, any combination of the two.

Hopefully the error is obvious, and not too embarassing.
lovatwest
 
Posts: 20
Joined: Tue Jul 19, 2011 9:06 pm
Location: Canada

Re: Rotating Objects on creation

Postby Julio Jerez » Mon Oct 31, 2011 11:51 am

the problem is that if you are using the entity class and seting values directly.
the entity class was disigned for multireaded system with smooth interporation , you can not simple change the values, you need to use the interface function for that
this is set matrix funtion of the entity class.
Code: Select all
void DemoEntity::SetMatrix(DemoEntityManager& world, const dQuaternion& rotation, const dVector& position)
{
   // read the data in a critical section to prevent race condition from oteh thread 
   world.Lock(m_lock);

   m_curRotation = m_nextRotation;
   m_nextRotation = rotation;

   m_curPosition = m_nextPosition;
   m_nextPosition = position;

   dFloat angle = m_curRotation.DotProduct(m_nextRotation);
   if (angle < 0.0f) {
      m_curRotation.Scale(-1.0f);
   }

   // release the critical section
   world.Unlock(m_lock);
}


as you can see it uses a critical section and is changes teh target position but not the memory, therefore it will get to teh traget at then end of a time step.
if you want and instant change, then you need to erase the memory you can do that by calling SetMatrix twice with the same value, like this:

Code: Select all
// set target location and copy the memory
SetMatrix (world, rotation, position);
// now copy the rotation and position to teh memory and set teh targe position again, objet will telport to the target imediatly
SetMatrix (world, rotation, position);


I beleive this is also valid for the tutorial entity, with the exception that is not mutitreaded.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Rotating Objects on creation

Postby lovatwest » Tue Nov 01, 2011 9:33 pm

Yes, you are correct that the definition of the class should have methods to change the variables. The Entity class of the version of tutorials that I am using did not have a pre-built method and I have not yet gotten to implementing one. I will redo parts of the class in the next few weeks.
My problem, as it turns out, was that I was not setting the "m_posit" of the NewtonBody matrix correctly, and once I sorted that out, it appears to behave as I would wish. Lesson learned for later.
I will have another look at the Demo Entity and multi-thread aspects, it is not something I have considered yet.
Thanks for the assistance and input, I now have a basic rotary magazine for cylinders. Now to load and unload it.
lovatwest
 
Posts: 20
Joined: Tue Jul 19, 2011 9:06 pm
Location: Canada


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron