program PhysicsDemo; {-$IMAGEBASE $500000} {-$M 16777216 2147483647} {$apptype console} {$DEFINE FULLSCREEN} {-$DEFINE REALFULLSCREEN} {$IFDEF FPC} {$MODE Delphi} {$IFDEF CPUI386} {$DEFINE CPU386} {$ASMMODE INTEL} {$ENDIF} {$ENDIF} {$j+} //FastMM4, //heapmm, uses SysUtils, Classes, Math, SDL, gamegraphics in 'GameGraphics.pas', opengl in 'opengl.pas', sandboxfile in 'pmf2pas\sandboxfile.pas', gameglobals in 'gameglobals.pas', shader in 'shader.pas', math2d in 'Math2D.pas', kraft in 'kraft.pas'; const ScreenBPP=32; TwoPI:single=2*PI; MaxObjects=15; cwChop:word=$F7B; PhysicsHz=120; PhysicsTimeStep=1.0/PhysicsHz; var Event:TSDL_Event; SurfaceWindow:PSDL_Window; SurfaceContext:PSDL_GLContext; SDLDisplayMode:TSDL_DisplayMode; VideoFlags:longword; SDLWaveFormat:TSDL_AudioSpec; BufPosition:integer; BestWidth,BestHeight,ViewPortWidth,ViewPortHeight,ViewPortX,ViewPortY:longint; Fullscreen:boolean; ShowCursor:boolean; SDLRunning,OldShowCursor:boolean; type TKeyByteArray=array[0..255] of bytebool; var HighResolutionTimerInstance:TKraftHighResolutionTimer; qcf,qc0,qc1:int64; First:boolean; Now:longword; // MousePos:Windows.TPoint; LastKeyByteArray,KeyByteArray:TKeyByteArray; SDLKeyByteArray:^TKeyByteArray; StartTime,FST,FST2,FET2:int64; ti,TimeStep:double; Physics:TKraft; RigidBody:TKraftRigidBody; RigidBodyFloor:TKraftRigidBody; // ShapeFloorBoxes:array[0..5] of TKraftShapeBox; ShapeFloorPlane:TKraftShapePlane; RigidBodyMesh:TKraftRigidBody; Mesh:TKraftMesh; ShapeMesh:TKraftShapeMesh; ConvexHull:TKraftConvexHull; ObjectRigidBody:array[0..17] of TKraftRigidBody; ObjectShape:array[0..17] of TKraftShape; pp:array[0..4,0..1] of TKraftVector3; // WorldPlaneDistanceConstraints:array[0..4] of TKraftConstraintJointWorldPlaneDistance; RopeConstraints:array[0..3] of TKraftConstraint; HingeConstraints:array[0..3] of TKraftConstraint; SliderConstraints:array[0..3] of TKraftConstraint; RagdollRigidBody:array[0..1+2+2+5] of TKraftRigidBody; RagdollShape:array[0..1+2+2+5] of TKraftShape; RagdollConstraints:array[0..9] of TKraftConstraint; const Dir:TKraftVector3=(x:0;y:0;z:1); var OldPosition,OldView,OldUpVector:TKraftVector3; Position,View,UpVector:TKraftVector3; LastPosition,LastView,LastUpVector:TKraftVector3; InterpolatedPosition,InterpolatedView,InterpolatedUpVector:TKraftVector3; FPS:double; DoDrawDebugStuff,DoDrawMoreDebugStuff:longbool; Font:TTRIFont; FontWidth,FontHeight:longint; PixelProjectionMatrix:TMatrix; procedure ShowText(x,y,z:single;Centered:boolean;pText:ansistring); var Matrix:TMatrix; begin if Centered then begin x:=x-(length(pText)*FontWidth); end; Matrix.Values[0]:=1.0; Matrix.Values[1]:=0.0; Matrix.Values[2]:=0.0; Matrix.Values[3]:=1.0; Matrix.Values[4]:=x+2;//TextRect.Left+(((TextRect.Right-TextRect.Left)-w)*0.5); Matrix.Values[5]:=y+2;//TextRect.Top+(((TextRect.Bottom-TextRect.Top)-h)*0.5); UseShader(0); Font.Draw(pText,Matrix.Multiply(PixelProjectionMatrix)); UseShader(0); end; function CheckCamera(const OldPosition,NewPosition:TKraftVector3;var Delta:TKraftVector3):boolean; var //Time,DirectionLength:single; Direction,NormalizedDirection{,Point,Normal}:TKraftVector3; // Shape:TKraftShape; begin Direction:=Vector3Sub(NewPosition,OldPosition); NormalizedDirection:=Direction; {DirectionLength:=Vector3NormalizeEx(NormalizedDirection); if Physics.SphereCast(OldPosition,NormalizedDirection,0.125,DirectionLength,Shape,Time,Point,Normal) then begin Delta:=Vector3ScalarMul(NormalizedDirection,Time); result:=false; end else begin Delta:=Direction; result:=true; end;} Delta:=Direction; result:=true; end; procedure MoveCamera(Speed:glFloat); var NewPosition,Delta:TKraftVector3; begin NewPosition:=Vector3Add(Position,Vector3ScalarMul(Vector3NormEx(Vector3Sub(View,Position)),Speed)); //CheckCamera(Position,NewPosition,Delta); Delta:=Vector3Sub(NewPosition,Position); Position:=Vector3Add(Position,Delta); View:=Vector3Add(View,Delta); end; procedure MoveCameraSidewards(Speed:glFloat); var NewPosition,Delta:TKraftVector3; begin NewPosition:=Vector3Add(Position,Vector3ScalarMul(Vector3NormEx(Vector3Cross(UpVector,Vector3NormEx(Vector3Sub(View,Position)))),Speed)); //CheckCamera(Position,NewPosition,Delta); Delta:=Vector3Sub(NewPosition,Position); Position:=Vector3Add(Position,Delta); View:=Vector3Add(View,Delta); end; procedure MoveCameraUpwards(Speed:glFloat); var ViewVector,NewPosition,Delta:TKraftVector3; begin ViewVector:=Vector3NormEx(Vector3Sub(View,Position)); NewPosition:=Vector3Add(Position,Vector3ScalarMul(Vector3NormEx(Vector3Cross(ViewVector,Vector3NormEx(Vector3Cross(UpVector,ViewVector)))),Speed)); //CheckCamera(Position,NewPosition,Delta); Delta:=Vector3Sub(NewPosition,Position); Position:=Vector3Add(Position,Delta); View:=Vector3Add(View,Delta); end; PROCEDURE RotateCamera(VAR Position:TKraftVector3;CONST X,Y,Z:glFloat); VAR vVector:TKraftVector3; BEGIN vVector.X:=View.X-Position.X; vVector.Y:=View.Y-Position.Y; vVector.Z:=View.Z-Position.Z; IF X<>0 THEN BEGIN View.Z:=(Position.Z+(SIN(X)*vVector.Y))+(COS(X)*vVector.Z); View.Y:=(Position.Y+(COS(X)*vVector.Y))-(SIN(X)*vVector.Z); END; IF Y<>0 THEN BEGIN View.Z:=(Position.Z+(SIN(Y)*vVector.X))+(COS(Y)*vVector.Z); View.X:=(Position.X+(COS(Y)*vVector.X))-(SIN(Y)*vVector.Z); END; IF Z<>0 THEN BEGIN View.X:=(Position.X+(SIN(Z)*vVector.Y))+(COS(Z)*vVector.X); View.Y:=(Position.Y+(COS(Z)*vVector.Y))-(SIN(Z)*vVector.X); END; END; {PROCEDURE MoveCameraByMouse; VAR mx,my:longint; middleX,middleY:INTEGER; deltaY,rotateY:glFloat; BEGIN middleX:=ScreenWidth SHR 1; middleY:=ScreenHeight SHR 1; SDL_GetMouseState(@mx,@my); IF (mX=middleX) AND (mY=middleY) THEN EXIT; SDL.SDL_WarpMouseInWindow(SurfaceWindow,middleX,middleY); rotateY:=(middleX-mX)/500; deltaY:=(middleY-mY)/1000; View.Y:=View.Y+(deltaY*5.0); IF (View.Y-Position.Y)>10 THEN View.Y:=Position.Y+10; IF (View.Y-Position.Y)<(-10) THEN View.Y:=Position.Y-10; RotateCamera(Position,0,-rotateY,0); END;{} procedure SaveOldCamera; begin OldPosition:=Position; OldView:=View; OldUpVector:=UpVector; end; procedure RestoreOldCamera; begin Position:=OldPosition; View:=OldView; UpVector:=OldUpVector; end; procedure TestCamera; var m:TKraftVector3; begin {m:=Vector3ScalarMul(Gravitation,TimeStep); Position:=Vector3Add(Position,m); View:=Vector3Add(View,m);} { PhysicsCollidePoint(Collide,Vector3Add(Position,Vector3ScalarMul(Vector3Sub(View,Position),1)),8); m:=Vector3Origin; for i:=0 to Collide.NumContacts-1 do begin m:=Vector3Add(m,Vector3ScalarMul(Collide.Contacts^[i].Normal,Collide.Contacts^[i].Depth/Collide.NumContacts)); end; Position:=Vector3Add(Position,m); View:=Vector3Add(View,m);} m:=Position; if Physics.PushSphere(m,0.5) then begin View:=Vector3Add(View,Vector3Sub(m,Position)); Position:=m; end; end; var DragRigidBody:TKraftRigidBody; DragShape:TKraftShape; DragDelta:TKraftVector3; DragDistance:single; DragRigidBodyTransform:TKraftMatrix4x4; DragCameraTransform:TKraftMatrix4x4; procedure StartDrag; var Point,Normal:TKraftVector3; s:TKraftShape; t:single; begin DragRigidBody:=nil; DragShape:=nil; if Physics.RayCast(Position,Vector3Norm(Vector3Sub(View,Position)),1024.0,s,t,Point,Normal) then begin if assigned(s) and assigned(s.RigidBody) and (s.RigidBody.RigidBodyType=krbtDYNAMIC) then begin DragRigidBody:=s.RigidBody; DragShape:=s; DragDelta:=Vector3Sub(s.RigidBody.Sweep.c,Position); DragDistance:=Vector3Dist(s.RigidBody.Sweep.c,Position); // DragRigidBody.CollisionGroups:=[]; Exclude(DragRigidBody.Flags,krbfAwake); DragRigidBodyTransform:=DragRigidBody.WorldTransform; DragCameraTransform:=Matrix4x4LookAt(Position,View,UpVector); DragRigidBody.Refilter; DragRigidBody.StoreWorldTransform; end; end; end; procedure StopDrag; begin if assigned(DragRigidBody) then begin Include(DragRigidBody.Flags,krbfAwake); // DragRigidBody.CollisionGroups:=[0]; DragRigidBody.SetToAwake; DragRigidBody.StoreWorldTransform; end; DragRigidBody:=nil; DragShape:=nil; end; procedure ProcessDrag; var CameraTransform:TKraftMatrix4x4; begin if assigned(DragRigidBody) then begin if assigned(DragRigidBody) then begin CameraTransform:=Matrix4x4LookAt(InterpolatedPosition,InterpolatedView,InterpolatedUpVector); DragRigidBody.SetWorldTransformation(Matrix4x4TermMul(DragRigidBodyTransform,Matrix4x4TermMulInverted(DragCameraTransform,CameraTransform))); // DragRigidBody.SetWorldPosition(Vector3Add(Position,Vector3ScalarMul(Vector3Norm(Vector3Sub(View,Position)),DragDistance))); DragRigidBody.LinearVelocity:=Vector3Origin; DragRigidBody.AngularVelocity:=Vector3Origin; DragRigidBody.SetToSleep; DragRigidBody.Refilter; DragRigidBody.StoreWorldTransform; DragRigidBody.InterpolateWorldTransform(0.0); end; end; end;{} var GrabRigidBody:TKraftRigidBody; GrabShape:TKraftShape; GrabDelta:TKraftVector3; GrabDistance:single; GrabRigidBodyTransform:TKraftMatrix4x4; GrabCameraTransform:TKraftMatrix4x4; GrabConstraint:TKraftConstraintJointGrab; procedure StartGrab; var Point,Normal:TKraftVector3; s:TKraftShape; t:single; begin GrabRigidBody:=nil; GrabShape:=nil; if Physics.RayCast(Position,Vector3Norm(Vector3Sub(View,Position)),1024.0,s,t,Point,Normal) then begin if assigned(s) and assigned(s.RigidBody) and (s.RigidBody.RigidBodyType=krbtDYNAMIC) then begin GrabRigidBody:=s.RigidBody; GrabShape:=s; GrabDelta:=Vector3Sub(s.RigidBody.Sweep.c,Position); GrabDistance:=Vector3Dist(s.RigidBody.Sweep.c,Position); GrabConstraint:=TKraftConstraintJointGrab.Create(Physics,GrabRigidBody,Point,5.0,0.7,GrabRigidBody.Mass*1000.0); GrabRigidBody.SetToAwake; end; end; end; procedure StopGrab; begin if assigned(GrabRigidBody) then begin GrabRigidBody.SetToAwake; FreeAndNil(GrabConstraint); end; GrabRigidBody:=nil; GrabShape:=nil; GrabConstraint:=nil; end; procedure ProcessGrab; begin if assigned(GrabRigidBody) and assigned(GrabConstraint) then begin GrabConstraint.SetWorldPoint(Vector3Add(Position,Vector3ScalarMul(Vector3Norm(Vector3Sub(View,Position)),GrabDistance))); GrabRigidBody.SetToAwake; end; end; const GL_POLYGON_OFFSET_FACTOR = $8038; GL_POLYGON_OFFSET_UNITS = $2A00; GL_POLYGON_OFFSET_POINT = $2A01; GL_POLYGON_OFFSET_LINE = $2A02; GL_POLYGON_OFFSET_FILL = $8037; //procedure glPolygonOffset(factor, units: GLfloat); stdcall; external 'opengl32.dll'; const Licht0Pos: array[0..3] of GLFLOAT = (0.0,160.0,160.0,1.0); var DrawSunDisplayList:glUInt; DrawMeshTreeDisplayList:glUInt; procedure DrawSun(const CameraMatrix:TKraftMatrix4x4); const lats=16; longs=16; pi2=pi*2.0; Radius=15.0; var i,j:longint; lat0,z0,zr0,lat1,z1,zr1,lng,x,y:single; WorldTransform,ModelViewMatrix:TKraftMatrix4x4; begin glPushMatrix; glMatrixMode(GL_MODELVIEW); WorldTransform:=Matrix4x4Translate(Licht0Pos[0],Licht0Pos[1],Licht0Pos[2]); ModelViewMatrix:=Matrix4x4TermMul(WorldTransform,CameraMatrix); glLoadMatrixf(pointer(@ModelViewMatrix)); if DrawSunDisplayList=0 then begin DrawSunDisplayList:=glGenLists(1); glNewList(DrawSunDisplayList,GL_COMPILE); for i:=0 to lats do begin lat0:=pi*(((i-1)/lats)-0.5); z0:=sin(lat0)*Radius; zr0:=cos(lat0)*Radius; lat1:=pi*((i/lats)-0.5); z1:=sin(lat1)*Radius; zr1:=cos(lat1)*Radius; glBegin(GL_QUAD_STRIP); for j:=0 to longs do begin lng:=pi2*((j-1)/longs); x:=cos(lng); y:=sin(lng); glNormal3f(x*zr1,y*zr1,z1); glVertex3f(x*zr1,y*zr1,z1); glNormal3f(x*zr0,y*zr0,z0); glVertex3f(x*zr0,y*zr0,z0); end; glEnd; end; glEndList; end; if DrawSunDisplayList<>0 then begin glCallList(DrawSunDisplayList); end; glPopMatrix; end; procedure DrawObjectTreeNode(Tree:TKraftDynamicAABBTree;Node:PKraftDynamicAABBTreeNode); var I: integer; begin //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); for I:=0 to 1 do begin if Node^.Children[I]>=0 then begin DrawObjectTreeNode(Tree,@Tree.Nodes[Node^.Children[I]]); end; end; end; procedure DrawMeshTree(Shape:TKraftShapeMesh;const CameraMatrix:TKraftMatrix4x4); var i:integer; Node:PKraftMeshSkipListNode; ModelViewMatrix:TKraftMatrix4x4; begin glMatrixMode(GL_MODELVIEW); ModelViewMatrix:=Matrix4x4TermMul(Shape.WorldTransform,CameraMatrix); glLoadMatrixf(pointer(@ModelViewMatrix)); if DrawMeshTreeDisplayList=0 then begin DrawMeshTreeDisplayList:=glGenLists(1); glNewList(DrawMeshTreeDisplayList,GL_COMPILE); for i:=0 to Shape.Mesh.CountSkipListNodes-1 do begin Node:=@Shape.Mesh.SkipListNodes[i]; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Min.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Max.Y,Node^.AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Min.Z); glVertex3f(Node^.AABB.Max.X,Node^.AABB.Min.Y,Node^.AABB.Max.Z); glEnd; end; glEndList; end; if DrawMeshTreeDisplayList<>0 then begin glCallList(DrawMeshTreeDisplayList); end; end; procedure DrawObjectAABB(const AABB:TKraftAABB); begin //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glBegin(GL_LINE_STRIP); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Min.Z); glVertex3f(AABB.Max.X,AABB.Min.Y,AABB.Min.Z); glVertex3f(AABB.Max.X,AABB.Max.Y,AABB.Min.Z); glVertex3f(AABB.Min.X,AABB.Max.Y,AABB.Min.Z); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Min.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Max.Z); glVertex3f(AABB.Max.X,AABB.Min.Y,AABB.Max.Z); glVertex3f(AABB.Max.X,AABB.Max.Y,AABB.Max.Z); glVertex3f(AABB.Min.X,AABB.Max.Y,AABB.Max.Z); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Min.Z); glVertex3f(AABB.Min.X,AABB.Min.Y,AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(AABB.Min.X,AABB.Max.Y,AABB.Min.Z); glVertex3f(AABB.Min.X,AABB.Max.Y,AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(AABB.Max.X,AABB.Max.Y,AABB.Min.Z); glVertex3f(AABB.Max.X,AABB.Max.Y,AABB.Max.Z); glEnd; glBegin(GL_LINE_STRIP); glVertex3f(AABB.Max.X,AABB.Min.Y,AABB.Min.Z); glVertex3f(AABB.Max.X,AABB.Min.Y,AABB.Max.Z); glEnd; end; procedure Draw; const GlobalAmbient: array[0..3] of GLFLOAT = (0.025,0.025,0.025,1); Licht0Ambient: array[0..3] of GLFLOAT = (0.2,0.2,0.2,1); Licht0Diffuse: array[0..3] of GLFLOAT = (0.8,0.8,0.8,1); Licht0Specular: array[0..3] of GLFLOAT = (0.025,0.025,0.025,1); LModellAmbient: array[0..3] of GLFLOAT = (0.025,0.025,0.025,1); MaterialDiffuse: array[0..3] of GLFLOAT = (0.8,0.8,0.8,1); MaterialSpecular: array[0..3] of GLFLOAT = (0.1,0.1,0.1,1); MaterialAmbient: array[0..3] of GLFLOAT = (0.2,0.2,0.2,1); MaterialShininess: array[0..3] of GLFLOAT = (0.1,0.1,0.1,1); var i:longint; m:TKraftMatrix4x4; v:TKraftVector4; vv:TKraftVector3; Shape:TKraftShape; s:ansistring; begin PixelProjectionMatrix.Values[0]:=2.0/VirtualCanvasWidth; PixelProjectionMatrix.Values[1]:=0.0; PixelProjectionMatrix.Values[2]:=0.0; PixelProjectionMatrix.Values[3]:=-(2.0/VirtualCanvasHeight); PixelProjectionMatrix.Values[4]:=-1.0; PixelProjectionMatrix.Values[5]:=1.0; /// PixelVectorProcessor.ProjectionMatrix:=PixelProjectionMatrix; glViewPort(0,0,ScreenWidth,ScreenHeight); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT or GL_STENCIL_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glViewPort(ViewPortX,ViewPortY,ViewPortWidth,ViewPortHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); (**) glColor4f(0.1725,0.3275,0.6275,1.0); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glDepthMask(GL_FALSE); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glBegin(GL_QUADS); glVertex3f(1.0,-1.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(-1.0,1.0,0.0); glVertex3f(-1.0,-1.0,0.0); glEnd; glDepthMask(GL_TRUE); glMatrixMode(GL_PROJECTION); m:=Matrix4x4Perspective(90.0,ScreenWidth/ScreenHeight,0.1,1024.0); glLoadMatrixf(pointer(@m)); //glFrustum(-0.01,0.01,-0.0075,0.0075,0.01,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glCullFace(GL_BACK); gldisable(GL_BLEND); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightModelfv(GL_LIGHT_MODEL_AMBIENT,@LModellAmbient); glLightModelfv(GL_LIGHT_MODEL_AMBIENT,@GlobalAmbient); m:=Matrix4x4LookAt(InterpolatedPosition,InterpolatedView,InterpolatedUpVector); v:=PKraftVector4(pointer(@Licht0Pos))^; Vector4MatrixMul(v,m); glLightfv(GL_LIGHT0,GL_POSITION,@v); glLightfv(GL_LIGHT0,GL_AMBIENT,@Licht0Ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,@Licht0Diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,@Licht0Specular); glPushMatrix; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,@MaterialDiffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,@MaterialSpecular); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,@MaterialAmbient); glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,@MaterialShininess); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LEQUAL); glCullFace(GL_BACK); glEnable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLoadMatrixf(@m); glEnable(GL_CULL_FACE); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glColor4f(1,1,1,1); ShapeFloorPlane.Draw(m); glColor4f(0.75,0.5,0.125,1); Shape:=RigidBodyMesh.ShapeFirst; while assigned(Shape) do begin Shape.Draw(m); Shape:=Shape.ShapeNext; end; //ShapeMesh.Draw(m); {ShapeFloorBoxes[0].Draw(m); glColor4f(0,1,1,1); ShapeFloorBoxes[1].Draw(m); glColor4f(0,0,1,1); ShapeFloorBoxes[2].Draw(m); glColor4f(1,0,0,1); ShapeFloorBoxes[3].Draw(m); glColor4f(0,1,0,1); ShapeFloorBoxes[4].Draw(m); glColor4f(1,1,0,1); ShapeFloorBoxes[5].Draw(m);} glColor4f(1,0,1,1); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glDisable(GL_LIGHTING); glColor4f(1,1,0,1); DrawSun(m); glEnable(GL_LIGHTING); for i:=low(ObjectShape) to high(ObjectShape) do begin if assigned(ObjectShape[i]) then begin if krbfAwake in ObjectShape[i].RigidBody.Flags then begin glColor4f(1.0,0.0,1.0,1.0); end else begin glColor4f(1.0,0.0,0.0,1.0); end; ObjectShape[i].Draw(m); end; end; for i:=low(RagdollShape) to high(RagdollShape) do begin if assigned(RagdollShape[i]) then begin if krbfAwake in RagdollShape[i].RigidBody.Flags then begin glColor4f(1.0,0.0,1.0,1.0); end else begin glColor4f(1.0,0.0,0.0,1.0); end; RagdollShape[i].Draw(m); end; end; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_LIGHTING); glColor4f(0.25,0.25,0.25,1.0); glLineWidth(4); glBegin(GL_LINE_STRIP); for i:=9 to 13 do begin if assigned(ObjectShape[i]) then begin vv:=Vector3TermMatrixMul(PKraftVector3(pointer(@ObjectShape[i].WorldTransform[3,0]))^,m); glVertex3fv(@vv); end; end; glEnd; glLineWidth(10); glBegin(GL_LINE_STRIP); for i:=16 to 17 do begin if assigned(ObjectShape[i]) then begin vv:=Vector3TermMatrixMul(PKraftVector3(pointer(@ObjectShape[i].WorldTransform[3,0]))^,m); glVertex3fv(@vv); end; end; glEnd; glBegin(GL_LINE_STRIP); if assigned(ObjectShape[14]) and assigned(ObjectShape[15]) then begin vv:=Vector3TermMatrixMul(PKraftVector3(pointer(@ObjectShape[14].WorldTransform[3,0]))^,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(HingeConstraints[0].GetAnchorA,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(PKraftVector3(pointer(@ObjectShape[15].WorldTransform[3,0]))^,m); glVertex3fv(@vv); end; glEnd; glPointSize(4); glLineWidth(4); glColor4f(1.0,1.0,0.125,1.0); for i:=low(RagdollConstraints) to high(RagdollConstraints) do begin if assigned(RagdollConstraints[i]) then begin glBegin(GL_POINTS); // glBegin(GL_LINE_STRIP); vv:=Vector3TermMatrixMul(RagdollConstraints[i].GetAnchorA,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(RagdollConstraints[i].GetAnchorB,m); glVertex3fv(@vv); glEnd; glBegin(GL_LINE_STRIP); vv:=Vector3TermMatrixMul(RagdollConstraints[i].GetAnchorA,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(RagdollConstraints[i].GetAnchorB,m); glVertex3fv(@vv); glEnd; end; end; if assigned(GrabRigidBody) then begin glLineWidth(10); glColor4f(1.0,1.0,0.125,1.0); glBegin(GL_LINE_STRIP); vv:=Vector3TermMatrixMul(GrabConstraint.GetWorldPoint,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(GrabConstraint.GetAnchor,m); glVertex3fv(@vv); glEnd; end; {glLineWidth(1); glColor4f(1.0,1.0,0.125,1.0); for i:=0 to 4 do begin glBegin(GL_LINE_STRIP); vv:=Vector3TermMatrixMul(WorldPlaneDistanceConstraints[i].GetWorldPoint,m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(WorldPlaneDistanceConstraints[i].GetAnchor,m); glVertex3fv(@vv); glEnd; end;{} glLineWidth(2); glColor4f(0.125,1.0,1.0,1.0); for i:=0 to 4 do begin glBegin(GL_LINE_STRIP); vv:=Vector3TermMatrixMul(pp[i,0],m); glVertex3fv(@vv); vv:=Vector3TermMatrixMul(pp[i,1],m); glVertex3fv(@vv); glEnd; end; glLineWidth(1); if DoDrawMoreDebugStuff then begin glColor4f(0.1,0.1,0.1,1.0); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glDisable(GL_LIGHTING); glEnable(GL_POLYGON_OFFSET_LINE); glPolygonOffset(-1,1); glLineWidth(2); //glCullFace(GL_FRONT); {} glColor4f(0.5*0.25,0.5*0.25,0.5*0.25,1.0); ShapeFloorPlane.Draw(m); glColor4f(0.75*0.25,0.5*0.25,0.125*0.25,1.0); Shape:=RigidBodyMesh.ShapeFirst; while assigned(Shape) do begin Shape.Draw(m); Shape:=Shape.ShapeNext; end; for i:=low(ObjectShape) to high(ObjectShape) do begin if assigned(ObjectShape[i]) then begin if krbfAwake in ObjectShape[i].RigidBody.Flags then begin glColor4f(0.5,0.0,0.5,1.0); end else begin glColor4f(0.5,0,0,1.0); end; ObjectShape[i].Draw(m); end; end; for i:=low(RagdollShape) to high(RagdollShape) do begin if assigned(RagdollShape[i]) then begin if krbfAwake in RagdollShape[i].RigidBody.Flags then begin glColor4f(0.5,0.0,0.5,1.0); end else begin glColor4f(0.5,0,0,1.0); end; RagdollShape[i].Draw(m); end; end; glEnable(GL_POLYGON_OFFSET_LINE); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLoadMatrixf(@m); glLineWidth(2); glPolygonOffset(-1,1); if Physics.StaticAABBTree.Root>=0 then begin glColor4f(0.5,0.5,1.0,0.75); DrawObjectTreeNode(Physics.StaticAABBTree,@Physics.StaticAABBTree.Nodes[Physics.StaticAABBTree.Root]); end; glPolygonOffset(-2,2); if Physics.SleepingAABBTree.Root>=0 then begin glColor4f(1.0,0.5,0.5,0.75); DrawObjectTreeNode(Physics.SleepingAABBTree,@Physics.SleepingAABBTree.Nodes[Physics.SleepingAABBTree.Root]); end; glPolygonOffset(-3,3); if Physics.DynamicAABBTree.Root>=0 then begin glColor4f(0.5,1.0,0.5,0.75); DrawObjectTreeNode(Physics.DynamicAABBTree,@Physics.DynamicAABBTree.Nodes[Physics.DynamicAABBTree.Root]); end; glPolygonOffset(-4,4); if Physics.KinematicAABBTree.Root>=0 then begin glColor4f(1.0,0.5,1.0,0.75); DrawObjectTreeNode(Physics.KinematicAABBTree,@Physics.KinematicAABBTree.Nodes[Physics.KinematicAABBTree.Root]); end; glPolygonOffset(-5,5); DrawObjectAABB(ShapeMesh.WorldAABB); glLineWidth(1); for i:=low(ObjectShape) to high(ObjectShape) do begin if assigned(ObjectShape[i]) then begin if krbfAwake in ObjectShape[i].RigidBody.Flags then begin glColor4f(1.0,1.0,1.0,0.75); DrawObjectAABB(ObjectShape[i].WorldAABB); { end else begin glColor4f(1.0,0.75,0.75,0.75);{} end; end; end; for i:=low(RagdollShape) to high(RagdollShape) do begin if assigned(RagdollShape[i]) then begin if krbfAwake in RagdollShape[i].RigidBody.Flags then begin glColor4f(1.0,1.0,1.0,0.75); DrawObjectAABB(RagdollShape[i].WorldAABB); { end else begin glColor4f(1.0,0.75,0.75,0.75);{} end; end; end; glPolygonOffset(-1,1); glLineWidth(1); glDisable(GL_POLYGON_OFFSET_LINE); glDisable(GL_LIGHTING); glLineWidth(0.5); glColor4f(1.0,1.0,1.0,0.125); DrawMeshTree(TKraftShapeMesh(ShapeMesh),m); glLineWidth(1); glDisable(GL_BLEND); end; if DoDrawDebugStuff then begin glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glEnable(GL_POLYGON_OFFSET_LINE); glEnable(GL_POLYGON_OFFSET_POINT); glPolygonOffset(-8,8); glPointSize(8); glLineWidth(4); Physics.ContactManager.DebugDraw(m); { glPointSize(16); glDisable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLoadMatrixf(@m); glColor4f(0.5,0.5,0.5,1.0); glBegin(GL_POINTS); for i:=low(ObjectShape) to high(ObjectShape) do begin vv:=Vector3TermMatrixMul(ObjectShape[i].LocalCentroid.Vector,ObjectShape[i].InterpolatedWorldTransform); glVertex3fv(@vv); end; glEnd; glColor4f(0.5,0.5,0.0,1.0); glBegin(GL_POINTS); for i:=low(ObjectShape) to high(ObjectShape) do begin vv:=Vector3TermMatrixMul(ObjectShape[i].LocalCenterOfMass,ObjectShape[i].InterpolatedWorldTransform); glVertex3fv(@vv); end; glEnd;} glDisable(GL_POLYGON_OFFSET_LINE); glDisable(GL_POLYGON_OFFSET_POINT); end; glMatrixMode(GL_PROJECTION); glClear(GL_STENCIL_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glFrustum(-0.01,0.01,-0.0075,0.0075,0.01,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glColor4f(1,1,1,1); glPointSize(2); glLineWidth(2); glTranslatef(0,0,-4); glBegin(GL_LINES); glVertex3f(-0.25,0,0); glVertex3f(0.25,0,0); glVertex3f(0,-0.25,0); glVertex3f(0,0.25,0); glEnd; glBegin(GL_LINE_STRIP); for i:=0 to 16 do begin glVertex3f(cos(i*pi/8)*0.125,sin(i*pi/8)*0.125,0); end; glEnd; glBegin(GL_LINE_STRIP); for i:=0 to 16 do begin glVertex3f(cos(i*pi/8)*0.06125*0.5,sin(i*pi/8)*0.06125*0.5,0); end; glEnd; glPointSize(1); glLineWidth(1); (***) glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glColor4f(1.0,1.0,1.0,1.0); ShowText(0,0,0,false,IntToStr(trunc(FPS))+'.'+IntToStr(trunc(FPS*10) mod 10)+' FPS'); {} str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.BroadPhaseTime)*1000.0:1:8,s); ShowText(0,FontHeight*1.0,0,false,s+' ms broad phase time (single-threaded)'); str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.MidPhaseTime)*1000.0:1:8,s); ShowText(0,FontHeight*2.0,0,false,s+' ms mid phase time (single-threaded)'); str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.NarrowPhaseTime)*1000.0:1:8,s); ShowText(0,FontHeight*3.0,0,false,s+' ms narrow phase time (multi-threaded)'); str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.SolverTime)*1000.0:1:8,s); ShowText(0,FontHeight*4.0,0,false,s+' ms discrete solver time (multi-threaded)'); str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.ContinuousTime)*1000.0:1:8,s); ShowText(0,FontHeight*5.0,0,false,s+' ms continuous collection detection and time of impact solver time (single-threaded)'); str(Physics.HighResolutionTimer.ToFloatSeconds(Physics.TotalTime)*1000.0:1:8,s); ShowText(0,FontHeight*6.0,0,false,s+' ms total physics step time'); ShowText(0,FontHeight*7.0,0,false,'Using '+IntToStr(Max(1,Physics.CountThreads))+' CPU threads'); {} ShowText(0,VirtualCanvasHeight-(FontHeight*4.0),0,false,'Left mouse button = Grab, Right mouse button = Drag'); ShowText(0,VirtualCanvasHeight-(FontHeight*3.0),0,false,'ESC = Quit, Space = Shoot impulse, Backspace = Darth-vader-like gravity control'); ShowText(0,VirtualCanvasHeight-(FontHeight*2.0),0,false,'1 = Normal gravity, 2 = No gravity, 3 = Inverted gravity'); ShowText(0,VirtualCanvasHeight-(FontHeight*1.0),0,false,'4 = Draw debug stuff toggle, 5 = Draw more debug stuff toggle'); if assigned(DragRigidBody) then begin ShowText(0,VirtualCanvasHeight-(FontHeight*6.0),0,false,'Dragging...'); end else if assigned(GrabRigidBody) then begin ShowText(0,VirtualCanvasHeight-(FontHeight*6.0),0,false,'Grabing...'); end; glEnable(GL_DEPTH_TEST); end; procedure Fire; var Point,Normal:TKraftVector3; s:TKraftShape; t:single; begin if Physics.RayCast(Position,Vector3Norm(Vector3Sub(View,Position)),1024.0,s,t,Point,Normal) then begin if assigned(s) and assigned(s.RigidBody) then begin s.RigidBody.ApplyImpulseAtPosition(Point,Vector3ScalarMul(Vector3Norm(Vector3Sub(View,Position)),s.RigidBody.Mass*10.0)); // s.RigidBody.ApplyImpulseAtPosition(Point,Vector3ScalarMul(Normal,-s.RigidBody.Mass*10.0)); // s.RigidBody.ApplyImpulseAtPosition(Point,Vector3ScalarMul(Vector3Neg(Normal),abs(Vector3Dot(Normal,Vector3Sub(View,Position))*s.RigidBody.Mass*10.0))); s.RigidBody.SetToAwake; end; end; end; {const ConvexHullPoints:array[0..4] of TKraftVector3=((x:-1.0;y:-1.0;z:-1.0), (x:1.0;y:-1.0;z:-1.0), (x:0.0;y:-1.0;z:1.0), (x:0.0;y:1.0;z:1.0), (x:1.0;y:0.0;z:0.0)); } {const ConvexHullPoints:array[0..5] of TKraftVector3=((x:-2.0;y:0.0;z:0.0), (x:2.0;y:0.0;z:0.0), (x:0.0;y:-2.0;z:0.0), (x:0.0;y:2.0;z:0.0), (x:0.0;y:0.0;z:-2.0), (x:0.0;y:0.0;z:2.0));{} const ConvexHullPoints:array[0..5] of TKraftVector3=((x:-2.0;y:1.0;z:1.0), (x:2.0;y:0.0;z:0.0), (x:1.0;y:-2.0;z:1.0), (x:0.0;y:2.0;z:0.0), (x:1.0;y:1.0;z:-2.0), (x:0.0;y:0.0;z:2.0));{} procedure Resize(NewWidth,NewHeight:longint); var Factor:int64; rw,rh:longint; begin ScreenWidth:=NewWidth; ScreenHeight:=NewHeight; begin Factor:=int64($100000000); rw:=VirtualCanvasWidth; rh:=VirtualCanvasHeight; while (max(rw,rh)>=128) and (((rw or rh)<>0) and (((rw or rh) and 1)=0)) do begin rw:=rw shr 1; rh:=rh shr 1; end; if ScreenWidthScreenWidth then begin Factor:=((ScreenWidth*int64($100000000))+(ViewPortWidth div 2)) div ViewPortWidth; end; end else begin ViewPortWidth:=ScreenWidth; ViewPortHeight:=((ScreenWidth*rh)+((rw+1) div 2)) div rw; if ViewPortHeight>ScreenHeight then begin Factor:=((ScreenHeight*int64($100000000))+(ViewPortHeight div 2)) div ViewPortHeight; end; end; if Factor=((BestWidth*90) div 100) then begin k:=((BestWidth*90) div 100); ScreenHeight:=(ScreenHeight*k) div ScreenWidth; ScreenWidth:=k; end; if ScreenHeight>=((BestHeight*90) div 100) then begin k:=((BestHeight*90) div 100); ScreenWidth:=(ScreenWidth*k) div ScreenHeight; ScreenHeight:=k; end; Resize(ScreenWidth,ScreenHeight); {$ifdef gles20} SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,0); {$else} SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,0); {$endif} SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,0); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); SDL_GL_SetSwapInterval(1); VideoFlags:=0; if paramstr(1)='f' then begin VideoFlags:=VideoFlags or SDL_WINDOW_FULLSCREEN; Fullscreen:=true; ScreenWidth:=1280; ScreenHeight:=720; end; for k:={$ifdef UseFBO}0{$else}{$ifdef gles20}2{$else}{$ifdef gles30}2{$else}4{$endif}{$endif}{$endif} downto 0 do begin if k=0 then begin SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,0); end else begin SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,1 shl k); end; SurfaceWindow:=SDL_CreateWindow(pansichar(Title),(BestWidth-ScreenWidth) div 2,(BestHeight-ScreenHeight) div 2,ScreenWidth,ScreenHeight,SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE or VideoFlags); if assigned(SurfaceWindow) then begin // SDL_EventState(SDL_DROPFILE,SDL_ENABLE); SurfaceContext:=SDL_GL_CreateContext(SurfaceWindow); if not assigned(SurfaceContext) then begin SDL_DestroyWindow(SurfaceWindow); SurfaceWindow:=nil; if k=0 then begin exit; end else begin continue; end; end; end else begin exit; end; if not {$ifdef gles20}true{$else}(Load_GL_version_2_0 and Load_GL_ARB_framebuffer_object){$endif} then begin if assigned(SurfaceContext) then begin SDL_GL_DeleteContext(SurfaceContext); SurfaceContext:=nil; end; SDL_DestroyWindow(SurfaceWindow); SurfaceWindow:=nil; if k=0 then begin exit; end else begin continue; end; end; break; end; SDL_GL_SetSwapInterval(1); SDL_ShowCursor(0); InitializeGraphics; Font:=TTRIFont.Create('font.fnt'); DragRigidBody:=nil; DragShape:=nil; GrabRigidBody:=nil; GrabShape:=nil; FontWidth:=22; FontHeight:=22; glHint(GL_POINT_SMOOTH_HINT,GL_FASTEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST); glHint(GL_LINE_SMOOTH_HINT,GL_FASTEST); glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST); DrawSunDisplayList:=0; DrawMeshTreeDisplayList:=0; Physics:=TKraft.Create(Max(-1,StrToIntDef(paramstr(1),-1))); Physics.SetFrequency(PhysicsHz); {Physics.VelocityIterations:=32; Physics.PositionIterations:=8;{} Physics.VelocityIterations:=4; Physics.PositionIterations:=1;{} //Physics.EnableFriction:=false; RigidBodyFloor:=TKraftRigidBody.Create(Physics); RigidBodyFloor.SetRigidBodyType(krbtSTATIC); ShapeFloorPlane:=TKraftShapePlane.Create(Physics,RigidBodyFloor,Plane(Vector3Norm(Vector3(0.0,1.0,0.0)),0.0)); {ShapeFloorBoxes[0]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(50.0,25.0,50.0)); ShapeFloorBoxes[0].LocalTransform:=Matrix4x4Translate(0.0,-25.0,0.0); ShapeFloorBoxes[0].Friction:=0.4; ShapeFloorBoxes[0].Restitution:=0.2; ShapeFloorBoxes[1]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(50.0,25.0,50.0)); ShapeFloorBoxes[1].LocalTransform:=Matrix4x4Translate(0.0,50.0,0.0); ShapeFloorBoxes[2]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(50.0,50.0,25.0)); ShapeFloorBoxes[2].LocalTransform:=Matrix4x4Translate(0.0,25.0,50.0); ShapeFloorBoxes[3]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(50.0,50.0,25.0)); ShapeFloorBoxes[3].LocalTransform:=Matrix4x4Translate(0.0,25.0,-50.0); ShapeFloorBoxes[4]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(25.0,50.0,50.0)); ShapeFloorBoxes[4].LocalTransform:=Matrix4x4Translate(50.0,25.0,0); ShapeFloorBoxes[5]:=TKraftShapeBox.Create(Physics,RigidBodyFloor,Vector3(25.0,50.0,50.0)); ShapeFloorBoxes[5].LocalTransform:=Matrix4x4Translate(-50.0,25.0,0);} RigidBodyFloor.Finish; RigidBodyFloor.SetWorldTransformation(Matrix4x4Translate(0.0,-8.0,0.0)); RigidBodyFloor.CollisionGroups:=[0]; RigidBodyMesh:=TKraftRigidBody.Create(Physics); RigidBodyMesh.SetRigidBodyType(krbtSTATIC); Mesh:=TKraftMesh.Create(Physics); Mesh.Load(@sandboxData,sandboxSize); Mesh.Scale(0.1); Mesh.Finish; ShapeMesh:=TKraftShapeMesh.Create(Physics,RigidBodyMesh,Mesh); ShapeMesh.Friction:=0.7; ShapeMesh.Restitution:=0.4; ShapeMesh.Density:=1.0; ShapeMesh.Finish; RigidBodyMesh.Finish; RigidBodyMesh.SetWorldTransformation(Matrix4x4Translate(0.0,-4.0,0.0)); RigidBodyMesh.CollisionGroups:=[0]; (**) ObjectRigidBody[0]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[0].SetRigidBodyType(krbtDYNAMIC); ObjectShape[0]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[0],3.0); //ObjectShape[0]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[0],5.0); //ObjectShape:=TKraftShapeBox.Create(Physics,ObjectRigidBody,Vector3(5.0,5.0,5.0)); ObjectShape[0].Friction:=0.4; ObjectShape[0].Restitution:=0.2; ObjectShape[0].Density:=1.0; //ObjectRigidBody.ForcedMass:=1.0; ObjectRigidBody[0].Finish; ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(10.0,5.0,5.0)); ObjectRigidBody[0].AngularVelocity.x:=0.0; ObjectRigidBody[0].SetToAwake; ObjectRigidBody[1]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[1].SetRigidBodyType(krbtDYNAMIC); // ObjectShape[2]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[1],1.0); ObjectShape[1]:=TKraftShapeBox.Create(Physics,ObjectRigidBody[1],Vector3(1.0,1.0,1.0)); ObjectShape[1].Friction:=0.4; ObjectShape[1].Restitution:=0.5; ObjectShape[1].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[1].Finish; ObjectRigidBody[1].SetWorldTransformation(Matrix4x4Translate(4.0,4.0,0.0)); // ObjectRigidBody[1].SetWorldTransformation(Matrix4x4Translate(2.0,2.0,-12.0)); ObjectRigidBody[1].SetToAwake; ObjectRigidBody[1].CollisionGroups:=[2]; {}ObjectRigidBody[2]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[2].SetRigidBodyType(krbtDYNAMIC); ObjectShape[2]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[2],1.0); //ObjectShape[2]:=TKraftShapeBox.Create(Physics,ObjectRigidBody[2],Vector3(1.0,1.0,1.0)); ObjectShape[2].Friction:=0.4; ObjectShape[2].Restitution:=0.2; ObjectShape[2].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[2].Finish; ObjectRigidBody[2].SetWorldTransformation(Matrix4x4Translate(-2.0,2.0,-12.0)); // ObjectRigidBody[2].SetWorldTransformation(Matrix4x4Translate(-2.0,16.0,0.0)); ObjectRigidBody[2].SetToAwake;{} {}ObjectRigidBody[3]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[3].SetRigidBodyType(krbtDYNAMIC); ObjectShape[3]:=TKraftShapeCapsule.Create(Physics,ObjectRigidBody[3],1.0,4.0); // Cylinder ObjectShape[3].Friction:=0.4; ObjectShape[3].Restitution:=0.2; ObjectShape[3].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[3].Finish; ObjectRigidBody[3].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-2.0,11.0,-20.0))); //ObjectRigidBody[3].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-5.0,4.0,5.0))); ObjectRigidBody[3].SetToAwake;{} {}ObjectRigidBody[4]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[4].SetRigidBodyType(krbtDYNAMIC); ObjectShape[4]:=TKraftShapeCapsule.Create(Physics,ObjectRigidBody[4],1.0,4.0); ObjectShape[4].Friction:=0.4; ObjectShape[4].Restitution:=0.2; ObjectShape[4].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[4].Finish; ObjectRigidBody[4].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.5),Matrix4x4Translate(-3.0,1.0,5.0))); ObjectRigidBody[4].SetToAwake;{} {}ObjectRigidBody[5]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[5].SetRigidBodyType(krbtDYNAMIC); ObjectShape[5]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[5],1.0); //ObjectShape[5]:=TKraftShapeTriangle.Create(Physics,ObjectRigidBody[5],Vector3(15.0,0.0,-8.0),Vector3(-15.0,0.0,-8.0),Vector3(-15.0,15.0,-8.0)); ObjectShape[5].Friction:=0.4; ObjectShape[5].Restitution:=0.7; ObjectShape[5].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[5].Finish; // ObjectRigidBody[5].SetWorldTransformation(Matrix4x4Translate(-37.3,24.0,-36.0)); // ObjectRigidBody[5].SetWorldTransformation(Matrix4x4Translate(-35.0,30.0,-26.0)); ObjectRigidBody[5].SetWorldTransformation(Matrix4x4Translate(-35.0,30.0,-25.0)); ObjectRigidBody[5].SetToAwake;{} {}ObjectRigidBody[6]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[6].SetRigidBodyType(krbtDYNAMIC); ObjectShape[6]:=TKraftShapeCapsule.Create(Physics,ObjectRigidBody[6],1.0,4.0); ObjectShape[6].Friction:=0.4; ObjectShape[6].Restitution:=0.2; ObjectShape[6].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[6].Finish; ObjectRigidBody[6].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.5),Matrix4x4RotateY(pi*0.5)),Matrix4x4Translate(-10.0,1.0,-4.5))); ObjectRigidBody[6].SetToAwake;{} {}ObjectRigidBody[7]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[7].SetRigidBodyType(krbtDYNAMIC); ObjectShape[7]:=TKraftShapeCapsule.Create(Physics,ObjectRigidBody[7],1.0,4.0); //ObjectShape[7]:=TKraftShapeCone.Create(Physics,ObjectRigidBody[7],1.0,4.0); ObjectShape[7].Friction:=0.4; ObjectShape[7].Restitution:=0.2; ObjectShape[7].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[7].Finish; ObjectRigidBody[7].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,1.0,5.0))); // ObjectRigidBody[7].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.5),Matrix4x4Translate(-8.0,1.0,5.0))); ObjectRigidBody[7].SetToAwake;{} {}ObjectRigidBody[8]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[8].SetRigidBodyType(krbtDYNAMIC); // ObjectShape[8]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[8],1.0); ConvexHull:=TKraftConvexHull.Create(Physics); ConvexHull.Load(pointer(@ConvexHullPoints),length(ConvexHullPoints)); ConvexHull.Build; ConvexHull.Finish; // Dump1(ConvexHull); {DumpFile(@ConvexHull.Vertices[0],length(ConvexHull.Vertices)*sizeof(ConvexHull.Vertices[0]),'hbv'); DumpFaces(ConvexHull.Faces,'hbf'); DumpFile(@ConvexHull.Edges[0],length(ConvexHull.Edges)*sizeof(ConvexHull.Edges[0]),'hbe'); DumpFile(@ConvexHull.AABB,sizeof(ConvexHull.AABB),'hb_aabb'); DumpFile(@ConvexHull.MassData,sizeof(ConvexHull.MassData),'hb_massdata'); {} // // ObjectShape[8]:=TKraftShapeConvexHull.Create(Physics,ObjectRigidBody[8],ConvexHull);{ ObjectShape[8]:=TKraftShapeConvexHull.Create(Physics,ObjectRigidBody[8],pointer(@ConvexHullPoints),length(ConvexHullPoints));{} ObjectShape[8].Friction:=0.4; ObjectShape[8].Restitution:=0.2; ObjectShape[8].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[8].Finish; {DumpFile(@ObjectShape[8].Vertices[0],length(ObjectShape[8].Vertices)*sizeof(ObjectShape[8].Vertices[0]),'hav'); DumpFaces(ObjectShape[8].Faces,'haf'); DumpFile(@ObjectShape[8].Edges[0],length(ObjectShape[8].Edges)*sizeof(ObjectShape[8].Edges[0]),'hae');{} //Dump2(ObjectShape[8]); // ObjectRigidBody[8].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.125),Matrix4x4Translate(0.0,3.0,0.0))); ObjectRigidBody[8].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.125),Matrix4x4Translate(5.0,-1.5,5.0))); //ObjectRigidBody[8].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.125),Matrix4x4Translate(0.0,1.0,8.0))); ObjectRigidBody[8].SetToAwake;{} ObjectRigidBody[9]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[9].SetRigidBodyType(krbtSTATIC); ObjectShape[9]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[9],0.1); ObjectRigidBody[9].Finish; ObjectRigidBody[9].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,8.0,15.0))); ObjectRigidBody[9].SetToAwake; ObjectRigidBody[10]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[10].SetRigidBodyType(krbtDYNAMIC); ObjectShape[10]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[10],1.0); ObjectRigidBody[10].Finish; ObjectRigidBody[10].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,6.5,15.0))); ObjectRigidBody[10].SetToAwake; ObjectRigidBody[11]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[11].SetRigidBodyType(krbtDYNAMIC); ObjectShape[11]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[11],1.0); ObjectRigidBody[11].Finish; ObjectRigidBody[11].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,5.0,15.0))); ObjectRigidBody[11].SetToAwake; ObjectRigidBody[12]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[12].SetRigidBodyType(krbtDYNAMIC); ObjectShape[12]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[12],1.0); ObjectRigidBody[12].Finish; ObjectRigidBody[12].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,3.5,15.0))); ObjectRigidBody[12].SetToAwake; ObjectRigidBody[13]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[13].SetRigidBodyType(krbtDYNAMIC); ObjectShape[13]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[13],1.0); ObjectRigidBody[13].Finish; ObjectRigidBody[13].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-8.0,1.0,15.0))); ObjectRigidBody[13].SetToAwake; ObjectRigidBody[14]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[14].SetRigidBodyType(krbtSTATIC); ObjectShape[14]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[14],1.0); ObjectRigidBody[14].Finish; ObjectRigidBody[14].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-12.0,6.0,15.0))); ObjectRigidBody[14].SetToAwake; ObjectRigidBody[15]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[15].SetRigidBodyType(krbtDYNAMIC); ObjectShape[15]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[15],1.0); ObjectRigidBody[15].Finish; ObjectRigidBody[15].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-12.0,3.0,15.0))); ObjectRigidBody[15].SetToAwake; ObjectRigidBody[16]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[16].SetRigidBodyType(krbtSTATIC); ObjectShape[16]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[16],1.0); ObjectRigidBody[16].Finish; ObjectRigidBody[16].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-16.0,6.0,15.0))); ObjectRigidBody[16].SetToAwake; ObjectRigidBody[17]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[17].SetRigidBodyType(krbtDYNAMIC); ObjectShape[17]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[17],1.0); ObjectRigidBody[17].Finish; ObjectRigidBody[17].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(-16.0,6.0,12.0))); ObjectRigidBody[17].SetToAwake; {Constraints[0]:=TKraftConstraintJointDistance.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],Vector3(-8.0,7.0,15.0),Vector3(-8.0,7.0,15.0),4.0,0.5); Constraints[1]:=TKraftConstraintJointDistance.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],Vector3(-8.0,5.0,15.0),Vector3(-8.0,5.0,15.0),4.0,0.5); {} {Constraints[0]:=TKraftConstraintJointDistance.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],Vector3(0.0,0.01,0),Vector3(0.0,-0.01,0.0),4.0,0.5); Constraints[1]:=TKraftConstraintJointDistance.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],Vector3(0.0,0.01,0),Vector3(0.0,-0.01,0.0),4.0,0.5); {} {WorldPlaneDistanceConstraints[0]:=TKraftConstraintJointWorldPlaneDistance.Create(Physics,ObjectRigidBody[1],Vector3(0.0,-1.0,0.0),Plane(Vector3(0.0,1.0,0.0),-1.0),false,4.0,kclbLimitMinimumDistance,1.0,0.5); WorldPlaneDistanceConstraints[1]:=TKraftConstraintJointWorldPlaneDistance.Create(Physics,ObjectRigidBody[1],Vector3(-0.65,-1.0,-0.65),Plane(Vector3(0.0,1.0,0.0),-1.0),false,4.0,kclbLimitMinimumDistance,1.0,0.5); WorldPlaneDistanceConstraints[2]:=TKraftConstraintJointWorldPlaneDistance.Create(Physics,ObjectRigidBody[1],Vector3(0.65,-1.0,-0.65),Plane(Vector3(0.0,1.0,0.0),-1.0),false,4.0,kclbLimitMinimumDistance,1.0,0.5); WorldPlaneDistanceConstraints[3]:=TKraftConstraintJointWorldPlaneDistance.Create(Physics,ObjectRigidBody[1],Vector3(-0.65,-1.0,0.65),Plane(Vector3(0.0,1.0,0.0),-1.0),false,4.0,kclbLimitMinimumDistance,1.0,0.5); WorldPlaneDistanceConstraints[4]:=TKraftConstraintJointWorldPlaneDistance.Create(Physics,ObjectRigidBody[1],Vector3(0.65,-1.0,0.65),Plane(Vector3(0.0,1.0,0.0),-1.0),false,4.0,kclbLimitMinimumDistance,1.0,0.5);{} Exclude(ObjectRigidBody[1].Flags,krbfAllowSleep); {} RopeConstraints[0]:=TKraftConstraintJointRope.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],Vector3(0.0,-0.55,0),Vector3(0.0,0.55,0.0),1.0,true); RopeConstraints[1]:=TKraftConstraintJointRope.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],Vector3(0.0,-0.55,0),Vector3(0.0,0.55,0.0),1.0,true); RopeConstraints[2]:=TKraftConstraintJointRope.Create(Physics,ObjectRigidBody[11],ObjectRigidBody[12],Vector3(0.0,-0.55,0),Vector3(0.0,0.55,0.0),1.0,true); RopeConstraints[3]:=TKraftConstraintJointRope.Create(Physics,ObjectRigidBody[12],ObjectRigidBody[13],Vector3(0.0,-0.55,0),Vector3(0.0,0.55,0.0),1.0,true); //Constraints[3]:=TKraftConstraintJointFixed.Create(Physics,ObjectRigidBody[12],ObjectRigidBody[13],Vector3(-8.0,3.0,15.0)); {} //HingeConstraints[0]:=TKraftConstraintJointBallSocket.Create(Physics,ObjectRigidBody[14],ObjectRigidBody[15],Vector3(-12.0,4.5,18.0)); //HingeConstraints[0]:=TKraftConstraintJointFixed.Create(Physics,ObjectRigidBody[14],ObjectRigidBody[15],Vector3(-12.0,4.5,18.0)); HingeConstraints[0]:=TKraftConstraintJointHinge.Create(Physics,ObjectRigidBody[14],ObjectRigidBody[15],Vector3(-12.0,4.5,15.0),Vector3Norm(Vector3(1.0,0.0,0.0)),false,false,-1.0,1.0,0.0,0.0,true); SliderConstraints[0]:=TKraftConstraintJointSlider.Create(Physics,ObjectRigidBody[16],ObjectRigidBody[17],Vector3(-16.0,6.0,13.5),Vector3Norm(Vector3(0.0,0.0,1.0)),true,false,-0.5,0.5,0.0,0.0,true); { Constraints[0]:=TKraftConstraintJointVelocityBasedDistance.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],2.0); Constraints[1]:=TKraftConstraintJointVelocityBasedDistance.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],2.0); {} { Constraints[0]:=TKraftConstraintJointVelocityBasedBall.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],Vector3(0.0,-1.01,0.0),Vector3(0.0,1.01,0.0)); Constraints[1]:=TKraftConstraintJointVelocityBasedBall.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],Vector3(0.0,-1.01,0.0),Vector3(0.0,1.01,0.0)); //Constraints[0]:=TKraftConstraintJointVelocityBasedDistance.Create(Physics,ObjectRigidBody[9],ObjectRigidBody[10],4.0); //Constraints[1]:=TKraftConstraintJointVelocityBasedDistance.Create(Physics,ObjectRigidBody[10],ObjectRigidBody[11],4.0); begin end; (**) {ObjectRigidBody[0]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[0].SetRigidBodyType(krbtDYNAMIC); ObjectShape[0]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[0],1.0); //ObjectShape[0]:=TKraftShapeTriangle.Create(Physics,ObjectRigidBody[5],Vector3(15.0,0.0,-8.0),Vector3(-15.0,0.0,-8.0),Vector3(-15.0,15.0,-8.0)); ObjectShape[0].Friction:=0.4; ObjectShape[0].Restitution:=0.2; ObjectShape[0].Density:=1.0; //ObjectRigidBody[0].ForcedMass:=1; ObjectRigidBody[0].Finish; // ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(-37.3,24.0,-36.0)); ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(-35.0,30.0,-25.0)); ObjectRigidBody[0].SetToAwake;{} {ObjectRigidBody[0]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[0].SetRigidBodyType(krbtDYNAMIC); ObjectShape[0]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[0],1.0); //ObjectShape[0]:=TKraftShapeTriangle.Create(Physics,ObjectRigidBody[0],Vector3(15.0,0.0,-8.0),Vector3(-15.0,0.0,-8.0),Vector3(-15.0,15.0,-8.0)); ObjectShape[0].Friction:=0.4; ObjectShape[0].Restitution:=0.2; ObjectShape[0].Density:=1.0; //ObjectRigidBody[1].ForcedMass:=1; ObjectRigidBody[0].Finish; // ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(-37.3,24.0,-36.0)); // ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(-35.0,30.0,-26.0)); ObjectRigidBody[0].SetWorldTransformation(Matrix4x4Translate(-35.0,30.0,-25.0)); ObjectRigidBody[0].SetToAwake;{} {ObjectRigidBody[0]:=TKraftRigidBody.Create(Physics); ObjectRigidBody[0].SetRigidBodyType(krbtDYNAMIC); // ObjectShape[8]:=TKraftShapeSphere.Create(Physics,ObjectRigidBody[8],1.0); ConvexHull:=TKraftConvexHull.Create(Physics); ConvexHull.Load(pointer(@ConvexHullPoints),length(ConvexHullPoints)); ConvexHull.Build; ConvexHull.Finish; ObjectShape[0]:=TKraftShapeConvexHull.Create(Physics,ObjectRigidBody[0],ConvexHull); ObjectShape[0].Friction:=0.4; ObjectShape[0].Restitution:=0.2; ObjectShape[0].Density:=1.0; ObjectRigidBody[0].Finish; ObjectRigidBody[0].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.125),Matrix4x4Translate(5.0,-1.5,5.0))); ObjectRigidBody[0].SetToAwake;{} (*) begin RagdollOffset:=Matrix4x4Translate(BODY_BASE_X,BODY_BASE_Y,BODY_BASE_Z); RagdollRigidBody[BODYPART_PELVIS]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_PELVIS].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_PELVIS]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_PELVIS],ScaleRagDoll*0.15,ScaleRagDoll*0.20); RagdollRigidBody[BODYPART_PELVIS].Finish; RagdollRigidBody[BODYPART_PELVIS].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(0.0,ScaleRagDoll*1.0,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_PELVIS].SetToAwake; Exclude(RagdollRigidBody[BODYPART_PELVIS].Flags,krbfContinuous); RagdollRigidBody[BODYPART_SPINE]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_SPINE].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_SPINE]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_SPINE],ScaleRagDoll*0.15,ScaleRagDoll*0.28); RagdollRigidBody[BODYPART_SPINE].Finish; RagdollRigidBody[BODYPART_SPINE].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(0.0,ScaleRagDoll*1.2,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_SPINE].SetToAwake; Exclude(RagdollRigidBody[BODYPART_SPINE].Flags,krbfContinuous); RagdollRigidBody[BODYPART_HEAD]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_HEAD].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_HEAD]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_HEAD],ScaleRagDoll*0.10,ScaleRagDoll*0.05); RagdollRigidBody[BODYPART_HEAD].Finish; RagdollRigidBody[BODYPART_HEAD].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(0.0,ScaleRagDoll*1.6,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_HEAD].SetToAwake; Exclude(RagdollRigidBody[BODYPART_HEAD].Flags,krbfContinuous); RagdollRigidBody[BODYPART_LEFT_UPPER_LEG]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_LEFT_UPPER_LEG].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_LEFT_UPPER_LEG]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_LEFT_UPPER_LEG],ScaleRagDoll*0.07,ScaleRagDoll*0.45); RagdollRigidBody[BODYPART_LEFT_UPPER_LEG].Finish; RagdollRigidBody[BODYPART_LEFT_UPPER_LEG].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(ScaleRagDoll*(-0.18),ScaleRagDoll*0.65,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_LEFT_UPPER_LEG].SetToAwake; Exclude(RagdollRigidBody[BODYPART_LEFT_UPPER_LEG].Flags,krbfContinuous); RagdollRigidBody[BODYPART_LEFT_LOWER_LEG]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_LEFT_LOWER_LEG].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_LEFT_LOWER_LEG]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_LEFT_LOWER_LEG],ScaleRagDoll*0.05,ScaleRagDoll*0.37); RagdollRigidBody[BODYPART_LEFT_LOWER_LEG].Finish; RagdollRigidBody[BODYPART_LEFT_LOWER_LEG].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(ScaleRagDoll*(-0.18),ScaleRagDoll*(-0.2),0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_LEFT_LOWER_LEG].SetToAwake; Exclude(RagdollRigidBody[BODYPART_LEFT_LOWER_LEG].Flags,krbfContinuous); RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_RIGHT_UPPER_LEG]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG],ScaleRagDoll*0.07,ScaleRagDoll*0.45); RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG].Finish; RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(ScaleRagDoll*(0.18),ScaleRagDoll*0.65,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG].SetToAwake; Exclude(RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG].Flags,krbfContinuous); RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_RIGHT_LOWER_LEG]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG],ScaleRagDoll*0.05,ScaleRagDoll*0.37); RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG].Finish; RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateX(pi*0.0),Matrix4x4Translate(ScaleRagDoll*(0.18),ScaleRagDoll*(-0.2),0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG].SetToAwake; Exclude(RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG].Flags,krbfContinuous); RagdollRigidBody[BODYPART_LEFT_UPPER_ARM]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_LEFT_UPPER_ARM].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_LEFT_UPPER_ARM]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_LEFT_UPPER_ARM],ScaleRagDoll*0.05,ScaleRagDoll*0.33); RagdollRigidBody[BODYPART_LEFT_UPPER_ARM].Finish; RagdollRigidBody[BODYPART_LEFT_UPPER_ARM].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateZ(pi*0.5),Matrix4x4Translate(ScaleRagDoll*(-0.35),ScaleRagDoll*1.45,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_LEFT_UPPER_ARM].SetToAwake; Exclude(RagdollRigidBody[BODYPART_LEFT_UPPER_ARM].Flags,krbfContinuous); RagdollRigidBody[BODYPART_LEFT_LOWER_ARM]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_LEFT_LOWER_ARM].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_LEFT_LOWER_ARM]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_LEFT_LOWER_ARM],ScaleRagDoll*0.04,ScaleRagDoll*0.25); RagdollRigidBody[BODYPART_LEFT_LOWER_ARM].Finish; RagdollRigidBody[BODYPART_LEFT_LOWER_ARM].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateZ(pi*0.5),Matrix4x4Translate(ScaleRagDoll*(-0.7),ScaleRagDoll*1.45,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_LEFT_LOWER_ARM].SetToAwake; Exclude(RagdollRigidBody[BODYPART_LEFT_LOWER_ARM].Flags,krbfContinuous); RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_RIGHT_UPPER_ARM]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM],ScaleRagDoll*0.05,ScaleRagDoll*0.33); RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM].Finish; RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateZ(-(pi*0.5)),Matrix4x4Translate(ScaleRagDoll*(0.35),ScaleRagDoll*1.45,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM].SetToAwake; Exclude(RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM].Flags,krbfContinuous); RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM]:=TKraftRigidBody.Create(Physics); RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM].SetRigidBodyType(krbtDYNAMIC); RagdollShape[BODYPART_RIGHT_LOWER_ARM]:=TKraftShapeCapsule.Create(Physics,RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM],ScaleRagDoll*0.04,ScaleRagDoll*0.25); RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM].Finish; RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM].SetWorldTransformation(Matrix4x4TermMul(Matrix4x4TermMul(Matrix4x4RotateZ(-(pi*0.5)),Matrix4x4Translate(ScaleRagDoll*(0.7),ScaleRagDoll*1.45,0.0)),RagdollOffset)); RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM].SetToAwake; Exclude(RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM].Flags,krbfContinuous); /// ******* PELVIS ******** /// RagdollConstraints[0]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_PELVIS], RagdollRigidBody[BODYPART_SPINE], Vector3(0.0,0.15*ScaleRagdoll,0.0), Vector3(0.0,-(0.15*ScaleRagdoll),0.0)); /// ******* SPINE HEAD ******** /// RagdollConstraints[1]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_SPINE], RagdollRigidBody[BODYPART_HEAD], Vector3(0.0,0.30*ScaleRagdoll,0.0), Vector3(0.0,-(0.15*ScaleRagdoll),0.0), true); /// ******* LEFT HIP ******** /// RagdollConstraints[2]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_PELVIS], RagdollRigidBody[BODYPART_LEFT_UPPER_LEG], Vector3(-(0.18*ScaleRagdoll),-(0.10*ScaleRagdoll),0.0), Vector3(0.0*ScaleRagdoll,0.225*ScaleRagdoll,0.0)); /// ******* LEFT KNEE ******** /// RagdollConstraints[3]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_LEFT_UPPER_LEG], RagdollRigidBody[BODYPART_LEFT_LOWER_LEG], Vector3(0.0,-(0.225*ScaleRagdoll),0.0), Vector3(0.0*ScaleRagdoll,0.185*ScaleRagdoll,0.0)); /// ******* RIGHT HIP ******** /// RagdollConstraints[4]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_PELVIS], RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG], Vector3(0.18*ScaleRagdoll,-(0.10*ScaleRagdoll),0.0), Vector3(0.0*ScaleRagdoll,0.225*ScaleRagdoll,0.0)); /// ******* RIGHT KNEE ******** /// RagdollConstraints[5]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_RIGHT_UPPER_LEG], RagdollRigidBody[BODYPART_RIGHT_LOWER_LEG], Vector3(0.0,-(0.225*ScaleRagdoll),0.0), Vector3(0.0*ScaleRagdoll,0.185*ScaleRagdoll,0.0)); /// ******* LEFT SHOULDER ******** /// RagdollConstraints[6]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_SPINE], RagdollRigidBody[BODYPART_LEFT_UPPER_ARM], Vector3(-(0.2*ScaleRagdoll),0.15*ScaleRagdoll,0.0), Vector3(0.0*ScaleRagdoll,-(0.18*ScaleRagdoll),0.0)); /// ******* LEFT ELBOW ******** /// RagdollConstraints[7]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_LEFT_UPPER_ARM], RagdollRigidBody[BODYPART_LEFT_LOWER_ARM], Vector3(0.0,0.18*ScaleRagdoll,0.0), Vector3(0.0,-(0.14*ScaleRagdoll),0.0)); /// ******* RIGHT SHOULDER ******** /// RagdollConstraints[8]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_SPINE], RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM], Vector3(0.2*ScaleRagdoll,0.15*ScaleRagdoll,0.0), Vector3(0.0*ScaleRagdoll,-(0.18*ScaleRagdoll),0.0)); /// ******* RIGHT ELBOW ******** /// RagdollConstraints[9]:=TKraftConstraintJointBallSocket.Create(Physics, RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM], RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM], Vector3(0.0,0.18*ScaleRagdoll,0.0), Vector3(0.0,-(0.14*ScaleRagdoll),0.0)); { /// ******* RIGHT SHOULDER ******** /// RagdollConstraints[8]:=TKraftConstraintJointBallSocket.Create(Physics,RagdollRigidBody[BODYPART_SPINE],RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM],Vector3(0.25*ScaleRagdoll,0.15*ScaleRagdoll,0.0),Vector3(0.0,-(0.18*ScaleRagdoll),0.0)); /// ******* RIGHT ELBOW ******** /// RagdollConstraints[9]:=TKraftConstraintJointBallSocket.Create(Physics,RagdollRigidBody[BODYPART_RIGHT_UPPER_ARM],RagdollRigidBody[BODYPART_RIGHT_LOWER_ARM],Vector3(0.0,0.18*ScaleRagdoll,0.0),Vector3(0.0,-(0.25*ScaleRagdoll),0.0)); {} end; {}for i:=low(ObjectRigidBody) to high(ObjectRigidBody) do begin ObjectRigidBody[i]:=nil; end; for i:=low(ObjectShape) to high(ObjectShape) do begin ObjectShape[i]:=nil; end;{}(***) //Physics.StaticAABBTree.Rebalance(128); //Physics.MaximalLinearVelocity:=100.0; Physics.ContinuousMode:=kcmTimeOfImpactSubSteps; Physics.Gravity.y:=-20.0; Gravitation:=Physics.Gravity; OldGravitation:=Gravitation; ToUp:=Vector3YAxis; {Position:=vector3(2,-0.5,-18); View:=vector3(2,-0.5,-17);{} {Position:=vector3(0,10,18); View:=vector3(0,10,17);{} //View:=vector3(-2,4,10);{} View:=vector3(-2,4,10);{} Position:=Vector3Add(View,Vector3SafeNorm(Vector3(0.0,0.5,1.0))); {Position:=vector3(-4,18,-20); View:=vector3(-3,17,-20);{} {View:=PKraftVector3(pointer(@ObjectRigidBody[5].WorldTransform[3,0]))^; Position:=PKraftVector3(pointer(@ObjectRigidBody[5].WorldTransform[3,0]))^;{} {View:=PKraftVector3(pointer(@ObjectRigidBody[0].WorldTransform[3,0]))^; Position:=PKraftVector3(pointer(@ObjectRigidBody[0].WorldTransform[3,0]))^;{} {Position.x:=Position.x+1.0; Position.y:=Position.y+1.0;{} {Position.x:=Position.x+1.0;{} {View.y:=View.y-8.0; View.z:=View.z+14.0;{} {Position:=vector3(0,1,18); View:=vector3(0,1,17);{} UpVector:=vector3(0,1,0); // yy:=8; DoDrawDebugStuff:=false; DoDrawMoreDebugStuff:=false; StartTime:=HighResolutionTimerInstance.GetTime; //LastTime:=0; //MSG.wParam:=0; fillchar(KeyByteArray,sizeof(KeyByteArray),#0); LastKeyByteArray:=KeyByteArray; Now:=HighResolutionTimerInstance.GetTime-StartTime; FST:=Now; //FET:=FST; FST2:=FST; //FET2:=FET; Frames:=0; LastPosition:=Position; LastView:=View; LastUpVector:=UpVector; ti:=0.0; qcf:=0; Physics.StoreWorldTransforms; Physics.InterpolateWorldTransforms(0.0); SDL_SetRelativeMouseMode(1); FullScreen:=false; SDLRunning:=true; while SDLRunning do begin Now:=HighResolutionTimerInstance.GetTime-StartTime; inc(Frames); if abs(FST2-Now)>=HighResolutionTimerInstance.Frequency then begin FET2:=FST2; FST2:=Now; if (FST2-FET2)<>0 then begin FPS:=(Frames*HighResolutionTimerInstance.Frequency)/(FST2-FET2); end; Frames:=0; end; Now:=HighResolutionTimerInstance.GetTime-StartTime; TimeStep:=abs(Now-FST)/HighResolutionTimerInstance.Frequency; FST:=Now; if TimeStep>0.02 then begin TimeStep:=0.02; end; // MoveCameraByMouse; {GetCursorPos(MousePos); DX:=ScreenWidthHalf-MousePos.X; DY:=ScreenHeightHalf-MousePos.Y; SetCursorPos(ScreenWidthHalf,ScreenHeightHalf); Angle:=Angle+(DX*TimeStep*PI*0.1); Dir:=Vector3(0,0,1); Vector3RotateY(Dir,Angle); yy:=yy+(dy*0.1);{} // PhysicsRigidBodyAddImpulse(Object1RigidBody,Object1RigidBody.Pos,Vector3(-DX*100*TimeStep,0,-DY*100*TimeStep)); //writeln(ObjectRigidBody.Position.y:1:5,' ',ObjectRigidBody.ShapeFirst.WorldAABB.Min.y:1:5); First:=true; ti:=ti+TimeStep; while ti>=PhysicsTimeStep do begin ti:=ti-PhysicsTimeStep; // writeln(TConverter.ToString(ObjectShape[8])); /// Dump(ObjectShape[8]); ProcessDrag; ProcessGrab; qc1:=HighResolutionTimerInstance.GetTime; Physics.StoreWorldTransforms; HoverHeight:=9.0; HoverForce:=9.0; ObjectRigidBody[1].Force:=Vector3Origin; ObjectRigidBody[1].Torque:=Vector3origin; ObjectRigidBody[1].UpdateWorldInertiaTensor; (* sv0:=Vector3Neg(Vector3(ObjectRigidBody[1].WorldTransform[1,0],ObjectRigidBody[1].WorldTransform[1,1],ObjectRigidBody[1].WorldTransform[1,2])); sv1:=Vector3Origin; j:=0; for i:=1 to 4 do begin sv:=Vector3Add(WorldPlaneDistanceConstraints[i].GetAnchor,Vector3ScalarMul(sv0,0.0)); if Physics.RayCast(sv,sv0,1e+30,Shape,Time,Point,Normal,[0]) then begin pp[i,0]:=sv; pp[i,1]:=Point; WorldPlaneDistanceConstraints[i].SetWorldPlane(Plane(Normal,-Vector3Dot(Normal,Point))); WorldPlaneDistanceConstraints[i].SetWorldDistance(2.0); // Include(WorldPlaneDistanceConstraints[i].Flags,kcfActive); Exclude(WorldPlaneDistanceConstraints[i].Flags,kcfActive); { if (Time+0.0)0 then begin Vector3Normalize(sv1); // ToUp:=sv1; Include(ObjectRigidBody[1].Flags,krbfHasOwnGravity); ObjectRigidBody[1].Gravity:=Vector3ScalarMul(sv1,-9.81); end; ObjectRigidBody[1].Sweep.q:=QuaternionSlerp(ObjectRigidBody[1].Sweep.q,QuaternionMul(QuaternionFromToRotation(Vector3(ObjectRigidBody[1].WorldTransform[1,0],ObjectRigidBody[1].WorldTransform[1,1],ObjectRigidBody[1].WorldTransform[1,2]),ToUp),ObjectRigidBody[1].Sweep.q),0.125); ObjectRigidBody[1].Sweep.q0:=ObjectRigidBody[1].Sweep.q;{} ObjectRigidBody[1].SynchronizeProxies; Exclude(WorldPlaneDistanceConstraints[0].Flags,kcfActive); Exclude(WorldPlaneDistanceConstraints[1].Flags,kcfActive); Exclude(WorldPlaneDistanceConstraints[2].Flags,kcfActive); Exclude(WorldPlaneDistanceConstraints[3].Flags,kcfActive); Exclude(WorldPlaneDistanceConstraints[4].Flags,kcfActive);(**) Physics.Step(PhysicsTimeStep); qc0:=HighResolutionTimerInstance.GetTime; if First then begin qcf:=0; end; qcf:=max(qcf,qc0-qc1); LastPosition:=Position; LastView:=View; LastUpVector:=UpVector; // MoveCameraByMouse; SDLKeyByteArray:=pointer(SDL_GetKeyboardState(nil)); KeyByteArray:=SDLKeyByteArray^; if KeyByteArray[SDL_SCANCODE_BACKSPACE] and not LastKeyByteArray[SDL_SCANCODE_BACKSPACE] then begin Physics.Gravity:=Vector3ScalarMul(Vector3Norm(Vector3Add(Gravitation,Vector3ScalarMul(Vector3Norm(Vector3Sub(View,Position)),100.0))),9.81); RigidBody:=Physics.RigidBodyFirst; while assigned(RigidBody) do begin Exclude(RigidBody.Flags,krbfAllowSleep); RigidBody.SetToAwake; RigidBody:=RigidBody.RigidBodyNext; end; end else if LastKeyByteArray[SDL_SCANCODE_BACKSPACE] and not KeyByteArray[SDL_SCANCODE_BACKSPACE] then begin Physics.Gravity:=Gravitation; RigidBody:=Physics.RigidBodyFirst; while assigned(RigidBody) do begin if Vector3Length(Gravitation)>0.001 then begin Include(RigidBody.Flags,krbfAllowSleep); end else begin Exclude(RigidBody.Flags,krbfAllowSleep); end; RigidBody.SetToAwake; RigidBody:=RigidBody.RigidBodyNext; end; Exclude(ObjectRigidBody[1].Flags,krbfAllowSleep); end; if KeyByteArray[SDL_SCANCODE_SPACE] and not LastKeyByteArray[SDL_SCANCODE_SPACE] then begin Fire; end; if KeyByteArray[SDL_SCANCODE_1] and not LastKeyByteArray[SDL_SCANCODE_1] then begin Gravitation:=OldGravitation; Physics.Gravity:=Gravitation; RigidBody:=Physics.RigidBodyFirst; while assigned(RigidBody) do begin Include(RigidBody.Flags,krbfAllowSleep); RigidBody.SetToAwake; RigidBody:=RigidBody.RigidBodyNext; end; Exclude(ObjectRigidBody[1].Flags,krbfAllowSleep); end; if KeyByteArray[SDL_SCANCODE_2] and not LastKeyByteArray[SDL_SCANCODE_2] then begin Gravitation:=Vector3Origin; Physics.Gravity:=Gravitation; RigidBody:=Physics.RigidBodyFirst; while assigned(RigidBody) do begin Exclude(RigidBody.Flags,krbfAllowSleep); RigidBody.SetToAwake; RigidBody:=RigidBody.RigidBodyNext; end; end; if KeyByteArray[SDL_SCANCODE_3] and not LastKeyByteArray[SDL_SCANCODE_3] then begin Gravitation:=Vector3Neg(OldGravitation); Physics.Gravity:=Gravitation; RigidBody:=Physics.RigidBodyFirst; while assigned(RigidBody) do begin Include(RigidBody.Flags,krbfAllowSleep); RigidBody.SetToAwake; RigidBody:=RigidBody.RigidBodyNext; end; Exclude(ObjectRigidBody[1].Flags,krbfAllowSleep); end; if KeyByteArray[SDL_SCANCODE_4] and not LastKeyByteArray[SDL_SCANCODE_4] then begin DoDrawDebugStuff:=not DoDrawDebugStuff; end; if KeyByteArray[SDL_SCANCODE_5] and not LastKeyByteArray[SDL_SCANCODE_5] then begin DoDrawMoreDebugStuff:=not DoDrawMoreDebugStuff; end; if KeyByteArray[SDL_SCANCODE_UP] or KeyByteArray[SDL_SCANCODE_W] then begin MoveCamera(PhysicsTimeStep*10); end; if KeyByteArray[SDL_SCANCODE_DOWN] or KeyByteArray[SDL_SCANCODE_S] then begin MoveCamera(-PhysicsTimeStep*10); end; if KeyByteArray[SDL_SCANCODE_LEFT] or KeyByteArray[SDL_SCANCODE_A] then begin MoveCameraSidewards(PhysicsTimeStep*10); end; if KeyByteArray[SDL_SCANCODE_RIGHT] or KeyByteArray[SDL_SCANCODE_D] then begin MoveCameraSidewards(-PhysicsTimeStep*10); end; if KeyByteArray[SDL_SCANCODE_PAGEUP] or KeyByteArray[SDL_SCANCODE_R] then begin MoveCameraUpwards(PhysicsTimeStep*10); end; if KeyByteArray[SDL_SCANCODE_PAGEDOWN] or KeyByteArray[SDL_SCANCODE_F] then begin MoveCameraUpwards(-PhysicsTimeStep*10); end; LastKeyByteArray:=KeyByteArray; TestCamera; end; Physics.InterpolateWorldTransforms(ti/PhysicsTimeStep); InterpolatedPosition:=Vector3Lerp(LastPosition,Position,ti/PhysicsTimeStep); InterpolatedView:=Vector3Lerp(LastView,View,ti/PhysicsTimeStep); InterpolatedUpVector:=Vector3Lerp(LastUpVector,UpVector,ti/PhysicsTimeStep); while SDL_PollEvent(@Event)<>0 do begin case Event.type_ of SDL_QUITEV,SDL_APP_TERMINATING:begin SDLRunning:=false; break; end; SDL_APP_WILLENTERBACKGROUND:begin //SDL_PauseAudio(1); end; SDL_APP_DIDENTERFOREGROUND:begin //SDL_PauseAudio(0); end; SDL_RENDER_TARGETS_RESET,SDL_RENDER_DEVICE_RESET:begin end; SDL_KEYDOWN:begin case Event.key.keysym.sym of SDLK_ESCAPE:begin // BackKey; SDLRunning:=false; break; end; SDLK_RETURN:begin if (Event.key.keysym.modifier and ((KMOD_LALT or KMOD_RALT) or (KMOD_LMETA or KMOD_RMETA)))<>0 then begin FullScreen:=not FullScreen; if FullScreen then begin SDL_SetWindowFullscreen(SurfaceWindow,SDL_WINDOW_FULLSCREEN_DESKTOP); end else begin SDL_SetWindowFullscreen(SurfaceWindow,0); end; end; end; SDLK_F4:begin if (Event.key.keysym.modifier and ((KMOD_LALT or KMOD_RALT) or (KMOD_LMETA or KMOD_RMETA)))<>0 then begin SDLRunning:=false; break; end; end; end; end; SDL_KEYUP:begin end; SDL_WINDOWEVENT:begin case event.window.event of SDL_WINDOWEVENT_RESIZED:begin ScreenWidth:=event.window.Data1; ScreenHeight:=event.window.Data2; Resize(ScreenWidth,ScreenHeight); end; end; end; SDL_MOUSEMOTION:begin if (event.motion.xrel<>0) or (event.motion.yrel<>0) then begin rotateY:=-(event.motion.xrel/500); deltaY:=-(event.motion.yrel/1000); View.Y:=View.Y+(deltaY*5.0); IF (View.Y-Position.Y)>10 THEN View.Y:=Position.Y+10; IF (View.Y-Position.Y)<(-10) THEN View.Y:=Position.Y-10; RotateCamera(Position,0,-rotateY,0); end; end; SDL_MOUSEBUTTONDOWN:begin case event.button.button of SDL_BUTTON_LEFT:begin StartGrab; end; SDL_BUTTON_RIGHT:begin StartDrag; end; end; end; SDL_MOUSEBUTTONUP:begin case event.button.button of SDL_BUTTON_LEFT:begin StopGrab; end; SDL_BUTTON_RIGHT:begin StopDrag; end; end; end; end; end; { View:=PKraftVector3(pointer(@ObjectShape[5].InterpolatedWorldTransform[3,0]))^; Position:=PKraftVector3(pointer(@ObjectShape[5].InterpolatedWorldTransform[3,0]))^; Position.x:=Position.x+4.0; Position.y:=Position.y+2.0; InterpolatedPosition:=Position; InterpolatedView:=View;{} ProcessDrag; Draw; SDL_GL_SwapWindow(SurfaceWindow); //Physics.GetDistance(ObjectShape[1],ObjectShape[5]); end; Physics.Free; Font.Free; FinalizeGraphics; if DrawSunDisplayList>0 then begin glDeleteLists(DrawSunDisplayList,1); end; if DrawMeshTreeDisplayList>0 then begin glDeleteLists(DrawMeshTreeDisplayList,1); end; if assigned(SurfaceContext) then begin SDL_GL_DeleteContext(SurfaceContext); SurfaceContext:=nil; end; if assigned(SurfaceWindow) then begin SDL_DestroyWindow(SurfaceWindow); SurfaceWindow:=nil; end; HighResolutionTimerInstance.Free; SDL_Quit; end.