From Test-Scratch-Wiki

Translate: - English - Türkçe - 中文 

物理引擎(physics engine)is a 编程方法 used for creating the physics, or movement, in a 专案. 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 Scratch 用户s to save the time of recreating one. This article contains an example physics engine which can be used in a Scratch project.

Note Note: this physics engine is not the only possible one to create

示例

This example[1] uses only one script. It requires only one costume and contains the ability for a 角色 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.

當 @greenflag 被點擊
變數 [x velocity v] 設為 (0) // 将水平(横向)的速度设定成 "0"
變數 [y velocity v] 設為 (0) // 将垂直(直向)的速度设定成 "0"
重複無限次 
  如果 <[向右 v] 鍵被按下?> 那麼 
    變數 [x velocity v] 改變 (1) // "x velocity" is the horizontal speed, and changing it by "1" makes the sprite move more right
  end // when you intend for the sprite to move right
  如果 <[向左 v] 鍵被按下?> 那麼 
    變數 [x velocity v] 改變 (-1) // makes the sprite move more left
  end // when you intend for the sprite to move left
  變數 [x velocity v] 設為 ((x velocity) * (0.9)) // for a gradual slowdown and to prevent the speed from reaching too high
  x 改變 (x velocity) // the actual, physical movement
  如果 <碰到顏色 [#1B2BE0] ?> 那麼 
    y 改變 (([abs v] 數值 (x velocity)) + (1)) // the faster you move, the more velocity it has to go up a slope
    如果 <碰到顏色 [#1B2BE0] ?> 那麼 
      y 改變 ((0) - (([abs v] 數值 (x velocity)) + (1))) // go back down it
      x 改變 ((0) - (x velocity)) // go to the previous x座標 at the bottom of the slope
      變數 [x velocity v] 設為 ((x velocity) / (2)) // slow down after collision with wall or steep slope
    end // if the slope is too steep
  end // if colliding with either a ramp or wall
  如果 <[向上 v] 鍵被按下?> 那麼 
    y 改變 (-1) // truly, the sprite is 1 pixel above the platform, meaning it has to move down 1 to check if it's "on it"
    如果 <碰到顏色 [#1B2BE0] ?> 那麼 
      變數 [y velocity v] 設為 (10) // gives the speed needed to jump
    end // if on the platform
    y 改變 (1) // go back up 1 pixel since the sprite went down 1 before to check if it was on a platform
  end
  如果 <(y velocity) < (3)> 那麼 
    y 改變 (-1) // to check if on the platform
    如果 <<碰到顏色 [#1B2BE0] ?> 不成立> 那麼 
      變數 [y velocity v] 改變 (-1) // gravity exerting a faster downward force
    end // if not on the platform
    y 改變 (1) // because before the sprite moved down 1 pixel, and now it must go back in place
  end // when reaching the top of a jump or falling downward
  變數 [y velocity v] 設為 ((y velocity) * (.9)) // for gradual movement change, as in realistic physics
  y 改變 (y velocity) // the previous scripts set the speed, but now the sprite physically moves vertically
  如果 <碰到顏色 [#1B2BE0] ?> 那麼 
    y 改變 ((0) - (y velocity)) // reverses the direction
    變數 [y velocity v] 設為 ((y velocity) / (2.5)) // greatly reduces vertical speed after collision to the sprite nearly stops
  end // if colliding with a floor or ceiling
end

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.

参见

参考

  1. http://scratchforums.blob8108.net/forums/viewtopic.php?id=89988#p1190320