|
body_a - chassis physics-body handle body_b - wheel physics-body handle in the same world; must differ from body_a x# - world anchor X, metres y# - world anchor Y, metres z# - world anchor Z, metres suspension_x# - world suspension/steering axis X component suspension_y# - world suspension/steering axis Y component suspension_z# - world suspension/steering axis Z component axle_x# - world axle/spin direction X component axle_y# - world axle/spin direction Y component axle_z# - world axle/spin direction Z component |
|
Creates a wheel joint with body A as chassis and body B as wheel. The anchor and both direction vectors are in world space. Local frame A X follows the normalised suspension axis. The axle is projected perpendicular to that axis and normalised to form local frame B Z; both attachment frames initially share the resulting world orientation. This captures zero travel and steering even for differently rotated or parented bodies. Axes must be finite, non-zero and non-parallel; a projected normalised axle length at or below 0.0001 is rejected. Returns a positive joint handle; PhysicsJointType reports 6. Default suspension is enabled at 1 Hz with damping 0.7; travel limits, spin motor and steering are disabled, with zero motor/steering torque caps. The joint must be a wheel joint. Physics angles and angular velocities use radians; entity rotation commands retain degrees. All setters validate every argument before changing native state and wake connected bodies. Getters do not wake bodies or mutate physics. Settings, frame edits and native wake operations are recorded for deterministic physics replay. Freeing either body, its entity or the world invalidates the joint handle. Invalid/stale/wrong-kind handles, mixed worlds, non-finite inputs and unsupported joint families raise runtime errors. PhysicsJointFrame can edit the body-local attachment frames. PhysicsJointForceX/Y/Z and PhysicsJointTorqueX/Y/Z report the last solved reaction on body B; torque excludes the moment of the attachment force. Native overload events are sampled during bias solves, while public reaction getters reflect the final relaxed solve. Motor or steering torque is one contribution to the combined joint reaction. The sample's 32-sided convex tyres use ordinary contact friction, so they can slip and need sufficient drive/steering effort under load. See also: PhysicsWheelJointSuspension, PhysicsWheelJointSpinMotor, PhysicsWheelJointSteering, PhysicsJointFrame. |
; CreatePhysicsWheelJoint Example
; -------------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2
camera=CreateCamera()
light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 60,60,60
; An independent Box3D world with standard gravity
world=CreatePhysicsWorld()
; A big floor with stripes, so movement is easy to see
ground=CreateCube()
ScaleEntity ground,30,0.5,30
PositionEntity ground,0,-0.5,0
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,60,1,60)
For stripe_z=-28 To 28 Step 4
stripe=CreateCube()
ScaleEntity stripe,30,0.01,0.3
PositionEntity stripe,0,0.01,stripe_z
EntityColor stripe,60,120,200
Next
; The chassis
chassis=CreateCube()
ScaleEntity chassis,0.8,0.2,1.4
PositionEntity chassis,0,1,0
EntityColor chassis,255,120,70
chassis_body=CreatePhysicsBody(world,chassis,PHYSICS_DYNAMIC)
chassis_shape=PhysicsBodyBox(chassis_body,1.6,0.4,2.8,20)
; Four wheels on wheel joints: suspension along Y, axles along X
Dim wheel_joints(3)
For i=0 To 3
x#=-1.1+(i Mod 2)*2.2
z#=1-(i/2)*2
wheel=CreateCylinder(16)
ScaleMesh wheel,0.4,0.15,0.4
RotateMesh wheel,0,0,90
PositionEntity wheel,x,0.6,z
EntityColor wheel,40,40,50
wheel_body=CreatePhysicsBody(world,wheel,PHYSICS_DYNAMIC)
wheel_shape=PhysicsBodySphere(wheel_body,0.8)
PhysicsShapeFriction wheel_shape,1
wheel_joints(i)=CreatePhysicsWheelJoint(chassis_body,wheel_body,x,0.6,z,0,1,0,1,0,0)
PhysicsWheelJointSuspension wheel_joints(i),True,4,0.7
If i<2 Then PhysicsWheelJointSteering wheel_joints(i),True,6,0.8,40
Next
front=wheel_joints(0)
springs=True
While Not KeyDown(1)
; Up and Down drive, Left and Right steer, Space brakes
throttle=KeyDown(200)-KeyDown(208)
steer#=(KeyDown(203)-KeyDown(205))*0.4
For i=0 To 3
If KeyDown(57)
PhysicsWheelJointSpinMotor wheel_joints(i),True,0,40
Else
PhysicsWheelJointSpinMotor wheel_joints(i),throttle<>0,throttle*15,20
EndIf
If i<2 Then PhysicsWheelJointSteeringTarget wheel_joints(i),steer
Next
StepPhysics world,1.0/60.0,4
; The camera follows the car, so the arrow keys drive instead
PositionEntity camera,EntityX(chassis),EntityY(chassis)+4,EntityZ(chassis)-8
PointEntity camera,chassis
RenderWorld
Text 0,0,"Up / Down: drive Left / Right: steer Space: brake"
Text 0,20,"The camera follows the car Esc: exit"
Text 0,40,"CreatePhysicsWheelJoint(chassis,wheel,x,y,z,0,1,0,1,0,0)"
Text 0,60,"Each wheel slides on a vertical suspension axis and spins about X"
Flip
Wend
FreePhysicsWorld world
End