PDA

View Full Version : Newton Terrain Physics

M109uk
09-06-2007, 10:48 AM
Hi,

I have a terrain, and i want to use newton for collision detection.. i have the terrain working and the physics of course in not working to good,

my code is:

procedure TMTFloor.CreateCollisionTree;
var
Face&#58; Array &#91;0..3&#93; Of Geometry.TVector3f;
M&#58; Geometry.TMatrix4f;
Min,Max&#58; Geometry.TVector3f;
X,Z&#58; Integer;
begin
nCollTree &#58;= NewtonCreateTreeCollision&#40;nWorld, Nil&#41;;
NewtonTreeCollisionBeginBuild&#40;nCollTree&#41;;

For X &#58;= 0 To MAP_SIZE Do
Begin
For Z &#58;= 0 To MAP_SIZE Do
Begin
xz &#58;= floorIndex&#40;X, Z&#41;;
x1z &#58;= floorIndex&#40;X+1, Z&#41;;
xz1 &#58;= floorIndex&#40;X, Z+1&#41;;
x1z1 &#58;= floorIndex&#40;X+1, Z+1&#41;;

Face&#91;0&#93; &#58;= Vector3fMake&#40;X*lBlock, height&#91;xz&#93;, Z*lBlock&#41;;
Face&#91;1&#93; &#58;= Vector3fMake&#40;&#40;X+1&#41;*lBlock, height&#91;x1z&#93;, Z*lBlock&#41;;
Face&#91;2&#93; &#58;= Vector3fMake&#40;&#40;X+1&#41;*lBlock, height&#91;x1z1&#93;, &#40;Z+1&#41;*lBlock&#41;;
Face&#91;3&#93; &#58;= Vector3fMake&#40;X*lBlock, height&#91;xz1&#93;, &#40;Z+1&#41;*lBlock&#41;;

End;
End;

NewtonTreeCollisionEndBuild&#40;nCollTree, 0&#41;;
nBody &#58;= NewtonCreateBody&#40;nWorld, nCollTree&#41;;

M &#58;= IdentityHmgMatrix;
NewtonBodySetMatrix&#40;nBody, @M&#91;0, 0&#93;&#41;;
NewtonCollisionCalculateAABB&#40;nCollTree, @M&#91;0, 0&#93;, @Min&#91;0&#93;, @Max&#91;0&#93;&#41;;
NewtonSetWorldSize&#40;nWorld, @Min&#91;0&#93;, @Max&#91;0&#93;&#41;;
NewtonReleaseCollision&#40;nWorld, nCollTree&#41;;
end;

lBlock is a const value set at 8.001, this is because the terrain is scaled by 8.001, 1, 8.001..

If i dont use scale and i take out lBlock then the physics works fine, so im positive its something to with the scale, so i removed glScale from the terrain, and i am now using the same coordinates given to Newton, the terrain renders correctly yet still the collision detection does'nt.. am i missing something??

Thanks for any help :)

JernejL
09-06-2007, 03:05 PM
what exactly happens wrong? make a debug renderer of the scene.

M109uk
09-06-2007, 03:51 PM
there is a form of collision detection, it just does'nt match the terrain.. how would i go about doing a debug render, i am rendering the terrain with exaclty the same vertex data that i am putting into newton.

JernejL
09-06-2007, 07:14 PM
there is a form of collision detection, it just does'nt match the terrain.. how would i go about doing a debug render, i am rendering the terrain with exaclty the same vertex data that i am putting into newton.

see how newton examples implement debug rendering, and the geometry comes from newton's internals - not your app.

M109uk
10-06-2007, 10:33 AM
I have put in debug rendering, and it is correct, exactly the same as the terrain im rendering...

Heres the app to show whats wrong, the debug lines on on automaticlly.

The modified code:

procedure TMTFloor.CreateCollisionTree;
var
Face&#58; Array &#91;0..3&#93; Of Geometry.TVector3f;
M&#58; Geometry.TMatrix4f;
Min,Max&#58; Geometry.TVector3f;
X,Z,x1,z1,x1t,z1t,xz,x1z,xz1,x1z1&#58; Integer;
xt,zt&#58; Double;
begin
nCollTree &#58;= NewtonCreateTreeCollision&#40;nWorld, Nil&#41;;
NewtonTreeCollisionBeginBuild&#40;nCollTree&#41;;

For X &#58;= 0 To MAP_SIZE Do
Begin
For Z &#58;= 0 To MAP_SIZE Do
Begin
xz &#58;= floorIndex&#40;X, Z&#41;;
x1z &#58;= floorIndex&#40;X+1, Z&#41;;
xz1 &#58;= floorIndex&#40;X, Z+1&#41;;
x1z1 &#58;= floorIndex&#40;X+1, Z+1&#41;;

Face&#91;0&#93; &#58;= Vector3fMake&#40;X*lBlock, hf&#91;xz&#93;, Z*lBlock&#41;;
Face&#91;1&#93; &#58;= Vector3fMake&#40;&#40;X+1&#41;*lBlock, hf&#91;x1z&#93;, Z*lBlock&#41;;
Face&#91;2&#93; &#58;= Vector3fMake&#40;&#40;X+1&#41;*lBlock, hf&#91;x1z1&#93;, &#40;Z+1&#41;*lBlock&#41;;
Face&#91;3&#93; &#58;= Vector3fMake&#40;X*lBlock, hf&#91;xz1&#93;, &#40;Z+1&#41;*lBlock&#41;;

End;
End;

NewtonTreeCollisionEndBuild&#40;nCollTree, 0&#41;;
nBody &#58;= NewtonCreateBody&#40;nWorld, nCollTree&#41;;

tmpM &#58;= IdentityHmgMatrix;
NewtonBodySetMatrix&#40;nBody, @M&#91;0,0&#93;&#41;;
NewtonCollisionCalculateAABB&#40;nCollTree, @M&#91;0,0&#93;, @Min&#91;0&#93;, @Max&#91;0&#93;&#41;;
NewtonSetWorldSize&#40;nWorld, @Min&#91;0&#93;, @Max&#91;0&#93;&#41;;
NewtonReleaseCollision&#40;nWorld, nCollTree&#41;;
end;

Many thanks..

JernejL
10-06-2007, 02:27 PM
I have put in debug rendering, and it is correct, exactly the same as the terrain im rendering...

did you use the actual NewtonBodyForEachPolygonDo ?

your app is totally non-interactive, i cant even see myself how the physics are supposedly "wrong".

and how about some instructions to go with the test app?

M109uk
10-06-2007, 04:47 PM
For debug im using:

procedure Debug_ShowGeometryCollision&#40;const Body&#58; PNewtonBody; VertexCount&#58; Integer; const FaceArray&#58; PFloat; FaceId&#58; Int&#41;; cdecl;
var
i&#58; Integer;
v0,v1&#58; TVector3f;
vA&#58; Array Of Single;
begin
If VertexCount = 0 Then Exit;
SetLength&#40;vA, VertexCount*3&#41;;
Move&#40;FaceArray^, vA&#91;0&#93;, VertexCount*3*SizeOf&#40;Single&#41;&#41;;
v0&#91;0&#93; &#58;= vA&#91;&#40;VertexCount-1&#41;*3&#93;;
v0&#91;1&#93; &#58;= vA&#91;&#40;VertexCount-1&#41;*3+1&#93;;
v0&#91;2&#93; &#58;= vA&#91;&#40;VertexCount-1&#41;*3+2&#93;;
For i &#58;= 0 To VertexCount-1 Do
Begin
v1&#91;0&#93; &#58;= vA&#91;i*3&#93;;
v1&#91;1&#93; &#58;= vA&#91;i*3+1&#93;;
v1&#91;2&#93; &#58;= vA&#91;i*3+2&#93;;
glVertex3f&#40;v0&#91;0&#93;, v0&#91;1&#93;, v0&#91;2&#93;&#41;;
glVertex3f&#40;v1&#91;0&#93;, v1&#91;1&#93;, v1&#91;2&#93;&#41;;
v0 &#58;= v1;
End;
end;

procedure Debug_ShowBodyCollision&#40;const Body&#58; PNewtonBody&#41;; cdecl;
begin
NewtonBodyForEachPolygonDo&#40;Body, Debug_ShowGeometryCollision&#41;;
end;

Sorry my bad, completly forgot :s

forward=W
backward=S
turn left=A
turn right=D
run=SHIFT
Quit=ESC
Regenerate terrain=F1

JernejL
10-06-2007, 05:19 PM
what about, dropping some cubes onto terrain? :/

M109uk
10-06-2007, 06:09 PM
what about, dropping some cubes onto terrain? :/

I dont have any other objects except for the camera and the terrain atm... i will implement some cubes in tonight :)

JernejL
10-06-2007, 09:04 PM
Then how do you know that something is wrong in newton if you only got the terrain in without even testing?

what is the actual problem?

M109uk
10-06-2007, 09:10 PM
I know its not working correctly because the camera is the character, i have newton physics assigned to the camera too.

i want the camera to follow on top of the terrain, but it doesnt work, you will notice if you walk around in the demo.

M109uk
11-06-2007, 12:01 AM
I think i might of found the reason, i forgot to delete some extra translate routines out of my camera class, i will check it tomorow.

Thanks again :)

M109uk
14-06-2007, 05:00 PM
Ok, i still have the same problem, if not worse :s

I have now got a Player Class (based upon the SDLCharacterController Demo from Sascha), however it doesnt seem to work as plan, it does'nt call the forceback procedures or the jumps feedback function..

I have the ellipse hovering above the terrain without any movement of any kind:
http://www.pulse-soft.oneuk.com/images/stories/terrain_newton.jpg

(Note: The screenshot is rendered using the geometry from Newton.)

the code is as follows:

const
PlayerSpeed = 10;
PlayerMass = 25;
PlayerJumpForce = 5000;

procedure playerApplyForceCallback&#40;const Body&#58; PNewtonBody&#41;; cdecl;
var
Mass,Ixx,Iyy,Izz,Length&#58; Single;
Force,Velocity,GoalVelocity,Accel&#58; TVector3f;
UserData&#58; Pointer;
begin
Accel &#58;= Vector3fMake&#40;0,0,0&#41;;
Time &#58;= SDL_GetTicks-Time;
UserData &#58;= NewtonBodyGetUserData&#40;Body&#41;;
If UserData = Nil Then Exit;

With TMTPlayer&#40;UserData&#41; Do
Begin
NewtonBodyGetMassMatrix&#40;Body, @Mass, @Ixx, @Iyy, @Izz&#41;;
Force &#58;= Vector3fMake&#40;0, -9.8 * Mass, 0&#41;;

NewtonBodyGetMatrix&#40;Body, @Matrix&#91;0,0&#93;&#41;;

Length &#58;= Sqrt&#40;Sqr&#40;Movement&#91;0&#93;&#41;+Sqr&#40;Movement&#91;2&#93;&#41;&#41;;
If Length = 0 Then Length &#58;= 1;
Movement &#58;= Vector3fMake&#40;Movement&#91;0&#93;/Length, Movement&#91;1&#93;, Movement&#91;2&#93;/Length&#41;;

NewtonBodyGetVelocity&#40;Body, @Velocity&#91;0&#93;&#41;;

GoalVelocity &#58;= Vector3fMake&#40;Movement&#91;0&#93;*PlayerSpeed, 0, Movement&#91;2&#93;*PlayerSpeed&#41;;

If Run Then
GoalVelocity &#58;= Vector3fMake&#40;GoalVelocity&#91;0&#93;*3, GoalVelocity&#91;1&#93;*3, GoalVelocity&#91;2&#93;*3&#41;;

If Time = 0 Then Time &#58;= 1;
Accel&#91;0&#93; &#58;= 0.3*&#40;&#40;GoalVelocity&#91;0&#93;-Velocity&#91;0&#93;&#41;/&#40;Time/10&#41;&#41;*100;
Accel&#91;2&#93; &#58;= 0.3*&#40;&#40;GoalVelocity&#91;2&#93;-Velocity&#91;2&#93;&#41;/&#40;Time/10&#41;&#41;*100;

If Accel&#91;0&#93; > 200 Then Accel&#91;0&#93; &#58;= 200;
If Accel&#91;0&#93; <200> 200 Then Accel&#91;2&#93; &#58;= 200;
If Accel&#91;2&#93; < -200 Then Accel&#91;2&#93; &#58;= -200;

If &#40;ExternalForce&#91;0&#93; <> 0&#41; or &#40;ExternalForce&#91;1&#93; <> 0&#41; or &#40;ExternalForce&#91;2&#93; <> 0&#41; then
ExternalForce &#58;= Vector3fMake&#40;0, 0, 0&#41;;
End;
Time &#58;= SDL_GetTicks;
end;

function playerRayCastFilter&#40;const Body&#58; PNewtonBody; const hitNormal&#58; PFloat; collisionID&#58; NewtonImport.Int; userData&#58; Pointer; intersectParam&#58; Float &#41;&#58; Float; cdecl;
begin
Result &#58;= IntersectParam;
If Body = Player.nBody Then Exit;
Distance &#58;= IntersectParam;
end;

constructor TMTPlayer.Create&#40;const Pos,Rot&#58; Geometry.TVector3f&#41;;
const
UpDir&#58; Geometry.TVector3f = &#40;0, 1, 0&#41;;
var
Collider&#58; PNewtonCollision;
StartMatrix&#58; Geometry.TMatrix4f;
begin
Size &#58;= Vector3fMake&#40;1.5, 3.5, 1.5&#41;;
Collider &#58;= NewtonCreateSphere&#40;nWorld, Size&#91;0&#93;, Size&#91;1&#93;, Size&#91;2&#93;, Nil&#41;;
nBody &#58;= NewtonCreateBody&#40;nWorld, Collider&#41;;

StartMatrix &#58;= IdentityHmgMatrix;
StartMatrix&#91;3&#93;&#91;0&#93; &#58;= Pos&#91;0&#93;;
StartMatrix&#91;3&#93;&#91;1&#93; &#58;= Pos&#91;1&#93;;
StartMatrix&#91;3&#93;&#91;2&#93; &#58;= Pos&#91;2&#93;;
NewtonBodySetMatrix&#40;nBody, @StartMatrix&#91;0, 0&#93;&#41;;
NewtonBodySetMassMatrix&#40;nBody, 12, 1/5*PlayerMass*&#40;Sqr&#40;Size&#91;1&#93;&#41;+Sqr&#40;Size&#91;0&#93;&#41;&#41;, 1/5*PlayerMass*&#40;Sqr&#40;Size&#91;2&#93;&#41;+Sqr&#40;Size&#91;0&#93;&#41;&#41;, 1/5*PlayerMass*&#40;Sqr&#40;Size&#91;2&#93;&#41;+Sqr&#40;Size&#91;1&#93;&#41;&#41;&#41;;
NewtonConstraintCreateUpVector&#40;nWorld, @UpDir&#91;0&#93;, nBody&#41;;
NewtonBodySetForceAndTorqueCallBack&#40;nBody, playerApplyForceCallback&#41;;

NewtonReleaseCollision&#40;nWorld, Collider&#41;;

NewtonBodySetAutoFreeze&#40;nBody, 0&#41;;
NewtonWorldUnfreezeBody&#40;nWorld, nBody&#41;;

NewtonBodySetUserData&#40;nBody, Self&#41;;
NewtonBodySetMaterialGroupID&#40;nBody, Material.PlayerID&#41;;
end;

destructor TMTPlayer.Destroy;
begin
NewtonDestroyBody&#40;nWorld, nBody&#41;;
inherited Destroy;
end;

procedure TMTPlayer.Jump;
var
P1,P2&#58; TVector3f;
begin
P1 &#58;= Vector3fMake&#40;Matrix&#91;3, 0&#93;, Matrix&#91;3, 1&#93;, Matrix&#91;3, 2&#93;&#41;;
P2 &#58;= Vector3fMake&#40;Matrix&#91;3, 0&#93;, Matrix&#91;3, 1&#93;-Size&#91;1&#93;*1.1, Matrix&#91;3, 2&#93;&#41;;
Distance &#58;= 1.1;
NewtonWorldRayCast&#40;nWorld, @P1&#91;0&#93;, @P2&#91;0&#93;, @playerRayCastFilter, @Self, Nil&#41;;
If Distance <1> 3600 Then Rotation&#91;1&#93; &#58;= Rotation&#91;1&#93;-360;
If Rotation&#91;1&#93; <0> 80 Then Rotation&#91;0&#93; &#58;= 80;
If Rotation&#91;0&#93; < -80 Then Rotation&#91;0&#93; &#58;= -80;
End;
End;

Run &#58;= &#40;&#40;xkLShift In Keys&#41; Or &#40;xkRShift In Keys&#41;&#41;;
end;

I have gone through the code and cant work out what im missing..

Thanks

tux
14-06-2007, 09:01 PM
move the body so that its within the world space

M109uk
14-06-2007, 09:10 PM
Sorry, im not sure what you mean?

Im creating the player object so its within the terrains X and Z.. i placed the Y at 100 for the time being since the terrain is randomly generated.

M109uk
14-06-2007, 11:18 PM
Ah great, no worries... i guess you meant to have it within the min and max of the world.. all seems to work great now :) many thanks