|
body_a - first physics body body_b - second physics body; must be a different body in the same world x# - world X of the anchor point the bodies pivot around y# - world Y of the anchor z# - world Z of the anchor |
|
Connects two bodies at a point they can swivel around freely. A ball-and-socket joint: the bodies are pinned together at the world-space anchor but rotation is completely free in every direction. That is a ragdoll's shoulder or hip, a link in a swinging chain, a hanging lantern that can sway any way the wind pushes it. Ball joints have no limits or motor - PhysicsJointLimits and PhysicsJointMotor raise a runtime error for them. If you need controlled rotation about a single axis, use CreatePhysicsHingeJoint instead. The anchor is captured at creation time, so position both bodies first. Free with FreePhysicsJoint; freeing either body removes the joint automatically. Use PhysicsBallJointConeLimit, PhysicsBallJointTwistLimits, PhysicsBallJointSpring, PhysicsBallJointTarget and PhysicsBallJointMotor for angular constraints/drives. PhysicsJointFrame defines body-local reference axes. Ball creation retains identity local rotations; differently rotated bodies need not start at zero relative joint rotation. See also: CreatePhysicsHingeJoint, CreatePhysicsSliderJoint, CreatePhysicsDistanceJoint, CreatePhysicsFixedJoint, FreePhysicsJoint. |
; CreatePhysicsBallJoint Example
; ------------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2
SeedRnd MilliSecs()
camera=CreateCamera()
PositionEntity camera,0,3,-8
light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50
; Independent Box3D world with Earth-like gravity
world=CreatePhysicsWorld()
PhysicsGravity world,0,-9.81,0
; Blue static anchor block near the top of the scene
anchor=CreateCube()
ScaleEntity anchor,0.25,0.25,0.25
PositionEntity anchor,0,4,0,True
EntityColor anchor,80,160,255
anchor_body=CreatePhysicsBody(world,anchor,PHYSICS_STATIC)
anchor_shape=PhysicsBodyBox(anchor_body,0.5,0.5,0.5,0)
; Orange pendulum arm hanging below the anchor
arm=CreateCube()
ScaleEntity arm,0.25,1,0.25
PositionEntity arm,0,2.75,0,True
EntityColor arm,255,120,70
arm_body=CreatePhysicsBody(world,arm,PHYSICS_DYNAMIC)
arm_shape=PhysicsBodyBox(arm_body,0.5,2,0.5,1)
; A ball joint pins the arm to the anchor point but leaves rotation free
joint=CreatePhysicsBallJoint(anchor_body,arm_body,0,3.75,0)
; Start the pendulum swinging
PhysicsBodyImpulse arm_body,1.2,0,0.8
While Not KeyDown(1)
; Space pushes the arm in a random direction - it swings in all planes
If KeyHit(57) Then PhysicsBodyImpulse arm_body,Rnd(-1.5,1.5),0,Rnd(-1.5,1.5)
; Advance the Box3D simulation by one 60 Hz step
StepPhysics world,1.0/60.0,4
; Arrow keys move the camera
If KeyDown(200) Then MoveEntity camera,0,0,0.1
If KeyDown(208) Then MoveEntity camera,0,0,-0.1
If KeyDown(203) Then TurnEntity camera,0,1,0
If KeyDown(205) Then TurnEntity camera,0,-1,0
RenderWorld
Text 0,0,"Arrow keys: move camera Space: push pendulum Esc: exit"
Text 0,20,"The ball joint lets the arm swing freely in every direction"
Flip
Wend
FreePhysicsWorld world
FreeEntity arm
FreeEntity anchor
End