Physics Engine
How do I start the physics engine?
Add the {"func":"registerFlagTrigger"}
code block to the Stage’s code and attach the {"func":"blockPhysicsStart"}
block right below it.
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsStart","values":[],"containers":[]}}
Add to my Backpack
How do I make gravity?
Use the {"func":"blockPhysicsSetGravity"}
block. Use 0 as the first parameter and adjust the second parameter to affect the strength of gravity.
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsStart","values":[],"containers":[],"next":{"func":"blockPhysicsSetGravity","values":[{"type":"number","value":"5"},{"type":"number","value":"10"}],"containers":[]}}}
Add to my Backpack
What happens if I change gravity?
Changing gravity affects the way objects fall. Gravity on earth has a strength of around 10, so experiment with higher and lower values to see how objects fall slower and faster.
What happens when Actors collide?
With the physics engine, Actors have physical properties and block each other from moving. You can detect collisions with the {"func":"registerSpriteCollision"}
block.
How do I control the edges of an object? (Bounding Box)
Use the code block {"func":"blockPhysicsSetGeometry"}
to control the way edges of Actors interact. You can choose rectangular for box-like edges or circular for ball-like edges.
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsStart","values":[],"containers":[],"next":{"func":"blockPhysicsSetGeometry","values":[{"type":"choice","value":"circular"}],"containers":[]}}}
Add to my Backpack
How do I make platforms that an Actor can stand on?
When you turn the physics engine on and apply gravity, all your Actors will fall until they hit the ground. If you want some Actors do stay put, like platforms, just use the ‘set static’ block and set the parameter to true.
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsStart","values":[],"containers":[],"next":{"func":"blockPhysicsSetStatic","values":[{"type":"boolean","value":"true"}],"containers":[]}}}
Add to my Backpack
How do I give Actors a “push”?
With the physics engine, you can do this 2 ways: 1) apply an impulse or 2) apply a force. You should specify the strength of the push, and have the option to specify the angle you push it at.
Sample Code:
{"func":"registerSpriteTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsApplyForceAngle","values":[{"type":"number","value":"6"},{"type":"number","value":"15"}],"containers":[]}}
Add to my Backpack
How do I shoot a cannonball?
Apply an impulse to the ball at an angle with the {"func":"blockPhysicsApplyImpulseAngle"}
.
Sample Code:
{"func":"registerKeyTrigger","values":[{"type":"choice","value":"space"}],"containers":[],"next":{"func":"blockMotionGoTo","values":[{"type":"number","value":"-200"},{"type":"number","value":"-150"}],"containers":[],"next":{"func":"blockControlWait","values":[{"type":"number","value":"1"}],"containers":[],"next":{"func":"blockPhysicsApplyImpulseAngle","values":[{"type":"number","value":"50"},{"type":"number","value":"45"}],"containers":[]}}}}
Add to my Backpack
How do I make a stacking game?
Start the physics engine, pick an action that will make boxes drop (the user pressing a certain key), make a clone of the object you’re stacking every time this action occurs, and let gravity do the rest!
Sample Code:
{"func":"registerCloned","values":[],"containers":[],"next":{"func":"blockMotionGoTo","values":[{"type":"wrapper","func":"valueOpRandom","values":[{"type":"number","value":"-30"},{"type":"number","value":"30"}]},{"type":"number","value":"150"}],"containers":[],"next":{"func":"blockPhysicsSetRestitution","values":[{"type":"number","value":"1"}],"containers":[],"next":{"func":"blockPhysicsSetGeometry","values":[{"type":"choice","value":"circular"}],"containers":[]}}}}
Add to my Backpack
How do I make a ball more bouncy?
Restitution adjusts the ‘bounciness’ of an Actor. Use the {"func":"blockPhysicsSetRestitution"}
code block to adjust an Actor’s restitution (higher restitution = more bounciness).
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsSetRestitution","values":[{"type":"number","value":"1"}],"containers":[]}}
Add to my Backpack
How do I use friction?
Friction affects how much an Actor will slide across things. Use the {"func":"blockPhysicsSetFriction"}
block to give an Actor friction (more friction = more resistance).
Sample Code:
{"func":"registerFlagTrigger","values":[],"containers":[],"next":{"func":"blockPhysicsSetActive","values":[{"type":"boolean","value":"false"}],"containers":[],"next":{"func":"blockPhysicsSetStatic","values":[{"type":"boolean","value":"false"}],"containers":[],"next":{"func":"blockPhysicsSetGeometry","values":[{"type":"choice","value":"circular"}],"containers":[],"next":{"func":"blockPhysicsSetFriction","values":[{"type":"number","value":"0.9"}],"containers":[]}}}}}
Add to my Backpack
How do I make my Actor jump using physics?
Apply a force or an impulse to an Actor to give it a push or jump. You can specify what angle the Actor jumps at, too.
Sample Code:
{"func":"registerKeyTrigger","values":[{"type":"choice","value":"space"}],"containers":[],"next":{"func":"blockMotionGoTo","values":[{"type":"number","value":"-200"},{"type":"number","value":"-150"}],"containers":[],"next":{"func":"blockControlWait","values":[{"type":"number","value":"1"}],"containers":[],"next":{"func":"blockPhysicsApplyImpulseAngle","values":[{"type":"wrapper","func":"valueVar","name":"Impulse"},{"type":"number","value":"45"}],"containers":[],"next":{"func":"blockVarSet","values":[{"type":"string","value":"Impulse"},{"type":"string","value":"0"}],"containers":[]}}}}}
Add to my Backpack
How do I make a loop de loop?
Make some platforms that loop around and set them to be static. Add a ball or another Actor, set it to be active, give it some friction, and give it some angular velocity to make it move.
- Getting Started
- Welcome to Tynker
- Minecraft Guide
- Tools
- Workshop
- Character Creator
- Level Editor
- Backpack
- Debugger
- Sharing
- LEGO WeDo
- Tips & Tricks
- Video Tutorials
- Working with Actors
- Music and Drawing
- Physics Engine
- Game Design
- Other Topics
- Tech Setup
- Glossary
- Blocks Reference
- Events
- Control
- Motion
- Animation
- Looks
- Sensing
- Sound
- Operator
- Pen
- Physics
- Variables
- Lists
- Functions
- Level Design
- Hardware
- Library Reference
- Platformer
- Top Down