Post by botbuilder on Jun 20, 2004 23:27:56 GMT
Here is a quick water demo I did for my docs. It uses some of the new features, specifically TOKRB_GravityEnable. Right now the water is just a plane (should be easy enough to make it a quad mesh), and the objects only float when the water hits the center. I plan to put together a system that will have pivots on the rigid body each applying an impulse if submerged.
Const Rigid_Bodies=200,Animated_Bodies=7
TOKSIM_SetRigidBodiesCount Rigid_Bodies
TOKSIM_SetAnimatedBodiesCount Animated_Bodies
TOKSIM_SetRigidParticleCount 0
TOKSIM_SetControllersCount 0
TOKSIM_SetGeometriesCount Rigid_bodies+Animated_Bodies ;Assuming each one only has one geometry. Not true with more complex rigid bodies and animated bodies.
TOKSIM_CreateSimulator(0,-1,0)
Graphics3D 640,480
;Create Scene
wall1=CreateCube()
ScaleEntity wall1,50,20,1
wall1rb=TOKAB_Create()
TOKAB_AddBox wall1rb,100,40,2
TOKAB_SetPosition wall1rb,0,0,-50
PositionEntity wall1,0,0,-50
wall2=CreateCube()
ScaleEntity wall2,50,20,1
wall2rb=TOKAB_Create()
TOKAB_AddBox wall2rb,100,40,2
TOKAB_SetPosition wall2rb,0,0,50
PositionEntity wall2,0,0,50
wall3=CreateCube()
ScaleEntity wall3,1,20,50
wall3rb=TOKAB_Create()
TOKAB_AddBox wall3rb,2,40,100
TOKAB_SetPosition wall3rb,-50,0,0
PositionEntity wall3,-50,0,0
wall4=CreateCube()
ScaleEntity wall4,1,20,50
wall4rb=TOKAB_Create()
TOKAB_AddBox wall4rb,2,40,100
TOKAB_SetPosition wall4rb,50,0,0
PositionEntity wall4,50,0,0
Flor=CreateCube()
ScaleEntity flor,50,1,50
PositionEntity flor,0,-20,0
Florb=TOKAB_Create()
TOKAB_AddBox Florb,100,2,100
TOKAB_SetPosition florb,0,-20,0
side1=CreateCube()
ScaleEntity side1,Sqr(3)*25/2,1,50 ;35.3552
RotateEntity side1,0,0,30
PositionEntity side1,32,-10,0
Side1b=TOKAB_Create()
TOKAB_AddBox Side1b,Sqr(3)*25,2,100
TOKAB_SetRotation Side1b,0,0,30
TOKAB_SetPosition Side1b,32,-10,0
side2=CreateCube()
ScaleEntity side2,Sqr(3)*25/2,1,50 ;35.3552
RotateEntity side2,0,0,-30
PositionEntity side2,-32,-10,0
Side2b=TOKAB_Create()
TOKAB_AddBox Side2b,Sqr(3)*25,2,100
TOKAB_SetRotation Side2b,0,0,-30
TOKAB_SetPosition Side2b,-32,-10,0
Water=CreatePlane()
EntityColor water,0,128,255
EntityAlpha water,.5
PositionEntity water,0,-10,0
campiv=CreatePivot()
cam=CreateCamera(campiv)
PositionEntity cam,0,50,-50
PointEntity cam,campiv
SeedRnd MilliSecs()
Dim obj(Rigid_Bodies)
Dim rb(Rigid_Bodies)
For i=1 To Rigid_Bodies
rot#=rot#+8.2
x#=Cos(rot)*20
z#=Sin(rot)*20
nr=Rnd(1,2)
If nr=2 Then
obj(i) = CreateCube()
ScaleEntity obj(i),1,0.5,0.5
rb(i) = TOKRB_Create()
geom = TOKRB_AddBox(rb(i),2.0,1.0,1.0)
TOKGEOM_SetMaterialIndex geom,1
TOKRB_SetPosition rb(i),x#,2+i*0.2,z#
TOKRB_SetLinearDamping rb(i),0.001
TOKRB_SetAngularDamping rb(i),0.002
TOKRB_SetMass rb(i),2.0
TOKRB_SetBoxInertiaTensor rb(i),2.0,1.0,1.0,2.0
EntityColor obj(i),Rnd(100,230),Rnd(100,230),Rnd(100,230)
ElseIf nr=1
obj(i) = CreateSphere()
rb(i) = TOKRB_Create()
TOKRB_AddSphere rb(i),2
TOKRB_SetPosition rb(i),x#,2+i*0.2,z#
TOKRB_SetAngularDamping rb(i),0.002
TOKRB_SetLinearDamping rb(i),0.001
TOKRB_SetMass rb(i),2.0
TOKRB_SetSphereInertiaTensor rb(i),2,2.0
EntityColor obj(i),Rnd(100,230),Rnd(100,230),Rnd(100,230)
EndIf
Next
l=CreateLight()
RotateEntity l,20,10,0
While Not KeyHit(1)
TurnEntity campiv,0,.1,0
TOKSIM_Advance .1,1
For i=1 To Rigid_Bodies
PositionEntity obj(i),TOKRB_GetX#(rb(i)),TOKRB_GetY#(rb(i)),TOKRB_GetZ#(rb(i))
RotateEntity obj(i),TOKRB_GetPitch#(rb(i)),TOKRB_GetYaw#(rb(i)),TOKRB_GetRoll#(rb(i)),False
If TOKRB_GetY#(rb(i))<-10 Then
If TOKRB_IsGravityEnabled(rb(i)) Then TOKRB_GravityEnable rb(i),0
TOKRB_ApplyImpulse rb(i),0,.02,0
TOKRB_SetAngularDamping rb(i),.01
TOKRB_SetLinearDamping rb(i),.01
ElseIf TOKRB_IsGravityEnabled(rb(i))=0 Then
TOKRB_GravityEnable rb(i),1
TOKRB_SetAngularDamping rb(i),.002
TOKRB_SetLinearDamping rb(i),.001
EndIf
Next
If KeyHit(57) Then
For i=1 To Rigid_Bodies
TOKRB_ApplyImpulse rb(i),Rand(-2,2),Rand(5,10),Rand(-2,2)
Next
EndIf
RenderWorld
Flip
Wend
TOKSIM_DestroySimulator