How to Make a Physics Engine

Материал из Поле цифровой дидактики

This tutorial shows how to make a simple physics engine where the sprite is not controlled by the arrow keys, as in Platformers, but rather interacts like dropping and pushing the Sprite in real life would.

Make Variables

First make the following Variables 'for this sprite'

XVelocityThevelocityofthespriteontheXaxisYVelocityThevelocityofthespriteontheYaxisDirectionThespeedatwhichthespriteisturningSlopeUsedforslopedetectionincollisions

Next make two sprites, one will be the one that uses the physics, and the other will be the one will be the ground that the sprite interacts with. In this tutorial, all the scripts are used in the main sprite and the ground sprite is called 'ground'.

Collision Detection

First, collision detection needs to be made. The collision detection that will be used is similar to ones used in platformers, however, this script does not use any controls to move the player.

definePhysicsfrictionstiffnessRunwithoutscreenrefreshifnottouchingground?thenchangeYvelocityby-1endchangeybyYvelocityiftouchingground?thenrepeatabsofceilingofYvelocitychangeybyabsofYvelocity/Yvelocity*-1endendsetYvelocityto1changexbyXVelocityiftouchingground?thensetslopeto0repeatuntilslope=-8ornottouchingground?changeslopeby-1changeyby1endifslope=-8thenchangeybysloperepeatabsofceilingofXVelocitychangexbyabsofXVelocity/xVelocity*-1endsetXVelocityto0endend

Turning When Hitting a Slope

This script will make the sprite fall toward the ground and not go through it, however, this does not give a realistic physics look because the sprite does not turn or move on slopes. To add turning and sliding down slopes, another custom block needs to be made, called Turn:

defineTurnRunwithoutscreenrfefreshchangexby10Movetotherightanddetectifstilltouchingtheground,ifnot,turnandslidetotherightalittlebit.ifnottouchingground?thenchangeDirectionby3changeXVelocityby.3endchangexby-20-20thistimebecauseitisnowneededtoundothemovementfrommovingtotherightandthenmovetotheleft,sotheequivalentofmovingtothelefttwice.ifnottouchingground?thenDotheoppositethistime.changeDirectionby-3changeXVelocityby-.3endchangexby10turn2degreesifnottouchingground?thenchangeDirectionby1endturn4degreesifnottouchingground?thenchangeDirectionby-1endturn12degreesifnottouchingground?thenchangeDirectionby2endturn20degreesifnottouchingground?thenchangeDirectionby-2endturn10degrees

Now that the turning custom block has been created, it needs to be used:

definePhysicsfrictionstiffnessRunwithoutscreenrefreshifnottouchingground?thenchangeYvelocityby-1endchangeybyYvelocityiftouchingground?thenrepeatabsofceilingofYvelocitychangeybyabsofYvelocity/Yvelocity*-1endendsetYvelocityto1changexbyXVelocityiftouchingground?thensetslopeto0repeatuntilslope=-8ornottouchingground?changeslopeby-1changeyby1endifslope=-8thenchangeybysloperepeatabsofceilingofXVelocitychangexbyabsofXVelocity/xVelocity*-1endsetXVelocityto0endendturncwDirectiondegreessetDirectiontoDirection*stiffnesssetXVelocitytoXvelocity*frictionTurn

Implementing Code

The turning and collision detection has been created, so the only thing left to do is to use it.

whenclickedgotox:0y:0Wherethespritewillstart(coordinatescanbedifferent)setDirectionto0SettingvariablestoavoidbugssetXVelocityto0setYVelocityto0foreverusingourcode!Physics0.95.6...Othercodecanbeplacedinhereforthecontrolsofthesprite,e.g.allowittobedragged,movedbythearrowkeys,etc.end

Final Code

Шаблон:Note Here is the final code for the physics engine:

definePhysicsfrictionstiffnessRunwithoutscreenrefreshifnottouchingground?thenchangeYvelocityby-1endchangeybyYvelocityiftouchingground?thenrepeatabsofceilingofYvelocitychangeybyabsofYvelocity/Yvelocity*-1endendsetYvelocityto1changexbyXVelocityiftouchingground?thensetslopeto0repeatuntilslope=-8ornottouchingground?changeslopeby-1changeyby1endifslope=-8thenchangeybysloperepeatabsofceilingofXVelocitychangexbyabsofXVelocity/xVelocity*-1endsetXVelocityto0endendturncwDirectiondegreessetDirectiontoDirection*stiffnesssetXVelocitytoXvelocity*frictionTurndefineTurnchangexby10Movetotherightanddetectifstilltouchingtheground,ifnot,turnandslidetotherightalittlebit.ifnottouchingground?thenchangeDirectionby3changeXVelocityby.3endchangexby-20-20thistimebecauseitisnowneededtoundothemovementfrommovingtotherightandthenmovetotheleft,sotheequivalentofmovingtothelefttwice.ifnottouchingground?thenDotheoppositethistime.changeDirectionby-3changeXVelocityby-.3endchangexby10turn2degreesifnottouchingground?thenchangeDirectionby1endturn4degreesifnottouchingground?thenchangeDirectionby-1endturn12degreesifnottouchingground?thenchangeDirectionby2endturn20degreesifnottouchingground?thenchangeDirectionby-2endturn10degreeswhenclickedgotox:0y:0Wherethespritewillstart(coordinatescanbedifferent)setDirectionto0SettingvariablestoavoidbugssetXVelocityto0setYVelocityto0foreverPhysics0.95.6...Othercodecanbeplacedinhereforthecontrolsofthesprite,e.g.allowittobedragged,movedbythearrowkeys,etc.end