Tur:Fizik Motorları

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.

Example
This example uses only one script. It requires only one costume and contains the ability for a sprite to: 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  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  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  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  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  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  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 > 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  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.
 * move horizontally on tilted and flat surfaces
 * jump
 * collide with a specific color (which can be modified to be a sprite)