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

#1 2008-02-09 12:47:02

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Scripts in blocks

I am wondering, what formula could I use to simulate the block that says "if touching edge, bounce"?


http://i.imgur.com/WBkM2QQ.png

Offline

 

#2 2008-02-09 13:11:25

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Scripts in blocks

It's fairly simple, if you are using velocity variables to control your sprites movement.  Then, if it is a left or right edge, just multiply the horizontal velocity by (-1) to change the sign.  If it is a top of bottom edge, multiply the vertical velocity by (-1).


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#3 2008-02-09 16:53:23

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Scripts in blocks

Not with veloctiy. With direction and move _ steps


http://i.imgur.com/WBkM2QQ.png

Offline

 

#4 2008-02-09 17:34:14

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Scripts in blocks

You will need to calculate which edge you are touching - so rather than using "if touching edge" use


If ABS(X)>239
> Point in direction (-1)*(direction)
> Repeat until ABS(X)<240
> >  Move 5     (this part is to stop it from getting stuck)
If ABS(Y)>179
> Point in direction (180)-(direction)
> Repeat until ABS(Y)<180
> >  Move 5     (this part is to stop it from getting stuck)


Enclose all that inside a loop, includeing "Move 5" at the start of the loop.

Example here:

http://scratch.mit.edu/projects/Mayhem/96405


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#5 2008-02-09 18:00:59

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Scripts in blocks

If you are using "move __ steps" then the bounce is just a change of direction.
If you are bouncing off a vertical edge, then the direction change is
    set direction to -direction
If you are bouncing off a horizontal edge, then the direction change is
   set direction to 180-direction

Offline

 

#6 2008-02-09 19:30:37

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Scripts in blocks

Thanks guys,


http://i.imgur.com/WBkM2QQ.png

Offline

 

#7 2008-02-09 19:34:34

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Scripts in blocks

Honestly I was going to use it so if a ball was to touch the side of a rectangle it would bounce in the correct direction rather than pick random -40 to 40. :@


http://i.imgur.com/WBkM2QQ.png

Offline

 

#8 2008-02-09 19:44:20

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Scripts in blocks

My code can easily be adaped for that by changing the values that X and Y are checked against.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#9 2008-02-10 16:05:59

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Scripts in blocks

I thought so.


http://i.imgur.com/WBkM2QQ.png

Offline

 

Board footer