Getting body matrices (advanced)

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Getting body matrices (advanced)

Postby aqnuep » Tue Sep 22, 2009 2:07 pm

Hi,

My rendering engine stores object matrices in buffer objects and the drawing is done in big batches.
Is there a way to get massive number of body matrices with a single API call or something like that?

The most naive solution would be the following:

Code: Select all
for each object do
{
    object_matrix <- NewtonBodyGetMatrix
    copy object_matrix to buffer_object
}


This seems simple but it's rather inefficient. Is there a better way to do the mentioned object matrix update?
I used Newton a lot in the past (years ago), but I'm not really familiar with the newest possibilities.

P.S. Is there or will be there any API documentation about Newton 2?
aqnuep
 
Posts: 97
Joined: Sat Dec 22, 2007 4:14 pm

Re: Getting body matrices (advanced)

Postby Julio Jerez » Tue Sep 22, 2009 2:42 pm

Version 2.0 have a iterators for eaver one of teh newton objects.
The one for rigid body I beleive is lik this

Code: Select all
NewtonBody* NewtonWorldGetFirstBody (const NewtonWorld* world);
NewtonBody* NewtonWorldGetNextBody (const NewtonWorld* world, const NewtonBody* curBody);


you can use after the Newton Update like this
Code: Select all
for (BewtonBody* bodyPtr = NewtonWorldGetFirstBody (world); bodyPtr; bodyPtr = NewtonWorldGetNextBody (world, bodyPtr) {
   // do what you want.

}


However ever you do not even need to do that at all.
Netwon only update the bodies that have change position since the last frame.
so if you write your callbacks right, then you do not even have to worrie about update matrixces at all.
if you matrix buffer is in Video Memory you could use a bouble buffer, for example

Code: Select all
MatrixBuffe* ptr0;
MatrixBuffe* ptr1;
MatrixBuffe* currentBuffet;

MainLoop()
{
    currentBuffer = SwapBuffet()
    LockBuffer (currentBuffet)
    NewtonUpdate ();
    UnlocBuffer();

   //do the render teh oteh Buffer (one frame behind)

}


Then you write the Matriccallback to place teh Matrix in teh CorrentBuffer, using an Index stored in teh body, or the vidual object;
I hope this is clear the clear?
done it right you can even have the Phisics running asyncronous in a separate thread that do not have anything to do with your Game loop. :mrgreen:

Yes there will be a documentaion, but we are still woking on it.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Getting body matrices (advanced)

Postby aqnuep » Wed Sep 23, 2009 4:28 am

Yes, that's exactly what I wanted.

I already use two buffers to avoid sync slowdowns.

I knew about the iterator stuff, but that's quite unsatisfactory for me too, but I didn't know / forgot that there is a callback for matrix updates.

Also I have the physics and rendering in different threads, so the solution is perfect for me because I do exactly what you mentioned:

Code: Select all
Physics thread:
    NewtonUpdate
    lock secondary_buffer_mutex
    for each body {
        copy new_matrix to secondary_buffer
    }
    unlock secondary_buffer_mutex

Rendering thread:
    lock secondary_buffer
    switch primary_buffer and secondary_buffer
    unlock secondary_buffer
    DoRendering


Just I have to think about a little bit of how to do the lockings in a way to minimize sync issues... :D
aqnuep
 
Posts: 97
Joined: Sat Dec 22, 2007 4:14 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron