Code:
const
PlayerSpeed = 10;
PlayerMass = 25;
PlayerJumpForce = 5000;
procedure playerApplyForceCallback(const Body: PNewtonBody); cdecl;
var
Mass,Ixx,Iyy,Izz,Length: Single;
Force,Velocity,GoalVelocity,Accel: TVector3f;
UserData: Pointer;
begin
Accel := Vector3fMake(0,0,0);
Time := SDL_GetTicks-Time;
UserData := NewtonBodyGetUserData(Body);
If UserData = Nil Then Exit;
With TMTPlayer(UserData) Do
Begin
NewtonBodyGetMassMatrix(Body, @Mass, @Ixx, @Iyy, @Izz);
Force := Vector3fMake(0, -9.8 * Mass, 0);
NewtonBodyAddForce(Body, @Force[0]);
NewtonBodyGetMatrix(Body, @Matrix[0,0]);
Length := Sqrt(Sqr(Movement[0])+Sqr(Movement[2]));
If Length = 0 Then Length := 1;
Movement := Vector3fMake(Movement[0]/Length, Movement[1], Movement[2]/Length);
NewtonBodyGetVelocity(Body, @Velocity[0]);
GoalVelocity := Vector3fMake(Movement[0]*PlayerSpeed, 0, Movement[2]*PlayerSpeed);
If Run Then
GoalVelocity := Vector3fMake(GoalVelocity[0]*3, GoalVelocity[1]*3, GoalVelocity[2]*3);
If Time = 0 Then Time := 1;
Accel[0] := 0.3*((GoalVelocity[0]-Velocity[0])/(Time/10))*100;
Accel[2] := 0.3*((GoalVelocity[2]-Velocity[2])/(Time/10))*100;
If Accel[0] > 200 Then Accel[0] := 200;
If Accel[0] <200> 200 Then Accel[2] := 200;
If Accel[2] < -200 Then Accel[2] := -200;
NewtonBodyAddForce(Body, @Accel[0]);
NewtonBodyAddForce(Body, @ExternalForce[0]);
If (ExternalForce[0] <> 0) or (ExternalForce[1] <> 0) or (ExternalForce[2] <> 0) then
ExternalForce := Vector3fMake(0, 0, 0);
End;
Time := SDL_GetTicks;
end;
function playerRayCastFilter(const Body: PNewtonBody; const hitNormal: PFloat; collisionID: NewtonImport.Int; userData: Pointer; intersectParam: Float ): Float; cdecl;
begin
Result := IntersectParam;
If Body = Player.nBody Then Exit;
Distance := IntersectParam;
end;
constructor TMTPlayer.Create(const Pos,Rot: Geometry.TVector3f);
const
UpDir: Geometry.TVector3f = (0, 1, 0);
var
Collider: PNewtonCollision;
StartMatrix: Geometry.TMatrix4f;
begin
Size := Vector3fMake(1.5, 3.5, 1.5);
Collider := NewtonCreateSphere(nWorld, Size[0], Size[1], Size[2], Nil);
nBody := NewtonCreateBody(nWorld, Collider);
StartMatrix := IdentityHmgMatrix;
StartMatrix[3][0] := Pos[0];
StartMatrix[3][1] := Pos[1];
StartMatrix[3][2] := Pos[2];
NewtonBodySetMatrix(nBody, @StartMatrix[0, 0]);
NewtonBodySetMassMatrix(nBody, 12, 1/5*PlayerMass*(Sqr(Size[1])+Sqr(Size[0])), 1/5*PlayerMass*(Sqr(Size[2])+Sqr(Size[0])), 1/5*PlayerMass*(Sqr(Size[2])+Sqr(Size[1])));
NewtonConstraintCreateUpVector(nWorld, @UpDir[0], nBody);
NewtonBodySetForceAndTorqueCallBack(nBody, playerApplyForceCallback);
NewtonReleaseCollision(nWorld, Collider);
NewtonBodySetAutoFreeze(nBody, 0);
NewtonWorldUnfreezeBody(nWorld, nBody);
NewtonBodySetUserData(nBody, Self);
NewtonBodySetMaterialGroupID(nBody, Material.PlayerID);
end;
destructor TMTPlayer.Destroy;
begin
NewtonDestroyBody(nWorld, nBody);
inherited Destroy;
end;
procedure TMTPlayer.Jump;
var
P1,P2: TVector3f;
begin
P1 := Vector3fMake(Matrix[3, 0], Matrix[3, 1], Matrix[3, 2]);
P2 := Vector3fMake(Matrix[3, 0], Matrix[3, 1]-Size[1]*1.1, Matrix[3, 2]);
Distance := 1.1;
NewtonWorldRayCast(nWorld, @P1[0], @P2[0], @playerRayCastFilter, @Self, Nil);
If Distance <1> 3600 Then Rotation[1] := Rotation[1]-360;
If Rotation[1] <0> 80 Then Rotation[0] := 80;
If Rotation[0] < -80 Then Rotation[0] := -80;
End;
End;
Run := ((xkLShift In Keys) Or (xkRShift In Keys));
end;
I have gone through the code and cant work out what im missing..
Bookmarks