First beta version BlitzODE 0.5 wrapper.
http://blitz.pp.ru/file/BlitzODE_0_5_b.rar
80% from ALL ODE functions.
http://blitz.pp.ru/file/BlitzODE_0_5_b.rar
80% from ALL ODE functions.
Function ODE_dTriMeshCreate%(mesh%) tris_count% = 0 vert_count% = 0 For i = 1 To CountSurfaces(mesh) tris_count = tris_count + CountTriangles(GetSurface(mesh, i)) vert_count = vert_count + CountVertices(GetSurface(mesh, i)) Next DebugLog "Tris count: " + tris_count DebugLog "Vertices count: " + vert_count DebugLog "surface count: "+CountSurfaces(mesh) vertices_bank% = CreateBank(vert_count * 4 * 4) indices_bank% = CreateBank(tris_count * 3 * 4) offset% = 0 offset_tris% = 0 baseverts=0 For i = 1 To CountSurfaces(mesh) sf = GetSurface(mesh, i) For v = 0 To CountVertices(sf) - 1 PokeFloat vertices_bank, offset, VertexX(sf, v) offset = offset + 4 PokeFloat vertices_bank, offset, VertexY(sf, v) offset = offset + 4 PokeFloat vertices_bank, offset, VertexZ(sf, v) offset = offset + 8 Next For t = 0 To CountTriangles(sf) - 1 For j = 0 To 2 v = TriangleVertex(sf, t, j)+baseverts PokeInt indices_bank, offset_tris, v offset_tris = offset_tris + 4 Next Next baseverts=baseverts+CountVertices(sf) Next t_data = ODE_dGeomTriMeshDataCreate() ODE_dGeomTriMeshDataBuildSimple(t_data, vertices_bank, vert_count, indices_bank, tris_count * 3) Return ODE_dCreateTriMesh(space, t_data) End Function
Const VertexCount = 5 Const IndexCount = 12 Global size#[3] Dim Vertices#(5, 3) Global Indices%[12] Global vr%[5]
; ---------------------------- ; Global ODE space ; ---------------------------- Global ODE_Space ; -------------------------- ; ODE Object Type ; -------------------------- Type ODE_Object Field ObjectBody Field ObjectMesh Field ObjectGeom Field ObjectFake Field ObjectMass# Field ObjectRotX# Field ObjectRotY# Field ObjectRotZ# Field ObjectPx# Field ObjectPy# Field ObjectPz# Field ObjectScaleX# Field ObjectScaleY# Field ObjectScaleZ# End Type ; --------------------------- ; Setup easily world ; --------------------------- Function ODE_InitWorld(Gravity#=-1.5,Bounce#=0.05) ODE_Space = ODE_dWorldCreate() ODE_dWorldSetGravity(0, Gravity#, 0) ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM) ODE_dSetBOUNCE(Bounce#) ODE_dSetSOFT_ERP(0.4) ODE_dSetSOFT_CFM(0.8) End Function ; -------------------------- ; Setup easily cube ; -------------------------- Function ODE_CreateCube(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,ScaleX#,ScaleY#,ScaleZ#,Mass#=10,Red=255,Green=255,Blue=255) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=ScaleX# G\ObjectScaleY#=ScaleY# G\ObjectScaleZ#=ScaleZ# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#*2,G\ObjectScaleY#*2,G\ObjectScaleZ#*2, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreateCube() PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue End Function ; ----------------------------- ; Setup easily sphere ; ----------------------------- Function ODE_CreateSphere(Px#,Py#,Pz#,Scale#,Mass#=10,Red=255,Green=255,Blue=255) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Scale# G\ObjectScaleY#=Scale# G\ObjectScaleZ#=Scale# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateSphere(ODE_Space,G\ObjectScaleX#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) G\ObjectMesh = CreateSphere() PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue End Function ; ------------------------------- ; Setup easily cylinder ; ------------------------------- Function ODE_CreateCylinder(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Radius#,Height#,Mass#=10,Red=255,Green=255,Blue=255) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Radius# G\ObjectScaleY#=Height# G\ObjectScaleZ#=Radius# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateCCylinder(ODE_Space,G\ObjectScaleX#, G\ObjectScaleY#*2,G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreatePivot() G\ObjectFake=CreateCylinder(8,1,G\ObjectMesh) RotateEntity G\ObjectFake, 90, 0, 0 PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectFake,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectFake, Red,Green,Blue RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# End Function ; --------------------- ; Update physic ; --------------------- Function ODE_UpdatePhysic(Time#=0.1) For G.ODE_Object = Each ODE_Object PositionEntity G\ObjectMesh, ODE_dGeomGetPositionX(G\ObjectGeom), ODE_dGeomGetPositionY(G\ObjectGeom), ODE_dGeomGetPositionZ(G\ObjectGeom) RotateEntity G\ObjectMesh, ODE_dGeomGetPitch(G\ObjectGeom), ODE_dGeomGetYaw(G\ObjectGeom), ODE_dGeomGetRoll(G\ObjectGeom), 1 Next End Function This is a test code --------------------------- ; ------------------------ ; Freelook cam var ; ------------------------ Global Mouse_X_Speed# Global Mouse_Y_Speed# Global Camera_VelX# Global Camera_VelZ# Global Camera_Pitch Global Camera_Yaw Include "BlitzODE.bb" Include "Inc_ODEStuff.bb" ;------ Initialize Graphics -------- Graphics3D 800, 600,16,2 SetBuffer BackBuffer() cam = CreateCamera() PositionEntity cam, 0, 10, -25 lh = CreateLight(2) PositionEntity lh,50,50,50 LightRange lh,100 LightColor lh,255,200,150 plane = CreatePlane() mirror = CreateMirror(plane) EntityColor plane, 0, 0, 150 EntityAlpha plane, .8 PositionEntity plane,0,-0.3,0 SeedRnd MilliSecs() ;--------- Initialize ODE ---------- ODE_InitWorld() ;------- Create siple cube --------- For i=5 To 40 Step 2 Mass#=Rnd(5,10) ODE_CreateCube(Rnd(-5,5),i,Rnd(-5,5),0,0,0,1,1,1,Mass#,Rnd(255),Rnd(255),Rnd(255)) ODE_CreateSphere(Rnd(-5,5),i,Rnd(-5,5),1,Mass#,Rnd(255),Rnd(255),Rnd(255)) ODE_CreateCylinder(Rnd(-5,5),i,Rnd(-5,5),45,50,65,1,3,Mass#,Rnd(255),Rnd(255),Rnd(255)) Next ;mesh=LoadMesh("002.b3d") ;ODE_dTriMeshCreate%(mesh) ;----------------------------------- ODE_UpdatePhysic() ;----------------------------------- gameFPS = 50.0 framePeriod = 1000 / gameFPS frameTime = MilliSecs () - framePeriod MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 While Not KeyHit(1) Repeat frameElapsed = MilliSecs () - frameTime Until frameElapsed frameTicks = frameElapsed / framePeriod frameTween# = Float(frameElapsed Mod framePeriod) / Float(framePeriod) For frameLimit = 1 To frameTicks If frameLimit = frameTicks Then CaptureWorld frameTime = frameTime + framePeriod ;------------ Update physics world ---------- ODE_dWorldQuickStep 0.1 ;-------------------------------------------- Next If KeyHit(57) For G.ODE_Object = Each ODE_Object ODE_dBodyEnable G\ObjectBody ODE_dBodyAddForce G\ObjectBody, 0, Rnd(20, 50), 0 ODE_dBodyAddTorque G\ObjectBody, Rnd(-30, 10), Rnd(-10, 10), Rnd(-10, 10) Next EndIf ;---- Set geometry position and rotation ---- ODE_UpdatePhysic() Proc_Freelook(Cam,1.05,0.01) ;-------------------------------------------- RenderWorld frameTween# Flip 0 Wend ODE_dCloseODE() End Function Proc_Freelook(CamEntity,Velocity#,Speed#) Mouse_X_Speed#=MouseXSpeed()*0.5 Mouse_Y_Speed#=MouseYSpeed()*0.5 MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 Camera_Pitch=Camera_Pitch+Mouse_Y_Speed# Camera_Yaw=Camera_Yaw+Mouse_X_Speed# RotateEntity CamEntity,Camera_Pitch,-Camera_Yaw,0 If KeyDown(203) Then Camera_VelX=Camera_VelX-Speed# ElseIf KeyDown(205) Camera_VelX=Camera_VelX+Speed# EndIf If KeyDown(208) Then Camera_VelZ=Camera_VelZ-Speed# ElseIf KeyDown(200) Camera_VelZ=Camera_VelZ+Speed# EndIf Camera_VelX=Camera_VelX/velocity# Camera_VelZ=Camera_VelZ/velocity# MoveEntity CamEntity,Camera_VelX,0,Camera_VelZ If KeyDown(200) Then MoveEntity CamEntity,0,0,Speed# EndIf If KeyDown(208) Then MoveEntity CamEntity,0,0,-Speed# EndIf If KeyDown(205) Then MoveEntity CamEntity,Speed#,0,0 EndIf If KeyDown(203) Then MoveEntity CamEntity,-Speed#,0,0 EndIf End Function
ax# = ODE_dBodyGetAngularVelX(cbody[0]) * .7 ay# = ODE_dBodyGetAngularVelY(cbody[0]) * .8 az# = ODE_dBodyGetAngularVelZ(cbody[0]) * .7 ODE_dBodySetAngularVel(cbody[0], ax#, ay#, az#)
; -------------------------- ; Setup easily cube ; -------------------------- Function ODE_CreateCubeMesh(Mesh$,Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Mass#=10,Red=255,Green=255,Blue=255) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = LoadMesh(Mesh$) G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# EntityColor G\ObjectMesh, Red,Green,Blue G\ObjectScaleX#=ODE_EntityScale#(G\ObjectMesh,0) G\ObjectScaleY#=ODE_EntityScale#(G\ObjectMesh,1) G\ObjectScaleZ#=ODE_EntityScale#(G\ObjectMesh,2) G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) End Function Function ODE_EntityScale#(entity,axis) x#=GetMatElement(entity,axis,0) y#=GetMatElement(entity,axis,1) z#=GetMatElement(entity,axis,2) Return Sqr(x*x+y*y+z*z) End Function
;links entities to rigid bodies Function ODELink(ent,geo) o.odelink=New odelink o\ent=ent o\geo=geo End Function ;updates entity transforms to match rigid bodies Function ODEUpdateLinks() For o.odelink=Each odelink PositionEntity o\ent, ODE_dGeomGetPositionX(o\geo), ODE_dGeomGetPositionY(o\geo), ODE_dGeomGetPositionZ(o\geo) RotateEntity o\ent, ODE_dGeomGetPitch(o\geo), ODE_dGeomGetYaw(o\geo), ODE_dGeomGetRoll(o\geo),1 Next End Function ;creates a rigid body cube from a source entity's dimensions and link them together Function ODECube(ent) ;get transform x#=EntityX(ent,1) y#=EntityY(ent,1) z#=EntityZ(ent,1) sx#=MeshWidth(ent) sy#=MeshHeight(ent) sz#=MeshDepth(ent) ;create ode rigid body o.odebody = New odebody o\body = ODE_dBodyCreate() ;create the rigid body geometry for ode o\geo = ODE_dCreateBox(ODE,sx,sy,sz,10) ODE_dGeomSetBody o\geo, o\body ;set position and rotation ODE_dBodySetPosition(o\body,x,y,z) ODE_dBodySetRotation(o\body,0,0,0) ;link ODELink(ent,o\geo) Return o\body End Function
For i=1 To 100 temp=CreateCube() PositionEntity temp,Rand(-100,100),100,Rand(-100,100) ;turn it into an ode object obj = ODECube(temp) Next
;mainloop While Not KeyHit(1) UpdateInput() Repeat elapsed=MilliSecs()-time Until elapsed ticks=elapsed/period tween#=Float(elapsed Mod period)/Float(period) For k=1 To ticks time=time+period If k=ticks Then CaptureWorld UpdateGame() ODEUpdate(0.1) ;required before updateworld UpdateWorld Next ODEUpdateLinks() ;required before renderworld RenderWorld tween Flip Wend ODEEnd() End
count = ODE_dBodyGetCollisionsCount(box) ;CountCollisions(ent) ;loop through all collisions... For i = 0 To count-1 depth# = ODE_dBodyGetCollisionDepth(box, i) ;force of collision... (depth)... use FX for a true force. collidedwith = ODE_dBodyGetCollisionBody(box,i) ;the body it collided with... Next
; ---------------------------- ; Global ODE space ; ---------------------------- Global ODE_Space ; -------------------------- ; ODE Object Type ; -------------------------- Type ODE_Object Field ObjectBody Field ObjectMesh Field ObjectGeom Field ObjectFake ; Pour les cylindres Field ObjectSound Field ObjectChannel Field ObjectMass# Field ObjectRotX# Field ObjectRotY# Field ObjectRotZ# Field ObjectPx# Field ObjectPy# Field ObjectPz# Field ObjectScaleX# Field ObjectScaleY# Field ObjectScaleZ# End Type ; ------------------------------------ ; On setup le monde ODE ; ------------------------------------ Function ODE_InitWorld(Gravity#=-1.5,Bounce#=0.5) ODE_Space = ODE_dWorldCreate() ODE_dWorldSetGravity(0, Gravity#, 0) ODE_dSetContactMode(dContactBounce+ dContactSoftERP ) ; dContactBounce+ dContactSoftERP + dContactSoftCFM ODE_dSetBOUNCE(Bounce#) ODE_dSetSOFT_ERP(0.4) ODE_dSetSOFT_CFM(0.8) End Function ; ----------------------------------- ; Setup easily cube mesh ; ----------------------------------- Function ODE_CreateCubeMesh(Mesh$,Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Mass#=1,Sound$="") G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = LoadMesh(Mesh$) ScaleEntity G\ObjectMesh,1,1,1 G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# G\ObjectScaleX#=MeshWidth(G\ObjectMesh) G\ObjectScaleY#=MeshHeight(G\ObjectMesh) G\ObjectScaleZ#=MeshDepth(G\ObjectMesh) G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf End Function ; ----------------------------------- ; Setup easily cube mesh ; ----------------------------------- Function ODE_CreateSphereMesh(Mesh$,Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Mass#=1,Sound$="") G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = LoadMesh(Mesh$) G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# G\ObjectScaleX#=MeshHeight(G\ObjectMesh) G\ObjectScaleY#=MeshHeight(G\ObjectMesh) G\ObjectScaleZ#=MeshHeight(G\ObjectMesh) G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateSphere(ODE_Space, G\ObjectScaleX#,G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf End Function ; -------------------------- ; Setup easily cube ; -------------------------- Function ODE_CreateCube(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,ScaleX#,ScaleY#,ScaleZ#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=ScaleX# G\ObjectScaleY#=ScaleY# G\ObjectScaleZ#=ScaleZ# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#*2,G\ObjectScaleY#*2,G\ObjectScaleZ#*2, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreateCube() RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ----------------------------- ; Setup easily sphere ; ----------------------------- Function ODE_CreateSphere(Px#,Py#,Pz#,Scale#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Scale# G\ObjectScaleY#=Scale# G\ObjectScaleZ#=Scale# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateSphere(ODE_Space,G\ObjectScaleX#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) G\ObjectMesh = CreateSphere() PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ------------------------------- ; Setup easily cylinder ; ------------------------------- Function ODE_CreateCylinder(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Radius#,Height#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Radius# G\ObjectScaleY#=Height# G\ObjectScaleZ#=Radius# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateCCylinder(ODE_Space,G\ObjectScaleX#, G\ObjectScaleY#*2,G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreatePivot() G\ObjectFake=CreateCylinder(8,1,G\ObjectMesh) RotateEntity G\ObjectFake, 90, 0, 0 PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectFake,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# EntityColor G\ObjectFake, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ------------------------------------------------ ; On update la physique des objets ; ------------------------------------------------ Function ODE_UpdatePhysic(Time#=0.1) For G.ODE_Object = Each ODE_Object ; -------------------------------------------------------------- ; On update la position et rotation des objets ; -------------------------------------------------------------- PositionEntity G\ObjectMesh, ODE_dGeomGetPositionX(G\ObjectGeom), ODE_dGeomGetPositionY(G\ObjectGeom), ODE_dGeomGetPositionZ(G\ObjectGeom) RotateEntity G\ObjectMesh, ODE_dGeomGetPitch(G\ObjectGeom), ODE_dGeomGetYaw(G\ObjectGeom), ODE_dGeomGetRoll(G\ObjectGeom), 1 ; ------------------------------------------------------------ ; Gestion du son en fonction des collisions ; ------------------------------------------------------------ If G\ObjectSound<>0 ODE_CollisionCount% = ODE_dBodyGetCollisionsCount%(G\ObjectBody) For i = 0 To ODE_CollisionCount% - 1 ODE_CollisionDepth# =ODE_dBodyGetCollisionDepth(G\ObjectBody, i) DebugLog ODE_CollisionDepth# If ODE_CollisionDepth# >=0.05 And ODE_CollisionDepth# <=0.3 If ChannelPlaying(G\ObjectChannel)=0 G\ObjectChannel = PlaySound(G\ObjectSound) ChannelVolume G\ObjectChannel, ODE_CollisionDepth * 3 EndIf EndIf Next EndIf Next End Function And an exemple ; ------------------------ ; Freelook cam var ; ------------------------ Global Mouse_X_Speed# Global Mouse_Y_Speed# Global Camera_VelX# Global Camera_VelZ# Global Camera_Pitch Global Camera_Yaw Include "BlitzODE.bb" Include "Inc_ODEStuff.bb" ;------ Initialize Graphics -------- Graphics3D 800, 600,16,2 SetBuffer BackBuffer() lh = CreateLight(2) PositionEntity lh,50,50,50 LightRange lh,100 LightColor lh,255,200,150 plane = CreatePlane() ;mirror = CreateMirror(plane) EntityColor plane, 0, 0, 150 ;EntityAlpha plane, .8 PositionEntity plane,0,-0.3,0 SeedRnd MilliSecs() ;--------- Initialize ODE ---------- ODE_InitWorld() ;------- Create siple cube --------- For i=0 To 100 Mass#=30 Area=30 ;ODE_CreateCube(Rnd(-5,5),i,Rnd(-5,5),0,0,0,1,1,1,Mass#,"Bounce.wav",Rnd(255),Rnd(255),Rnd(255)) ;ODE_CreateSphere(Rnd(-5,5),i,Rnd(-5,5),1,Mass#,"Bounce.wav",Rnd(255),Rnd(255),Rnd(255)) ODE_CreateCylinder(Rnd(-Area,Area),5,Rnd(-Area,Area),90,0,0,1,3,Mass#,"chimes.wav",Rnd(255),Rnd(255),Rnd(255)) ;ODE_CreateCubeMesh("chamber.b3d",Rnd(-20,20),i,Rnd(-20,20),0,0,0,Mass#,"Bounce.wav") ;ODE_CreateSphereMesh("chamber.b3d",Rnd(-20,20),i,Rnd(-20,20),0,0,0,Mass#,"Bounce.wav") Next ;mesh=LoadMesh("002.b3d") ;ODE_dTriMeshCreate%(mesh) ;----------------------------------- ODE_UpdatePhysic() cam = CreateCamera() PositionEntity cam, 0, 10, -25 cambody = ODE_CreateSphere(0, 10, -25,5,0,"",0,0,0,0) ;----------------------------------- gameFPS = 50.0 framePeriod = 1000 / gameFPS frameTime = MilliSecs () - framePeriod MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 While Not KeyHit(1) Repeat frameElapsed = MilliSecs () - frameTime Until frameElapsed frameTicks = frameElapsed / framePeriod frameTween# = Float(frameElapsed Mod framePeriod) / Float(framePeriod) For frameLimit = 1 To frameTicks If frameLimit = frameTicks Then CaptureWorld frameTime = frameTime + framePeriod ;------------ Update physics world ---------- ODE_dWorldQuickStep 0.1 ;-------------------------------------------- Next If KeyHit(57) For G.ODE_Object = Each ODE_Object ODE_dBodyEnable G\ObjectBody ODE_dBodyAddForce G\ObjectBody, 0, Rnd(20, 50), 0 ODE_dBodyAddTorque G\ObjectBody, Rnd(-30, 10), Rnd(-10, 10), Rnd(-10, 10) Next EndIf ;---- Set geometry position and rotation ---- ODE_UpdatePhysic() Proc_Freelook(Cam,1.05,0.01) ODE_dBodySetPosition(cambody,EntityX(Cam),EntityY(Cam), EntityZ(Cam)) ;-------------------------------------------- RenderWorld frameTween# Flip 0 Wend ODE_dCloseODE() End Function Proc_Freelook(CamEntity,Velocity#,Speed#) Mouse_X_Speed#=MouseXSpeed()*0.5 Mouse_Y_Speed#=MouseYSpeed()*0.5 MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 Camera_Pitch=Camera_Pitch+Mouse_Y_Speed# Camera_Yaw=Camera_Yaw+Mouse_X_Speed# RotateEntity CamEntity,Camera_Pitch,-Camera_Yaw,0 If KeyDown(203) Then Camera_VelX=Camera_VelX-Speed# ElseIf KeyDown(205) Camera_VelX=Camera_VelX+Speed# EndIf If KeyDown(208) Then Camera_VelZ=Camera_VelZ-Speed# ElseIf KeyDown(200) Camera_VelZ=Camera_VelZ+Speed# EndIf Camera_VelX=Camera_VelX/velocity# Camera_VelZ=Camera_VelZ/velocity# MoveEntity CamEntity,Camera_VelX,0,Camera_VelZ If KeyDown(200) Then MoveEntity CamEntity,0,0,Speed# EndIf If KeyDown(208) Then MoveEntity CamEntity,0,0,-Speed# EndIf If KeyDown(205) Then MoveEntity CamEntity,Speed#,0,0 EndIf If KeyDown(203) Then MoveEntity CamEntity,-Speed#,0,0 EndIf End Function
; ---------------------------- ; Global ODE space ; ---------------------------- Global ODE_Space ; -------------------------- ; ODE Object Type ; -------------------------- Type ODE_Object Field ObjectBody Field ObjectMesh Field ObjectGeom Field ObjectFake ; Pour les cylindres Field ObjectSound Field ObjectChannel Field ObjectMass# Field ObjectRotX# Field ObjectRotY# Field ObjectRotZ# Field ObjectPx# Field ObjectPy# Field ObjectPz# Field ObjectScaleX# Field ObjectScaleY# Field ObjectScaleZ# End Type ; ------------------------------------ ; On setup le monde ODE ; ------------------------------------ Function ODE_InitWorld(Gravity#=-1.5,Bounce#=0.1) ODE_Space = ODE_dWorldCreate() ; ODE_dWorldSetGravity(0, Gravity#, 0) ; ODE_dSetContactMode(dContactBounce+ dContactSoftERP ) ; dContactBounce+ dContactSoftERP + dContactSoftCFM ; ODE_dSetBOUNCE(Bounce#) ; ODE_dSetSOFT_ERP(0.4) ; ODE_dSetSOFT_CFM(0.8) ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM + dSlip1 + dSlip2) ODE_dSetBOUNCE(0.5) ODE_dSetSOFT_ERP(0.3) ODE_dSetSOFT_CFM(0.00000001);0.3) End Function ; ------------------------------------------------------------------------- ; Permet de créer un cube ODE a partie d'une mesh ; ------------------------------------------------------------------------- Function ODE_CreateCubeMesh(Mesh$,Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Mass#=1,Sound$="") G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = LoadMesh(Mesh$) ScaleEntity G\ObjectMesh,1,1,1 G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# G\ObjectScaleX#=MeshWidth(G\ObjectMesh) G\ObjectScaleY#=MeshHeight(G\ObjectMesh) G\ObjectScaleZ#=MeshDepth(G\ObjectMesh) G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf End Function ; ------------------------------------------------------------------------------ ; Permet de créer une sphere ODE a partie d'une mesh ; ------------------------------------------------------------------------------ Function ODE_CreateSphereMesh(Mesh$,Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Mass#=1,Sound$="") G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = LoadMesh(Mesh$) G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# G\ObjectScaleX#=MeshHeight(G\ObjectMesh) G\ObjectScaleY#=MeshHeight(G\ObjectMesh) G\ObjectScaleZ#=MeshHeight(G\ObjectMesh) G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateSphere(ODE_Space, G\ObjectScaleX#,G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf End Function ; ------------------------------------ ; Permet de créer un cube ; ------------------------------------ Function ODE_CreateCube(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,ScaleX#,ScaleY#,ScaleZ#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=ScaleX# G\ObjectScaleY#=ScaleY# G\ObjectScaleZ#=ScaleZ# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#*2,G\ObjectScaleY#*2,G\ObjectScaleZ#*2, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreateCube() RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ---------------------------------------- ; Permet de créer une sphere ; ---------------------------------------- Function ODE_CreateSphere(Px#,Py#,Pz#,Scale#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Scale# G\ObjectScaleY#=Scale# G\ObjectScaleZ#=Scale# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateSphere(ODE_Space,G\ObjectScaleX#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) G\ObjectMesh = CreateSphere() PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectMesh,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# EntityColor G\ObjectMesh, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ---------------------------------------- ; Permet de créer un cylindre ; --------------------------------------- Function ODE_CreateCylinder(Px#,Py#,Pz#,RotX#,RotY#,RotZ#,Radius#,Height#,Mass#=1,Sound$="",Red=255,Green=255,Blue=255,View%=1) G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectScaleX#=Radius# G\ObjectScaleY#=Height# G\ObjectScaleZ#=Radius# G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateCCylinder(ODE_Space,G\ObjectScaleX#, G\ObjectScaleY#*2,G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody G\ObjectPx#=Px# G\ObjectPy#=Py# G\ObjectPz#=Pz# G\ObjectRotX#=RotX# G\ObjectRotY#=RotY# G\ObjectRotZ#=RotZ# ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) G\ObjectMesh = CreatePivot() G\ObjectFake=CreateCylinder(8,1,G\ObjectMesh) RotateEntity G\ObjectFake, 90, 0, 0 PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ScaleEntity G\ObjectFake,G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ# RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# EntityColor G\ObjectFake, Red,Green,Blue If View%=0 Then HideEntity G\ObjectMesh EndIf If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf Return G\ObjectBody End Function ; ------------------------------------------------ ; On update la physique des objets ; ------------------------------------------------ Function ODE_UpdatePhysic(Time#=0.1) For G.ODE_Object = Each ODE_Object ; -------------------------------------------------------------- ; On update la position et rotation des objets ; -------------------------------------------------------------- PositionEntity G\ObjectMesh, ODE_dGeomGetPositionX(G\ObjectGeom), ODE_dGeomGetPositionY(G\ObjectGeom), ODE_dGeomGetPositionZ(G\ObjectGeom) RotateEntity G\ObjectMesh, ODE_dGeomGetPitch(G\ObjectGeom), ODE_dGeomGetYaw(G\ObjectGeom), ODE_dGeomGetRoll(G\ObjectGeom), 1 ; ------------------------------------------------------------ ; Gestion du son en fonction des collisions ; ------------------------------------------------------------ If G\ObjectSound<>0 ODE_CollisionCount% = ODE_dBodyGetCollisionsCount%(G\ObjectBody) For i = 0 To ODE_CollisionCount% - 1 ODE_CollisionDepth# =ODE_dBodyGetCollisionDepth(G\ObjectBody, i) DebugLog ODE_CollisionDepth# If ODE_CollisionDepth# >=0.05 And ODE_CollisionDepth# <=0.3 If ChannelPlaying(G\ObjectChannel)=0 G\ObjectChannel = PlaySound(G\ObjectSound) ChannelVolume G\ObjectChannel, ODE_CollisionDepth * 3 EndIf EndIf Next EndIf Next End Function Global TOKP_taillex Global TOKP_tailley Global TOKP_taillez Global TOKP_centrex# Global TOKP_centrey# Global TOKP_centrez# Function ODE_AnimMeshPhysic(Parent%) Children%=CountChildren(Parent%) ;je scanne que les premiers enfants pour pas créer une box au pivot For Loop = 1 To Children% Child%=GetChild (Parent%,Loop) ODE_CreateCubeChild(Child%,10,"") ;ODE_AnimMeshPhysic(Child%) Next End Function ; ------------------------------------------------------------------------- ; Permet de créer un cube ODE a partie d'une mesh ; ------------------------------------------------------------------------- Function ODE_CreateCubeChild(Mesh,Mass#=1,Sound$="") G.ODE_Object = New ODE_Object G\ObjectMass#=Mass# G\ObjectMesh = Mesh ;If mesh<>0 Then EntityParent Mesh,0;PivotDeMeurde G\ObjectRotX#=EntityPitch(Mesh,True) G\ObjectRotY#=EntityYaw(Mesh,True) G\ObjectRotZ#=EntityRoll(Mesh,True) ;RotateEntity G\ObjectMesh,G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ# ;PositionEntity G\ObjectMesh,G\ObjectPx#,G\ObjectPy#,G\ObjectPz# ;tu gères pas le scale de 3dsmax là, ca va te faire des irrégularités TOKP_CalculeTailleCentreEntity(G\ObjectMesh) G\ObjectPx#=EntityX(mesh) G\ObjectPy#=EntityY(mesh) G\ObjectPz#=EntityZ(mesh) G\ObjectScaleX#=TOKP_taillex*0.5 G\ObjectScaleY#=TOKP_tailley*0.5 G\ObjectScaleZ#=TOKP_taillez*0.5 G\ObjectBody = ODE_dBodyCreate() G\ObjectGeom = ODE_dCreateBox(ODE_Space, G\ObjectScaleX#,G\ObjectScaleY#,G\ObjectScaleZ#, G\ObjectMass#) ODE_dGeomSetBody G\ObjectGeom, G\ObjectBody ODE_dBodySetPosition(G\ObjectBody, G\ObjectPx#,G\ObjectPy#,G\ObjectPz#) ODE_dBodySetRotation(G\ObjectBody, G\ObjectRotX#,G\ObjectRotY#,G\ObjectRotZ#) If Sound$<>"" Then G\ObjectSound=LoadSound(Sound$) Else G\ObjectSound=0 EndIf End Function Function TOKP_CalculeTailleCentreEntity(e); xmin#=99999999999 xmax#=-99999999999 ymin#=99999999999 ymax#=-99999999999 zmin#=99999999999 zmax#=-99999999999 For k=1 To CountSurfaces(e) For l=0 To CountVertices(GetSurface(e,k))-1 If VertexX(GetSurface(e,k),l)<=xmin# Then xmin#=VertexX(GetSurface(e,k),l) If VertexX(GetSurface(e,k),l)>=xmax# Then xmax#=VertexX(GetSurface(e,k),l) If VertexY(GetSurface(e,k),l)<=ymin# Then ymin#=VertexY(GetSurface(e,k),l) If VertexY(GetSurface(e,k),l)>=ymax# Then ymax#=VertexY(GetSurface(e,k),l) If VertexZ(GetSurface(e,k),l)<=zmin# Then zmin#=VertexZ(GetSurface(e,k),l) If VertexZ(GetSurface(e,k),l)>=zmax# Then zmax#=VertexZ(GetSurface(e,k),l) Next Next TOKP_centrex#=xmin*0.5+xmax*0.5 TOKP_centrey#=ymin*0.5+ymax*0.5 TOKP_centrez#=zmin*0.5+zmax*0.5 ;Calcule les valeurs de scale x#=EntityPitch(e,True) y#=EntityYaw(e,True) z#=EntityRoll(e,True) RotateEntity e,0,0,0,True scalex#=GetMatElement(e,0,0) scaley#=GetMatElement(e,1,1) scalez#=GetMatElement(e,2,2) RotateEntity e,x,y,z,True TOKP_centrex=scalex*TOKP_centrex TOKP_centrey=scaley*TOKP_centrey TOKP_centrez=scalez*TOKP_centrez TOKP_taillex=(xmax-xmin)*scalex TOKP_tailley=(ymax-ymin)*scaley TOKP_taillez=(zmax-zmin)*scalez End Function Example code ; ------------------------ ; Freelook cam var ; ------------------------ Global Mouse_X_Speed# Global Mouse_Y_Speed# Global Camera_VelX# Global Camera_VelZ# Global Camera_Pitch Global Camera_Yaw Include "BlitzODE.bb" Include "Inc_ODEStuff.bb" ;------ Initialize Graphics -------- Graphics3D 800, 600,16,2 SetBuffer BackBuffer() lh = CreateLight(2) PositionEntity lh,50,50,50 LightRange lh,100 LightColor lh,255,200,150 plane = CreatePlane() ;mirror = CreateMirror(plane) EntityColor plane, 0, 0, 150 ;EntityAlpha plane, .8 PositionEntity plane,0,-0.3,0 SeedRnd MilliSecs() ;--------- Initialize ODE ---------- ODE_InitWorld() ;------- Create siple cube --------- ;For i=0 To 5 ;Mass#=30 ;Area=30 ;ODE_CreateCube(Rnd(-5,5),i,Rnd(-5,5),0,0,0,1,1,1,Mass#,"Bounce.wav",Rnd(255),Rnd(255),Rnd(255)) ;ODE_CreateSphere(Rnd(-5,5),i,Rnd(-5,5),1,Mass#,"Bounce.wav",Rnd(255),Rnd(255),Rnd(255)) ;ODE_CreateCylinder(Rnd(-Area,Area),5,Rnd(-Area,Area),90,0,0,1,3,Mass#,"chimes.wav",Rnd(255),Rnd(255),Rnd(255)) ; ;ODE_CreateCubeMesh("teapot.b3d",Rnd(-20,20),i,Rnd(-20,20),0,0,0,Mass#,"Bounce.wav") ;ODE_CreateSphereMesh("chamber.b3d",Rnd(-20,20),i,Rnd(-20,20),0,0,0,Mass#,"Bounce.wav") ;Next mesh=LoadAnimMesh("002.b3d") PositionEntity mesh,0,10,0 ODE_AnimMeshPhysic(mesh) ;----------------------------------- ODE_UpdatePhysic() cam = CreateCamera() PositionEntity cam, 0, 10, -25 cambody = ODE_CreateSphere(0, 10, -25,6,0,"",0,0,0,0) ;----------------------------------- gameFPS = 50.0 framePeriod = 1000 / gameFPS frameTime = MilliSecs () - framePeriod MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 While Not KeyHit(1) Repeat frameElapsed = MilliSecs () - frameTime Until frameElapsed frameTicks = frameElapsed / framePeriod frameTween# = Float(frameElapsed Mod framePeriod) / Float(framePeriod) For frameLimit = 1 To frameTicks If frameLimit = frameTicks Then CaptureWorld frameTime = frameTime + framePeriod ;------------ Update physics world ---------- ODE_dWorldQuickStep 0.1 ;-------------------------------------------- Next If KeyHit(57) For G.ODE_Object = Each ODE_Object ODE_dBodyEnable G\ObjectBody ODE_dBodyAddForce G\ObjectBody, 0, Rnd(20, 50), 0 ODE_dBodyAddTorque G\ObjectBody, Rnd(-30, 10), Rnd(-10, 10), Rnd(-10, 10) Next EndIf ;---- Set geometry position and rotation ---- ODE_UpdatePhysic() Proc_Freelook(Cam,1.05,0.05) ODE_dBodySetPosition(cambody,EntityX(Cam),EntityY(Cam), EntityZ(Cam)) ;-------------------------------------------- RenderWorld frameTween# Flip 0 Wend ODE_dCloseODE() End Function Proc_Freelook(CamEntity,Velocity#,Speed#) Mouse_X_Speed#=MouseXSpeed()*0.5 Mouse_Y_Speed#=MouseYSpeed()*0.5 MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 Camera_Pitch=Camera_Pitch+Mouse_Y_Speed# Camera_Yaw=Camera_Yaw+Mouse_X_Speed# RotateEntity CamEntity,Camera_Pitch,-Camera_Yaw,0 If KeyDown(203) Then Camera_VelX=Camera_VelX-Speed# ElseIf KeyDown(205) Camera_VelX=Camera_VelX+Speed# EndIf If KeyDown(208) Then Camera_VelZ=Camera_VelZ-Speed# ElseIf KeyDown(200) Camera_VelZ=Camera_VelZ+Speed# EndIf Camera_VelX=Camera_VelX/velocity# Camera_VelZ=Camera_VelZ/velocity# MoveEntity CamEntity,Camera_VelX,0,Camera_VelZ If KeyDown(200) Then MoveEntity CamEntity,0,0,Speed# EndIf If KeyDown(208) Then MoveEntity CamEntity,0,0,-Speed# EndIf If KeyDown(205) Then MoveEntity CamEntity,Speed#,0,0 EndIf If KeyDown(203) Then MoveEntity CamEntity,-Speed#,0,0 EndIf End Function
;creates a rigid body cube from a source entity's dimensions and link them together Function ODECube(ent) ;get transform x#=EntityX(ent,1) y#=EntityY(ent,1) z#=EntityZ(ent,1) sx#=MeshWidth(ent) sy#=MeshHeight(ent) sz#=MeshDepth(ent) ;create ode rigid body o.odebody = New odebody o\body = ODE_dBodyCreate() ;create the rigid body geometry for ode o\geo = ODE_dCreateBox(ODE,sx,sy,sz,10) ODE_dGeomSetBody o\geo, o\body ;set position and rotation ODE_dBodySetPosition(o\body,x,y,z) ODE_dBodySetRotation(o\body,0,0,0) ;link ODELink(ent,o\geo) Return o\body End Function
Graphics3D 800,600,16,2 Include "BlitzODE.bb" cam=CreateCamera() PositionEntity cam,0,2,-8 l=CreateLight(2) PositionEntity l,-5,10,-5 LightRange l,10 pl=CreatePlane(16) EntityAlpha pl,.5 texpl=CreateTexture(128,128,1+8) Color 255,255,255 Rect 0,0,64,64,1 Rect 64,64,64,64,1 CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(texpl) ScaleTexture texpl,6,6 EntityTexture pl,texpl,0,0 m=CreateMirror() ;---- Setting contact parameters --- ;ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM + dSlip1 + dSlip2) ODE_dSetMU(.5); Friction ODE_dSetBOUNCE(.5) ODE_dSetSOFT_ERP(.5) ODE_dSetSOFT_CFM(.01) ;------------------------------- ;Crear un Espacio para ODE: Global space = ODE_dWorldCreate(1) ;------------------------------- ;Chasis ode_body1=ODE_dBodyCreate() geom=ODE_dCreateBox(space,.3,.5,4,10) ODE_dGeomSetBody geom,ode_body1 ODE_dBodySetPosition ode_body1,0,4,-2 ODE_dBodySetRotation ode_body1,0,0,0 ODE_dBodySetMass ode_body1,200 vis1=CreateCube() ScaleEntity vis1,.15,.25,2 EntityColor vis1,255,100,0 ;rueda der ode_body2=ODE_dBodyCreate() ;geom=ODE_dCreateBox(space,.25,1,1,10) geom=ODE_dCreateSphere(space,1,10) ODE_dGeomSetBody geom,ode_body2 ODE_dBodySetPosition ode_body2,2,3,0 ODE_dBodySetRotation ode_body2,0,0,0 ODE_dBodySetMass ode_body2,30 vis2=CreateCube() ScaleEntity vis2,.125,.5,.5 EntityColor vis2,100,255,0 ;rueda izq ode_body3=ODE_dBodyCreate() ;geom=ODE_dCreateBox(space,.25,1,1,10) geom=ODE_dCreateSphere(space,1,10) ODE_dGeomSetBody geom,ode_body3 ODE_dBodySetPosition ode_body3,-2,3,0 ODE_dBodySetRotation ode_body3,0,0,0 ODE_dBodySetMass ode_body3,30 vis3=CreateCube() ScaleEntity vis3,.125,.5,.5 EntityColor vis3,100,255,0 ;Traseras ;rueda der ode_body4=ODE_dBodyCreate() ;geom=ODE_dCreateBox(space,.25,1,1,10) geom=ODE_dCreateSphere(space,1,10) ODE_dGeomSetBody geom,ode_body4 ODE_dBodySetPosition ode_body4,2,3,-4 ODE_dBodySetRotation ode_body4,0,0,0 ODE_dBodySetMass ode_body4,30 vis4=CreateCube() ScaleEntity vis4,.125,.5,.5 EntityColor vis4,0,0,255 ;rueda izq ode_body5=ODE_dBodyCreate() ;geom=ODE_dCreateBox(space,.25,1,1,10) geom=ODE_dCreateSphere(space,1,10) ODE_dGeomSetBody geom,ode_body5 ODE_dBodySetPosition ode_body5,-2,3,-4 ODE_dBodySetRotation ode_body5,0,0,0 ODE_dBodySetMass ode_body5,30 vis5=CreateCube() ScaleEntity vis5,.125,.5,.5 EntityColor vis5,0,0,255 ;Joints joint1=ODE_dJointCreateBall() ODE_dJointAttach joint1,0,ode_body1 ODE_dJointSetBallAnchor joint1,0,5,0 ;ODE_dJointSetUniversalAxis1 joint1,0,0,.1 ;ODE_dJointSetUniversalAxis2 joint1,1,0,0 suspension_hard#=4 suspension_soft#=.08 ;rueda der joint2=ODE_dJointCreateHinge2() ODE_dJointAttach joint2,ode_body1,ode_body2 ODE_dJointSetHinge2Anchor joint2,1.875,3,0 ODE_dJointSetHinge2Axis1 joint2,0,1,0 ODE_dJointSetHinge2Axis2 joint2,1,0,0 ODE_dJointSetHinge2Param joint2,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint2,dParamHiStop,0;Pi/4 ODE_dJointSetHinge2Param joint2,dParamSuspensionERP,suspension_hard ODE_dJointSetHinge2Param joint2,dParamSuspensionCFM,suspension_soft ;rueda izq joint3=ODE_dJointCreateHinge2() ODE_dJointAttach joint3,ode_body1,ode_body3 ODE_dJointSetHinge2Anchor joint3,-1.875,3,0 ODE_dJointSetHinge2Axis1 joint3,0,1,0 ODE_dJointSetHinge2Axis2 joint3,1,0,0 ODE_dJointSetHinge2Param joint3,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint3,dParamHiStop,0;Pi/4 ODE_dJointSetHinge2Param joint3,dParamSuspensionERP,suspension_hard ODE_dJointSetHinge2Param joint3,dParamSuspensionCFM,suspension_soft ;Joints Traseros: ;rueda der joint4=ODE_dJointCreateHinge2() ODE_dJointAttach joint4,ode_body1,ode_body4 ODE_dJointSetHinge2Anchor joint4,1.875,3,-4 ODE_dJointSetHinge2Axis1 joint4,0,1,0 ODE_dJointSetHinge2Axis2 joint4,1,0,0 ODE_dJointSetHinge2Param joint4,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint4,dParamHiStop,0;Pi/4 ODE_dJointSetHinge2Param joint4,dParamSuspensionERP,suspension_hard ODE_dJointSetHinge2Param joint4,dParamSuspensionCFM,suspension_soft ;rueda izq joint5=ODE_dJointCreateHinge2() ODE_dJointAttach joint5,ode_body1,ode_body5 ODE_dJointSetHinge2Anchor joint5,-1.875,3,-4 ODE_dJointSetHinge2Axis1 joint5,0,1,0 ODE_dJointSetHinge2Axis2 joint5,1,0,0 ODE_dJointSetHinge2Param joint5,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint5,dParamHiStop,0;Pi/4 ODE_dJointSetHinge2Param joint5,dParamSuspensionERP,suspension_hard ODE_dJointSetHinge2Param joint5,dParamSuspensionCFM,suspension_soft millis#=MilliSecs() millis2#=MilliSecs() elapsed#=0 While Not KeyDown(1) millis=MilliSecs()-millis elapsed=millis millis=MilliSecs() ODE_dBodyEnable ode_body1 ODE_dBodyEnable ode_body2 ODE_dBodyEnable ode_body3 If KeyDown(200) ODE_dBodyAddRelTorque ode_body2,40,0,0 ODE_dBodyAddRelTorque ode_body3,40,0,0 EndIf If KeyDown(208) ODE_dBodyAddRelTorque ode_body2,-40,0,0 ODE_dBodyAddRelTorque ode_body3,-40,0,0 EndIf angle#=Pi/4 If KeyDown(203) ODE_dJointSetHinge2Param joint2,dParamLoStop,angle ODE_dJointSetHinge2Param joint2,dParamHiStop,angle ODE_dJointSetHinge2Param joint3,dParamLoStop,angle ODE_dJointSetHinge2Param joint3,dParamHiStop,angle ElseIf KeyDown(205) ODE_dJointSetHinge2Param joint2,dParamLoStop,-angle ODE_dJointSetHinge2Param joint2,dParamHiStop,-angle ODE_dJointSetHinge2Param joint3,dParamLoStop,-angle ODE_dJointSetHinge2Param joint3,dParamHiStop,-angle Else ODE_dJointSetHinge2Param joint2,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint2,dParamHiStop,0;Pi/4 ODE_dJointSetHinge2Param joint3,dParamLoStop,0;-Pi/4 ODE_dJointSetHinge2Param joint3,dParamHiStop,0;Pi/4 EndIf If KeyHit(30) ODE_dJointAttach joint1,0,0 EndIf If KeyHit(31) ODE_dJointAttach joint1,0,ode_body1 EndIf ;------------- PositionEntity vis1,ODE_dBodyGetPositionX(ode_body1),ODE_dBodyGetPositionY(ode_body1),ODE_dBodyGetPositionZ(ode_body1) RotateEntity vis1,ODE_dBodyGetPitch(ode_body1),ODE_dBodyGetYaw(ode_body1),ODE_dBodyGetRoll(ode_body1) PositionEntity vis2,ODE_dBodyGetPositionX(ode_body2),ODE_dBodyGetPositionY(ode_body2),ODE_dBodyGetPositionZ(ode_body2) RotateEntity vis2,ODE_dBodyGetPitch(ode_body2),ODE_dBodyGetYaw(ode_body2),ODE_dBodyGetRoll(ode_body2) PositionEntity vis3,ODE_dBodyGetPositionX(ode_body3),ODE_dBodyGetPositionY(ode_body3),ODE_dBodyGetPositionZ(ode_body3) RotateEntity vis3,ODE_dBodyGetPitch(ode_body3),ODE_dBodyGetYaw(ode_body3),ODE_dBodyGetRoll(ode_body3) PositionEntity vis4,ODE_dBodyGetPositionX(ode_body4),ODE_dBodyGetPositionY(ode_body4),ODE_dBodyGetPositionZ(ode_body4) RotateEntity vis4,ODE_dBodyGetPitch(ode_body4),ODE_dBodyGetYaw(ode_body4),ODE_dBodyGetRoll(ode_body4) PositionEntity vis5,ODE_dBodyGetPositionX(ode_body5),ODE_dBodyGetPositionY(ode_body5),ODE_dBodyGetPositionZ(ode_body5) RotateEntity vis5,ODE_dBodyGetPitch(ode_body5),ODE_dBodyGetYaw(ode_body5),ODE_dBodyGetRoll(ode_body5) ;------------- ODE_dWorldQuickStep .15 UpdateWorld RenderWorld ; Text 10,10,elapsed ; Text 10,20,ODE_dBodyGetAngularVelX(ode_body2) Text 10,30,"press A to release the car attach" Text 10,40,"press S to force a re-attach of the car" Text 10,50,"use ARROWS to move" Flip Wend ODE_dCloseODE() MoveMouse 400,300:End