Most basic example

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Most basic example

Postby lperkins2 » Wed Jan 20, 2016 7:41 pm

So, I just rebuilt newton from the latest github version, good news is that the python wrapper has no issues parsing the updated header file. Bad news is I can't get even a basic test case to work.

Code: Select all
from pyNewt import NewtonWorld, NewtonCollision, NewtonBody, pyNewt

identity = [
    1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,0,1
]

w = NewtonWorld()
c = NewtonCollision.createSphere(w,1,1,pyNewt.ffi.NULL)
b = NewtonBody.createDynamicBody(w,c,identity)
mass=pyNewt.ffi.new('float[1]')
xx=pyNewt.ffi.new('float[1]')
yy=pyNewt.ffi.new('float[1]')
zz=pyNewt.ffi.new('float[1]')

b.setMassProperties(1, c)

b.getMassMatrix(mass,xx,yy,zz)
print 19, mass[0],xx[0],yy[0],zz[0]

b.setVelocity([0,1,0])
for i in range(6):
    f=pyNewt.ffi.new('float[3]')
    w.update(1/60.)
    b.getPosition(f)
    print list(f)
    import time
    time.sleep(1/60.)



I've also tried adding a transform callback, it never fires.
By default DynamicBodies have some linear damping, so the velocity should change, it doesn't. The test as displayed shows that the position of the body doesn't change. I must be missing something. The logic for this code was extracted from the core300 CNewton Tutorial 101 file (with an initial velocity instead of an initial height of 50, and no 'ground' for it to bounce off). Since that tutorial is only buildable on windows, I can't verify that the tutorial works. I'd appreciate any pointers on what to try next.

Edit: I've also tried Kinematic body, and box collisions, nothing makes a difference.
Last edited by lperkins2 on Thu Jan 21, 2016 4:00 am, edited 2 times in total.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby Sweenie » Thu Jan 21, 2016 3:16 am

Can't remember what the default mass for a body is if not set but if it has a mass of 0 it will be considered a static body.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Most basic example

Postby lperkins2 » Thu Jan 21, 2016 3:31 am

Defaults to 0, I just added

Code: Select all
b.setMassProperties(1, c)


right after creating the body, still not moving.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby lperkins2 » Thu Jan 21, 2016 3:59 am

Okay, got it working. It was as you said, due to having 0 mass. My initial test with mass failed because I was trying a Kinematic body, switching back to Dynamic and specifying a mass of 1 gets it moving. Thanks for the help.

On a related note, what's the purpose of a KinematicBody?
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby Sweenie » Thu Jan 21, 2016 8:02 am

A Kinematic Body can be used for different things.

Creating it like this...
Code: Select all
body = CreateKinematicBody(...);

This will create a body that won't be affected by other bodies or forces. Other bodies will not collide with it either but it will generate a collision callback if any body moves through it so it can be used as trigger zone to detect other bodies.

Creating it like this...
Code: Select all
body = CreateKinematicBody(...);
NewtonBodySetCollidable(body,1);

This will make other bodies bounce off it as it was a Dynamic Body with mass 0(static body).
But the difference is that you can move/animate this body yourself.
It's useful for Character controllers, elevators and moving platforms etc.
Note though, you will have to calculate the velocities yourself and set these to get correct collision response.
A neat trick is to set a velocity of the body but not moving it. Bodies falling on it will pick up it's velocity though so it's behaves kinda like a conveyor belt. :)
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Most basic example

Postby lperkins2 » Thu Jan 21, 2016 4:25 pm

Ah, but it didn't move when I specified a mass and velocity for the Kinematic body... I doubt I'll have any use for them, so I'm not going to worry about it. I managed to get a second test script that collides 2 cubes working, I'll add it to the repository later. Now I just have to integrate pyNewt into my rendering system so I can see what's actually happening. Thanks for the help!
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby Sweenie » Thu Jan 21, 2016 5:22 pm

No, setting the velocity won't move it. You have to move it by setting it's position manually. Then calculate the velocity based on the moved distance and elapsed time.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Most basic example

Postby lperkins2 » Thu Jan 21, 2016 9:32 pm

I suppose that makes sense, although I'm not certain how that would let it act as a conveyor belt, since it basically teleports a little each tick... Elevator I can see, since it would overlap the stuff on the floor, which would respond by moving up to prevent the collision, but sideways? I suppose if you had slats on the conveyor it would work.

Oh duh, I just reread your post, that's what you mean by having to calculate positions and velocities yourself, not only for the conveyor or platform, but also for its cargo.


On an unrelated note, how do I add arbitrary shapes for collisions? I see NewtonCreateConvexHull in Newton.cpp, but no examples of it actually being used. The basic tutorial uses
Code: Select all
       dNewtonCollisionMesh collision (world, 1);
       collision.BeginFace();

   // add the face one at a time
   collision.AddFace (4, &points[0][0], 3 * sizeof (dFloat), 0);

   // finish building the collision
   collision.EndFace();


but that system doesn't seem to be exposed to the C API.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby Sweenie » Fri Jan 22, 2016 3:13 am

The example you posted uses the Tree Collider...

Code: Select all
NewtonCollision* NewtonCreateTreeCollision (const NewtonWorld* const newtonWorld, int shapeID);
void NewtonTreeCollisionBeginBuild (const NewtonCollision* const treeCollision);
void NewtonTreeCollisionAddFace (const NewtonCollision* const treeCollision, int vertexCount, const dFloat* const vertexPtr, int strideInBytes, int faceAttribute);
void NewtonTreeCollisionEndBuild (const NewtonCollision* const treeCollision, int optimize);


This will make a collider of the polygons you feed to it.
Note though, a tree collider will not work with moving bodies and will force the body to become static, that is, a mass of 0.
It's mostly used for static level objects there you want detailed collisions like fences, trees etc.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Most basic example

Postby lperkins2 » Tue Jan 26, 2016 2:41 am

Okay, so what's the simplest way to make arbitrary shapes for dynamic bodies? NewtonCreateConvexHull takes a vertexCloud, how do you specify which vertices are connected to which?

There's also NewtonCreateConvexHullFromMesh, is it easier to make a NewtonMesh first?

Thanks for the information so far, it's let me get some working samples, including integrating it into a graphics engine.
lperkins2
 
Posts: 39
Joined: Fri Jul 03, 2015 4:16 am

Re: Most basic example

Postby Sweenie » Tue Jan 26, 2016 1:18 pm

There is an excellent page on the wiki explaining the different colliders.
http://newtondynamics.com/wiki/index.ph ... primitives
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests