PhysicsBodyImpulse body,x#,y#,z#
Parameters
|
body - physics-body handle returned by CreatePhysicsBody x# - impulse along the world X axis, in kilogram metres per second y# - impulse along the world Y axis z# - impulse along the world Z axis |
Description
|
Applies an instant impulse to a body's centre of mass. An impulse is a whole push delivered at once: the body's velocity changes immediately by impulse divided by mass. That mass division is what makes explosions feel right - the same blast sends a crate flying and barely rocks a boulder. For an exact velocity change regardless of mass, multiply by PhysicsBodyMass first. Use it for one-shot events - jumps, hits, kicks, blasts - called once per bang, not every frame. A push applied continuously works better as PhysicsBodyForce, which cooperates with the solver's sub-stepping. Applying an impulse wakes a sleeping body, and it adds to the current motion rather than replacing it (unlike PhysicsBodyVelocity). Requires Extended mode. See also: PhysicsBodyForce, PhysicsBodyVelocity, PhysicsBodyMass, PhysicsBodyIsAwake. |
Example
; PhysicsBodyImpulse Example ; -------------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() Const PHYSICS_STATIC=0 Const PHYSICS_DYNAMIC=2 camera=CreateCamera() PositionEntity camera,0,3,-10 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 ground slab ground=CreateCube() ScaleEntity ground,4,0.5,4 PositionEntity ground,0,-0.5,0,True EntityColor ground,80,160,255 ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC) ground_shape=PhysicsBodyBox(ground_body,8,1,8,0) ; Blue ceiling stops big jumps flying out of view ceiling=CreateCube() ScaleEntity ceiling,4,0.5,4 PositionEntity ceiling,0,6.5,0,True EntityColor ceiling,80,160,255 ceiling_body=CreatePhysicsBody(world,ceiling,PHYSICS_STATIC) ceiling_shape=PhysicsBodyBox(ceiling_body,8,1,8,0) ; Orange ball resting on the ground ball=CreateSphere() ScaleEntity ball,0.5,0.5,0.5 PositionEntity ball,0,0.5,0,True EntityColor ball,255,120,70 ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC) ball_shape=PhysicsBodySphere(ball_body,1,1) punch#=3 While Not KeyDown(1) ; [ and ] adjust the impulse strength If KeyHit(26) And punch>1 Then punch=punch-1 If KeyHit(27) And punch<8 Then punch=punch+1 ; Tap Space for a one-shot kick, unlike a sustained PhysicsBodyForce If KeyHit(57) Then PhysicsBodyImpulse ball_body,0,punch,0 ; 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,"Space: kick the ball [ / ]: strength Arrow keys: camera Esc: exit" Text 0,20,"PhysicsBodyImpulse ball,0,"+punch+",0" Text 0,40,"Vertical speed: "+PhysicsBodyVelocityY(ball_body)+" m/s" Flip Wend FreePhysicsWorld world FreeEntity ball FreeEntity ceiling FreeEntity ground End
Index