Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Newton Terrain Physics

  1. #11

    Newton Terrain Physics

    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
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #12

    Newton Terrain Physics

    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
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  3. #13

    Newton Terrain Physics

    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:


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

    the code is as follows:
    Code:
    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;;
        NewtonBodyAddForce&#40;Body, @Force&#91;0&#93;&#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;
    
        NewtonBodyAddForce&#40;Body, @Accel&#91;0&#93;&#41;;
    
        NewtonBodyAddForce&#40;Body, @ExternalForce&#91;0&#93;&#41;;
        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
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  4. #14

    Newton Terrain Physics

    move the body so that its within the world space

  5. #15

    Newton Terrain Physics

    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
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  6. #16

    Newton Terrain Physics

    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
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •