This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-05 00:02:38

LiquidMetal
Scratcher
Registered: 2011-06-15
Posts: 500+

Realistic physics in a scrolling environment - Help!

I'm trying to perfect velocity and gravity scripts in a scrolling program.

What it does right now is changes the y position by 1 if you are touching the terrain sprites.  This, however, is not fast enough, where a mountain comes out of the sprite, and then you slowly glide up the side of the mountain. Please help!


This is my currrent script (I couldn't get some parts to work correctly):

when gf clicked
set [bounce? v] to (0)
set [scrollX v] to (0) 
set [xvel v] to (0) 
set [yvel v] to (0) 
set [gravity v] to (0) 
forever
set [xvel v] to <(xvel)*(0.95)>
set [yvel v] to <(yvel)*(0.95)>
if <key [up arrow v] pressed?>
change [yvel v] by (1)
end
if <key [left arrow v] pressed?>
change [xvel v] by (-1)
turn ccw(15) degrees
end
if <key [right arrow v] pressed?>
change [xvel v] by (1)
turn cw(15) degrees
end
if <    <not <([abs v] of <(x position) - (normalx)>) < [11]> >    or < <> or <>    > >
<((scrollX)+(xvel)) < [0]>
<((scrollX)+(xvel)) > ((480)*((z.terrs)-(1)))>
Read the comment >>>// These above go in the empty "or" block 2above.
if < not <([abs v] of ((xpositon) + (xvel))) > [199] > >
change x by (xvel)
end
else
change [scrollX v] by (xvel)
set x to (normalx)
end
change y by ((yvel)-(gravity))
if <not < <touching [Terrain1 v]?> or <touching [Terrain2 v]?> >>
change [gravity v] by (0.1)
if <(gravity) > [1.2]>
set [bounce? v] to (1)
end
else
set [gravity v] to (0)
change y by (1)
if <(bounce) = [1]>
change [yvel v] by ((0.3) * (gravity))
set [bounce v] to (0)
end
end
end
Any suggestions?

Last edited by LiquidMetal (2012-04-05 12:05:27)

Offline

 

#2 2012-04-05 07:08:17

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Realistic physics in a scrolling environment - Help!

I didn't look at your script, but I'm commenting for when it is finished.  Working with slants has always been a challenge.  I do have one question though: by "realistic physics" do you want the sprite, when it hits slanted ground, to bounce at an angle, or can it simply bounce up and down as it would on flat ground?  Or do you not want the sprite to bounce at all and simply land on the ground?


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#3 2012-04-05 09:51:31

LiquidMetal
Scratcher
Registered: 2011-06-15
Posts: 500+

Re: Realistic physics in a scrolling environment - Help!

I want the yvel to bounce.  The xvel value will still be the same, so it will still move right/left as well.

Last edited by LiquidMetal (2012-04-05 09:51:56)

Offline

 

#4 2012-04-05 17:02:36

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Realistic physics in a scrolling environment - Help!

Well, I'm on a computer without Scratch right now, but here's what I have:

You want to check if there is a wall/slant before you move someplace.  Something like

if <key [right arrow v] pressed?>
change x by ((xvel)+(1))
if<touching [terrain v]?>
check if it is a slant or a wall
else
change [xvel v] by [1]
end
change x by ((-1)-(xvel))
end
To check if it is a slant or a wall try something like this:

change y by (3) // move up
if<touching [terrain v]?> // wall
change x by ((-1)-(xvel)) // bounce off wall
set [xvel v] to [0] // kill x velocity
else // slant
change y by (2) // move up slope
end
change y by (-3) // counter act original y
Something like that should do it.

Last edited by MoreGamesNow (2012-04-05 17:02:57)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-04-05 17:46:14

LiquidMetal
Scratcher
Registered: 2011-06-15
Posts: 500+

Re: Realistic physics in a scrolling environment - Help!

I tried that: 

1) it made the sprite slower on a slant
2) when I turned the other direction quickly, I wound up going the wrong way - into the wall. (b/c the x velocity was greater then 1)

Offline

 

#6 2012-04-05 21:26:02

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Realistic physics in a scrolling environment - Help!

Ok, message received.  Still on a non-Scratch machine unfortunately  sad


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#7 2012-04-06 08:36:02

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Realistic physics in a scrolling environment - Help!

Try putting a "change [xvel] by [1]" in the else statement of the slant/wall script.  That should change the slowness.

Maybe replace "set [xvel] to [0]" with "set [xvel] to ( (-1) * (xvel) ) for the second one?

Last edited by MoreGamesNow (2012-04-06 08:36:19)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

Board footer