Physics Engines

A physics engine is a scripting method used for creating the physics, or movement, in a project. This movement can include jumping, side-motion, wall-jumping, and more. Physics engines are commonly designed once and used for many projects or borrowed by other users to save the time of recreating one. This article contains an example physics engine which can be used in a Scratch project.

Warning Note: This physics engine is not the only possible one to create.

Example

This example[1] uses only one script. It requires only one costume and contains the ability for a sprite to:

  • move horizontally on tilted and flat surfaces
  • jump
  • collide with a specific color (which can be modified to be a sprite)

The following script would be placed into the sprite which performs the physics.

when gf clicked
set [x velocity v] to (0) //sets the horizontal speed to "0"
set [y velocity v] to (0) //sets the vertical speed to "0"
forever
if <key [right arrow v] pressed> then //when you intend for the sprite to move right
change [x velocity v] by (1) //"x velocity" is the horizontal speed, and changing it by "1" makes the sprite move more right
end
if <key [left arrow v] pressed> then //when you intend for the sprite to move left
change [x velocity v] by (-1) //makes the sprite move more left
end
set [x velocity v] to ((x velocity) * (0.9)) //for a gradual slowdown and to prevent the speed from reaching too high
change x by (x velocity) //the actual, physical movement
if <touching color [#1B2BE0]> then //if colliding with either a ramp or wall
change y by (([abs v] of (x velocity)) + (1)) //the faster you move, the more velocity it has to go up a slope
if <touching color [#1B2BE0]> then //if the slope is too steep
change y by ((0) - (([abs v] of (x velocity)) + (1))) //go back down it
change x by ((0) - (x velocity)) //go to the previous x position at the bottom of the slope
set [x velocity v] to ((x velocity) / (2)) //slow down after collision with wall or steep slope
end
end
if <key [up arrow v] pressed> then
change y by (-1) //truly, the sprite is 1 pixel above the platform, meaning it has to move down 1 to check if it's "on it"
if <touching color [#1B2BE0]> then //if on the platform
set [y velocity v] to (10) //gives the speed needed to jump
end
change y by (1) //go back up 1 pixel since the sprite went down 1 before to check if it was on a platform
end
if <(y velocity) < (3)> then //when reaching the top of a jump or falling downward
change y by (-1) //to check if on the platform
if <not <touching color [#1B2BE0]>> then //if not on the platform
change [y velocity v] by (-1) //gravity exerting a faster downward force
end
change y by (1) //because before the sprite moved down 1 pixel, and now it must go back in place
end
set [y velocity v] to ((y velocity) * (.9)) //for gradual movement change, as in realistic physics
change y by (y velocity) //the previous scripts set the speed, but now the sprite physically moves vertically
if <touching color [#1B2BE0]> then //if colliding with a floor or ceiling
change y by ((0) - (y velocity)) //reverses the direction
set [y velocity v] to ((y velocity) / (2.5)) //greatly reduces vertical speed after collision to the sprite nearly stops

There is a bug where after you jump, if there is a wall too close to the sprite, then there is some velocity "left over" and the sprite jumps up and down quickly. It can be solved by going to the edge of the platform so the sprite goes down slightly and then moving it back up on the platform.

See Also

References

  1. #p1190320 https://scratcharchive.asun.co/forums/viewtopic.php?id=89988#p1190320
  • This page was last modified on 21 August 2016, at 17:22.