Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Physics engine
- -SuperLazyAnimator-
-
16 posts
Physics engine
I am trying to create my own physics engine but I am stuck on making a jumping function
I need a jumping function that has velocity and looks smooth
I need a jumping function that has velocity and looks smooth
when green flag clicked
go to x: (0) y: (-80)
forever
if <key [w] pressed?> then
change y by (30)
repeat until <(y position) = [-80]>
change y by (-1)
end
end
end
- trolley_explodes
-
100+ posts
Physics engine
Here's a physics thing for you. The variables at the top are the ones you need. You'll also need a custom block, listed below the variables. I am trying to create my own physics engine but I am stuck on making a jumping function
I need a jumping function that has velocity and looks smoothwhen green flag clicked
go to x: (0) y: (-80)
forever
if <key (w v) pressed?> then
change y by (30)
repeat until <(y position) = [-80]>
change y by (-1)
end
end
end
(xvel)
(yvel)
(reversestep)
(tries)
Physics () () () () () <>:: custom blocks // Run without screen refresh.
when gf clicked
set [yvel v] to (0)
set [xvel v] to (0)
go to x: (starting x position) y: (starting y position)
forever
Physics (1) (-0.3) (7) (12) (-1) <(y position) < (160)>:: custom blocks
end
define Physics (accel) (decel) (speed cap) (jump force) (gravity) <dead?>
set [tries v] to (0)
change x by (xvel)
repeat until <<not <touching (Level v)>> or <(tries) = (100)>>
change x by ((0) -(xvel))
change [tries v] by (1) // Without the tries variable, you can sometimes get stuck in the level. Since it's running without screen refresh, this causes extreme lag.
end
if <<<key (left arrow v) pressed?> or <key (a v) pressed?>> or <<key (left arrow v) pressed?> or <key (a v) pressed?>>> then
if <<key (right arrow v) pressed?> or <key (d v) pressed?>> then
if <(xvel) < (speed cap)> then
change [xvel v] by (accel)
end
end
if <<key (left arrow v) pressed?> or <key (a v) pressed?>> then
if <([abs v] of (xvel):: operators) < ([abs v] of (speed cap):: operators)> then
change [xvel v] by ((0) - (accel))
end
end
else
change [xvel v] by ((xvel) * (decel))
end
if <<<key (up arrow v) pressed?> or <key (space v) pressed?>> or <key (w v) pressed?>> then
change y by (-1)
if <touching (Level v)?> then
set [yvel v] to (jump force)
end
change y by (1)
end
change y by (yvel)
if <touching (Level v)?> then
if <(yvel) < (0)> then
set [reversestep v] to (1)
else
set [reversestep v] to (-1)
end
repeat until <not <touching (Level v)?>>
change y by (reversestep)
end
else
change [yvel v] by (gravity)
end
if <dead?> then
broadcast (dead v)
stop [all v]
end
Last edited by trolley_explodes (Yesterday 18:17:52)
- Discussion Forums
- » Help with Scripts
-
» Physics engine