AnyKey`щик
Регистрация: 04.09.2006
Сообщений: 19
Написано 0 полезных сообщений (для 0 пользователей)
|
Re: ODE: требуется совет спеца!
;-----------------------------;
; BlitzODE ARKON Wrapper 0.5b ;
; Tutorial 3 ;
;-----------------------------;
Const SCALERATE# = 1
Const LENGTH# = 0.7 * 8*SCALERATE#
Const WIDTH# = 0.5 * 8*SCALERATE#
Const HEIGHT# = 0.2 * 8*SCALERATE#
Const RADIUS# = .16 * 8*SCALERATE#;*.3
Const STARTZ# = 5 + .5 ;* 8*SCALERATE#
Const CMASS# = 10
Const WMASS# = .1
Include "BlitzODE.bb"
;------ Initialize Graphics --------
Graphics3D 800, 600
SetBuffer BackBuffer()
SetFont LoadFont("Arial", 14, 1, 1, 0)
PV = CreatePivot()
cam = CreateCamera(PV)
PositionEntity cam, 2, 10, -40
lh = CreateLight(2)
PositionEntity lh, 100, 100, 100
;LightColor lh, 100, 100, 100
AmbientLight 50, 50, 50
plane = CreatePlane()
;mirror = CreateMirror(plane)
EntityColor plane, 0, 0, 150
EntityAlpha plane, .8
SeedRnd MilliSecs()
;--------- Initialize ODE ----------
Global space = ODE_dWorldCreate(1)
; ODE_dWorldSetGravity(0,-9.81,0);//proba
;---- Setting contact parameters ---
;ODE_dSetContactMode(dContactBounce + dContactSoftERP + dContactSoftCFM +dContactMu2);/+slip1+slip2/ContactApprox1dContactFDir2
;ODE_dSetMU(1);//
;ODE_dSetMU2(10);//
;ODE_dSetSLIP1(1)
;ODE_dSetSLIP2(10)
; ODE_dSetBOUNCE(0.5)
; ODE_dSetSOFT_ERP(0.9)
; ODE_dSetSOFT_CFM(0.1)
ODE_dSetContactMode(dContactBounce)
ODE_dSetMU(1)
;-------- Create many box ---------
For i = 1 To 1
g.TODEGeom = New TODEGeom
g\body = ODE_dBodyCreate()
g\geom = ODE_dCreateBox(space, 2, 2, 2, 10)
ODE_dGeomSetBody g\geom, g\body
ODE_dBodySetPosition(g\body, Rnd(-35, 35), Rnd(15, 25), Rnd(-35, 35))
ODE_dBodySetRotation(g\body, Rand(-180, 180), Rand(-180, 180), Rand(-180, 180))
g\mesh = CreateCube()
EntityColor g\mesh, Rand(255), Rand(255), Rand(255)
EntityShininess g\mesh, 0.7
Next
;------- Create many balls ---------
For i = 1 To 1
g.TODEGeom = New TODEGeom
g\body = ODE_dBodyCreate()
g\geom = ODE_dCreateSphere(space, 1, 10)
ODE_dGeomSetBody g\geom, g\body
ODE_dBodySetPosition(g\body, Rnd(-35, 35), Rnd(15, 25), Rnd(-35, 35))
ODE_dBodySetRotation(g\body, Rand(-180, 180), Rand(-180, 180), Rand(-180, 180))
g\mesh = CreateSphere()
EntityColor g\mesh, Rand(255), Rand(255), Rand(255)
EntityShininess g\mesh, 0.7
Next
;------- Create fixed box ----------
geom = ODE_dCreateBox(space, 15, .1, 15, 10)
ODE_dGeomSetPosition(geom, 40, 2.5, 0)
ODE_dGeomSetRotation(geom, 0, 0, 20)
mesh = CreateCube()
PositionEntity mesh, 40, 2.5, 0
RotateEntity mesh, 0, 0, 20
ScaleMesh mesh, .5, .05, .5
ScaleEntity mesh, 15, 1, 15
For i = 1 To 5
x# = Rnd(-200, 200)
y# = 0
z# = Rnd(-200, 200)
w# = Rnd(1, 10)
h# = Rnd(1, 10)
d# = Rnd(1, 10)
geom = ODE_dCreateBox(space, w, h, d, 1)
ODE_dGeomSetPosition geom, x, y, z
; ODE_dGeomSetRotation(geom, 0, 0, 20)
mesh = CreateCube()
PositionEntity mesh, x, y, z
; RotateEntity mesh, 0, 0, 20
ScaleMesh mesh, .5, .5, .5
ScaleEntity mesh, w, h, d
Next
;-----------------------------------
Global cbody%[5], joint%[4], box%[1], sphere%[4]
;----------- Create CAR -------------
.car
;Car body
cbody[0] = ODE_dBodyCreate()
ODE_dBodySetPosition(cbody[0], 0, STARTZ, 0)
box[0] = ODE_dCreateBox(0, LENGTH, HEIGHT, WIDTH, CMASS)
ODE_dGeomSetBody(box[0], cbody[0])
g.TODEGeom = New TODEGeom
g\body = cbody[0]
g\geom = box[0]
g\mesh = CreateCube();LoadMesh("44.3ds");
EntityColor g\mesh, 255, 255, 0
CAR = g\mesh
ScaleMesh g\mesh, .08, .24, .08;1,1,1
;EntityAlpha g\mesh,.3
ScaleEntity g\mesh, LENGTH, HEIGHT, WIDTH
RotateMesh g\mesh, 0,-90,0 ;\\
ODE_dBodySetMass(cbody[0], CMASS)
ODE_dBodyTranslateMass(cbody[0], 0, -1, 0)
;Car wheels
For i = 1 To 4
cbody[i] = ODE_dBodyCreate()
ODE_dBodySetRotation(cbody[i], 90, 0, 0)
sphere[i - 1] = ODE_dCreateSphere(0, RADIUS, WMASS)
ODE_dGeomSetBody(sphere[i - 1], cbody[i])
g.TODEGeom = New TODEGeom
g\body = cbody[i]
g\geom = sphere[i - 1]
g\mesh = CreateCylinder(16)
ScaleEntity g\mesh, RADIUS, RADIUS * .2, RADIUS
EntityColor g\mesh, 255, 255, 100
ODE_dBodySetMass(cbody[i], WMASS)
Next
ODE_dBodySetPosition (cbody[1],1.3*LENGTH,-1.8 + STARTZ-HEIGHT*0.5,WIDTH*0.05);
ODE_dBodySetPosition (cbody[2],1.3*LENGTH,-1.8 + STARTZ-HEIGHT*0.5,-WIDTH*0.05);
ODE_dBodySetPosition (cbody[3],-0.3*LENGTH,-1.8 + STARTZ-HEIGHT*0.5, WIDTH*0.5);
ODE_dBodySetPosition (cbody[4],-0.3*LENGTH,-1.8 + STARTZ-HEIGHT*0.5,-WIDTH*0.5);
For i = 0 To 3
joint[i] = ODE_dJointCreateHinge2()
ODE_dJointAttach(joint[i], cbody[0], cbody[i + 1])
ODE_dJointSetHinge2Anchor(joint[i], ODE_dBodyGetPositionX(cbody[i + 1]), ODE_dBodyGetPositionY(cbody[i + 1]), ODE_dBodyGetPositionZ(cbody[i + 1]))
ODE_dJointSetHinge2Axis1 (joint[i],0,1,0)
ODE_dJointSetHinge2Axis2 (joint[i],0,0,1)
Next
For i = 0 To 3
; ODE_dJointSetHinge2Param(joint[i], dParamSuspensionERP, .3)
; ODE_dJointSetHinge2Param(joint[i], dParamSuspensionCFM, .065)
ODE_dJointSetHinge2Param(joint[i], dParamSuspensionERP, .65)
ODE_dJointSetHinge2Param(joint[i], dParamSuspensionCFM, .1)
Next
For i = 2 To 3
ODE_dJointSetHinge2Param(joint[i],dParamLoStop,0)
ODE_dJointSetHinge2Param(joint[i],dParamHiStop,0)
Next
ODE_dBodySetPosition cbody[0], 0, 5, 0
car_space% = ODE_dSimpleSpaceCreate(space)
ODE_dSpaceAdd(car_space, box[0])
For i = 1 To 4
ODE_dSpaceAdd(car_space, sphere[i - 1])
Next
speed# = 0.1
;------------------------------------------
ODE_UpdateGeoms()
;-----------------------------------
gameFPS = 50.0
framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod
h=0
t=0
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
.physics
;-------------- Update car physics ------------
;
If KeyDown(200)
H#=H#+.1
ElseIf KeyDown(208)
H#=H#-.1
EndIf
If KeyDown(17)
Tn#=Tn#+1
ElseIf KeyDown(31)
Tn#=Tn#-1
EndIf
; If Abs(speed) > .01
; speed# = speed# * .95
; EndIf
If KeyDown(203)
If steer# < .90 steer# = steer# + .04
YawTorque#=YawTorque#-.1
ElseIf KeyDown(205)
If steer# > -.90 steer# = steer# - .04
YawTorque#=YawTorque#+.1
Else
steer# = steer# * .95
EndIf
ODE_dBodyAddRelForce cbody[0], H#, 0, 0;!!!!!!!!!!!!!!
ODE_dBodyAddForce cbody[0], 0,Tn#, 0;!!!!!!!!!!!!!!
ODE_dBodyAddRelTorque (cbody[0], 0,YawTorque,0); ODE_dBodyAddRelForceAtPos (cbody[0], -10,0,0, 0,0,-10)
; t=Abs(t)-.001
; H=Abs(H)-.001
For i = 0 To 3
ODE_dJointSetHinge2Param(joint[i],dParamVel2, 0);speed# * 12
ODE_dJointSetHinge2Param(joint[i],dParamFMax2, 20.0)
ODE_dJointSetHinge2Param(joint[i],dParamFudgeFactor2, 0.1);\\
Next
v# = steer# - ODE_dJointGetHinge2Angle1(joint[0])
v = v * 11.0
v1# = steer# - ODE_dJointGetHinge2Angle1(joint[1])
v1 = v1 * 11.0
ODE_dJointSetHinge2Param (joint[0],dParamVel,v)
ODE_dJointSetHinge2Param (joint[0],dParamFMax, 50)
ODE_dJointSetHinge2Param (joint[0],dParamLoStop,-0.95)
ODE_dJointSetHinge2Param (joint[0],dParamHiStop,0.95)
ODE_dJointSetHinge2Param (joint[1],dParamVel,v1)
ODE_dJointSetHinge2Param (joint[1],dParamFMax, 50)
ODE_dJointSetHinge2Param (joint[1],dParamLoStop,-0.95)
ODE_dJointSetHinge2Param (joint[1],dParamHiStop,0.95)
x1# = EntityX(PV, 1)
y1# = EntityY(PV, 1)
z1# = EntityZ(PV, 1)
x2# = EntityX(car, 1)
y2# = EntityY(car, 1)
z2# = EntityZ(car, 1)
; TranslateEntity PV, (x2 - x1) * .1, (y2 - y1) * .1, (z2 - z1) * .1, 1
; xd# = EntityYaw(car, 1) - EntityYaw(PV, 1)
; If Abs(xd) > 180
; xd = (360 - Abs(xd)) * (-Sgn(xd))
; EndIf
;
; TurnEntity PV, 0, xd * .1, 0
PointEntity cam, car
;------------ Update physics world ----------
ODE_dWorldQuickStep 0.1
;---------- Fix car angular momuntum --------
For i = 1 To 4
ax# = ODE_dBodyGetAngularVelX(cbody[i]) * .998
ay# = ODE_dBodyGetAngularVelY(cbody[i]) * .9
az# = ODE_dBodyGetAngularVelZ(cbody[i]) * .998
ODE_dBodySetAngularVel(cbody[i], ax#, ay#, az#)
Next
ax# = ODE_dBodyGetAngularVelX(cbody[0]) * .8
ay# = ODE_dBodyGetAngularVelY(cbody[0]) * .9
az# = ODE_dBodyGetAngularVelZ(cbody[0]) * .8
ODE_dBodySetAngularVel(cbody[0], ax#, ay#, az#)
Next
;---- Set geometry position and rotation ----
ODE_UpdateGeoms()
;--------------------------------------------
RenderWorld frameTween#
; FPS counter
fr = fr + 1
If MilliSecs() - frTime >= 1000
fps = fr
fr = 0
frTime = MilliSecs()
EndIf
Color 255, 255, 255
Text 5, 5, "FPS " + fps
Text 5, 15, "T " + T
Text 5, 580, "Space - Add Force and Torque"
Flip 0
Wend
ODE_dCloseODE()
End
|