Basic introduction

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Basic introduction

Postby lperkins2 » Thu Jul 09, 2015 2:48 am

The wiki is very out of date, and I'm having a bit of a hard time figuring out exactly how to do what I want from just the header file, so I'm hoping to get a little direction here.

Setting up the world, ticking it, and cleaning up is all pretty obvious. Detecting collisions, and applying external force is also pretty easy. What isn't so clear is how to go about loading the bodies in the first place. NewtonCreateKinematicBody and NewtonCreateDynamicBody both take a NewtonCollision object as an argument. Simple NewtonCollisions (box, sphere, et cetera) are also pretty easy, although I don't know what shapeID is appropriate. But how do I load some arbitrary shape? Say I have a model made in blender, what's the easiest way to get it turned into a collision and body for actually using it?

Also, I assume NewtonIslands are to somehow optimize the collision detection system, but I don't know when it is appropriate to use them, and how they differ from NewtonCollisionAggregates.

If I'm missing an updated wiki someplace, a pointer to it would be a great start, otherwise some basic help on figuring this out would be much appreciated.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Basic introduction

Postby d.l.i.w » Thu Jul 09, 2015 6:11 am

The wiki is stuck at Newton 2.0 but the basics didn't change that much.
In Newton.cpp you can find comments with a description for almost every function.

But how do I load some arbitrary shape? Say I have a model made in blender, what's the easiest way to get it turned into a collision and body for actually using it?

Take a look at NewtonTreeCollision*

In many cases the demos in applications/demosSandbox/sdkDemos/demos/ are a good reference, too.
d.l.i.w
 
Posts: 81
Joined: Mon Sep 26, 2011 4:35 am

Re: Basic introduction

Postby lperkins2 » Thu Jul 09, 2015 12:14 pm

Thanks, that's exactly what I needed. I'm not sure why I didn't think to look in the .cpp file, probably because there are some comments in the header, so I assumed it was as well, or better, commented than the source. I'm sure I'll have more questions, but this gets me started.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Basic introduction

Postby lperkins2 » Sat Jul 11, 2015 6:10 pm

Okay, so I have what I thought should be a minimal example, but I'm not getting any collisions. I'm using the python wrapper, but I think it should be obvious what API calls are being made. If someone can tell me what I'm doing wrong, I'd appreciate it.

Code: Select all
import sys
import pyNewt

@pyNewt.pyNewt.ffi.callback('void(*)(NewtonBody *, float *, int)')
def transformCallback(b, f, I):
    print 12,
    print b,
    print [f[i] for i in range(16)]

@pyNewt.pyNewt.ffi.callback('int (NewtonMaterial *, NewtonBody *, NewtonBody *, int)')
def UserOnAABBOverlap(*a):
    print 'in overlap'
    return 1


@pyNewt.pyNewt.ffi.callback('void(*)(NewtonJoint *, float, int)')
def UserContactFriction (contactJoint, timestep, threadIndex):
    print 'colliding'




def __main__(argv=sys.argv):
    world=pyNewt.NewtonWorld()
   
    defaultMaterialID = pyNewt.pyNewt.NewtonMaterialGetDefaultGroupID (world._wrapped)
    pyNewt.pyNewt.NewtonMaterialSetCollisionCallback (world._wrapped, defaultMaterialID, defaultMaterialID, pyNewt.pyNewt.ffi.NULL, UserOnAABBOverlap, UserContactFriction);
   
    collision=pyNewt.NewtonCollision.createBox(world,10,1,10,1,pyNewt.pyNewt.ffi.NULL)
    collision2=pyNewt.NewtonCollision.createBox(world,1,1,1,2,pyNewt.pyNewt.ffi.NULL)
   
    box1=pyNewt.NewtonBody.createDynamicBody(world, collision, [0]*16)
    box1.setTransformCallback(transformCallback)
    box1.setMassMatrix(1000,1000,1000,1000)
    box1.setMatrix([0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0, 1.0])
   
    box2=pyNewt.NewtonBody.createDynamicBody(world, collision2, [0]*16)
    box2.setTransformCallback(transformCallback)
    box2.setMassMatrix(1,1,1,1)
    box2.setVelocity([0,3,0])
   
   
    for i in range(20):
        world.update(1)
   

if __name__=='__main__':
    __main__()



I've also tried it with the identity matrix in createDynamicBody, but I get exactly the same results. 4 ticks in, it starts firing the overlap callback, which stops firing around tick 16. The collision callback never fires, and box1 never wakes up.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Basic introduction

Postby lperkins2 » Sun Jul 12, 2015 5:45 am

Looks like I figured this one out, since I last used newton, bodies no longer collide by default, and NewtonBodySetCollidable is a new function. We should see about starting some sort of changelog to make this sort of thing easier.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Basic introduction

Postby lperkins2 » Sun Jul 12, 2015 3:45 pm

Hm, still having a problem, after the collision, the last four entries of the matrix are nan, nan, nan, and 0. So something's wrong with the collision, and I'm not sure what...
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Basic introduction

Postby Julio Jerez » Sun Jul 12, 2015 5:10 pm

try sync to latest 3.14 I just check in, some fixed plus the fix made by D.I.I.W
I test on Linux and Max and it works now.

also the matrix is wrong
Code: Select all
box1.setMatrix([0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0, 1.0])


is would be
Code: Select all
box1.setMatrix([1.0, 0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0, 0.0,
                    0.0, 0.0, 1.0, 0.0,
                    0.0, 1.0, 0.0, 1.0])


and do no use the function,
box1.setMassMatrix(1000,1000,1000,1000)

use this thi one which will calculate correct inertia values
void NewtonBodySetMassProperties (const NewtonBody* const body, dFloat mass, const NewtonCollision* const collision);
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Basic introduction

Postby lperkins2 » Mon Jul 13, 2015 1:02 am

I'd already applied the keyboard fix manually, but the sync worked fine, pyNewt appears to work with 314.

Thanks for the help fixing the matrix, I understand the basics of how they work, but I haven't found a good python library for working with them in row-major order, and obviously, doing it by hand is prone to stupid errors. Newton uses dMatrix, but lacks a C header for it, and CFFI can't parse c++ headers yet.

My idea with setting the mass matrix was to simply make box1 hard to move, to decrease the objects I need to watch, but thanks for pointing out setMassProperties; I think it's new since I wrote my initial test program several years ago.

Anyway, I'm working on setting up a simple scenario similar to the variable friction demo, so I'll probably be back with more questions. Thanks a bunch for the help so far.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron