hi there, thanks for replies, ok so i got up this morning, checked all my libs and realised i had downloaded newton 2.29 but not extracted it .... ok so how silly i am i was still using 2.24 anyways i updated the dll, and now nothing collides correctly, so i moved back to trying to get boxes to collide.
i found 1 strange thing in my box.init procedure if i change the
CreateNewtonBody matrix to the const vPosition value i pass to the procedure it creates the boxes and all is drawn correct and seems to work, only everything is created in 1 place, not the values of vPosition_ and the debug seems has larger scale ( i have tried several things to fix with no success)
if i create from a seperate vector3df then it does not work, draws boxes elsewhere and physics debug is elsewhere also.
the code:
debug rendering:
Code:
procedure RenderDebugCollision(body: Pointer; vertexCount: Integer; const FaceArray: PFloat; faceId: Integer); cdecl;
var
i: Integer;
p0: Vector3df;
p1: Vector3df;
irr_p1: Vector3df;
irr_p2: Vector3df;
active: Integer;
color: PSColorf;
begin
active := NewtonBodyGetSleepState(body);
case active of
0: color := getSColorfp(255,0,255,0); //getSColorfp(255,0,255,0);
1: color := getSColorfp(255,255,255,0);
end;
i := vertexcount - 1;
{$POINTERMATH ON}
p0.x := FaceArray[i * 3 + 0];
p0.y := FaceArray[i * 3 + 1];
p0.z := FaceArray[i * 3 + 2];
for i := 0 to vertexcount-1 do begin
p1.x := FaceArray[i * 3 + 0];
p1.y := FaceArray[i * 3 + 1];
p1.z := FaceArray[i * 3 + 2];
Driver.draw3DLine(@p0.x, @p1.x, color.toSColor);
p0 := p1;
end;
{$POINTERMATH OFF}
end;
procedure DebugCallback(const body : PNewtonBody); cdecl;
var
m: matrix4;
begin
NewtonBodyGetMatrix(body, @m.M[0]);
NewtonCollisionForEachPolygonDo(NewtonBodyGetCollision(body), @m.M[0], RenderDebugCollision, Pointer(body));
end;
the loop
Code:
lastFPS := -1;
while device.run do begin
driver.beginScene(True, True, getSColor(255, 100, 101, 140), DefEVD, nil);
smgr.drawAll;
gui.drawAll;
DebugCallback(box1.newtonBody);
DebugCallback(box2.newtonBody);
DebugCallback(level.newtonBody);
driver.endScene;
// fps
fps := driver.getFPS;
if (lastFPS <> fps) then begin
device.setWindowCaption(PWideChar(WideString('FPS: ' + str(fps))));
lastFPS := fps;
end;
NewtonUpdate(nWorld, 1.0 / fps);
//NewtonUpdate(nWorld, 0.01);
end;
before the loop i create my game objects (custom records)
Code:
box1.Init(nWorld, smgr, 1, nil, -1, getvector3dfp(0,500,0),getvector3dfp(0,0,0),getvector3dfp(50,50,50), 10);
setMaterialFlag(box1.node, EMF_LIGHTING, false);
setMaterialTexture(box1.node, 0, getTexture(driver, 'models/crate.jpg'));
box2.Init(nWorld, smgr, 1, nil, -1, getvector3dfp(0,650,0),getvector3dfp(0,0,0),getvector3dfp(50,50,50), 10);
setMaterialFlag(box2.node, EMF_LIGHTING, false);
setMaterialTexture(box2.node, 0, getTexture(driver, 'models/crate.jpg'));
for now i just made them static records (no pointer) which then does the following:
Code:
procedure TBox.Init(nWorld: PNewtonWorld; smgr: ISceneManager; vSize: Single; vParent: PISceneNode; vId: Integer; const vPosition_: Pvector3dF; const vRotation_: Pvector3dF; const vScale_: Pvector3dF; pMass : Single);
var
Inertia : Vector3df;
Collision : PNewtonCollision;
offset: matrix4;
begin
offset.makeIdentity();
node := smgr.addCubeSceneNode(vSize, vParent, vId, vPosition_,vRotation_,vScale_);
Collision := NewtonCreateBox(nWorld, vScale_.x, vScale_.y, vScale_.z, 0, offset.pointer);
// Create the rigid body
newtonBody := CreateNewtonBody(nWorld, Collision, @vPosition_.x);
NewtonReleaseCollision(nWorld, Collision);
NewtonBodySetUserData(newtonBody, node);
Inertia.x := pMass * (vScale_.y * vScale_.y + vScale_.z * vScale_.z) / 12;
Inertia.y := pMass * (vScale_.x * vScale_.x + vScale_.z * vScale_.z) / 12;
Inertia.z := pMass * (vScale_.x * vScale_.x + vScale_.y * vScale_.y) / 12;
// Set the bodies mass and moment of inertia
NewtonBodySetMassMatrix(newtonBody, pMass, Inertia.x, Inertia.y, Inertia.z);
// Finally set the callback in which the forces on this body will be applied
NewtonBodySetForceAndTorqueCallBack(newtonBody, ForceAndTorqueCallBack);
NewtonBodySetTransformCallback(newtonBody, SetTransformCallback);
end;
callbacks:
Code:
procedure SetTransformCallback(const body: PNewtonBody; const matr: PFloat; threadIndex: Integer); cdecl;
var
node: ISceneNode;
irrMat: matrix4;
pos: Vector3df;
rot: Vector3df;
begin
node := NewtonBodyGetUserData(body);
if assigned(node) then begin
CopyMemory(irrMat.pointer(), matr, sizeof(Float)*16);
pos := irrMat.getTranslation^;
rot := irrMat.getRotationDegrees();
node.setPosition(@pos.x);
node.setRotation(@rot.x);
end;
end;
procedure ForceAndTorqueCallback(const body: PNewtonBody; timestep: float; threadindex: integer); cdecl;
var
Mass : Single;
Inertia : Vector3df;
Force : Vector3df;
begin
NewtonBodyGetMassMatrix(body, @Mass, @Inertia.x, @Inertia.y, @Inertia.z);
Force := getvector3df(0, -9.8 * Mass, 0); // earth? :)
NewtonBodyAddForce(body, @Force.x);
end;
example app:
download here
thanks hope you can help.
EDIT:
note to mods, there seems to be a glitch in [ code] it keeps adding extra tabs to my code.
Bookmarks