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

#1 2012-06-24 08:43:59

THEphish
New Scratcher
Registered: 2012-06-23
Posts: 15

Problems with sprites bouncing off walls

Hi, I'm creating a simple maze game at the mo called Planez in which I have sprites bouncing off walls. However, when a sprite goes diagonally at a wall it goes straight through. I wrote an explanation about it in a post called 2 Player Maze Game - Help!!! but as it is in a post originally about something else, I thought maybe making a new post about it would help people find it. If you can't find my post here's a link:
http://scratch.mit.edu/forums/viewtopic.php?pid=1289707#p1289707

Please help me find a solution!

Offline

 

#2 2012-06-25 16:00:10

wahoo77
New Scratcher
Registered: 2012-06-17
Posts: 27

Re: Problems with sprites bouncing off walls

Use this block

if on edge, bounce

Offline

 

#3 2012-06-25 16:20:57

northmeister
Scratcher
Registered: 2011-07-12
Posts: 1000+

Re: Problems with sprites bouncing off walls

wahoo77 wrote:

Use this block

if on edge, bounce

That's to bounce back off the edge of the screen, I think he means bounce it off the maze walls.


http://i48.tinypic.com/5a25g5.png

Offline

 

#4 2012-06-26 16:45:16

pipercubjl
Scratcher
Registered: 2010-05-20
Posts: 73

Re: Problems with sprites bouncing off walls

when clicked
forever
if
up key pressed
change y by
1
if
touching
maze
change y by
-1
if
down key pressed
change y by
-1
if
touching
maze
change y by
1
if
right key pressed
change x by
1
if
touching
maze
change x by
-1
if
left key pressed
change x by
-1
if
touching
maze
change x by
1

Last edited by pipercubjl (2012-06-26 16:47:50)


http://scratch.mit.edu/static/projects/pipercubjl/2621398_med.png  http://scratch.mit.edu/static/projects/pipercubjl/2634368_med.png
        Crypt            Pixel Lands

Offline

 

#5 2012-06-26 17:40:39

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: Problems with sprites bouncing off walls


Hai.

Offline

 

#6 2012-06-26 21:13:27

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

Re: Problems with sprites bouncing off walls

pipercubjl wrote:

when clicked
forever
if
key
up arrow
pressed?
change y by
1
if
touching
maze
?
change y by
-1
if
key
down arrow
pressed?
change y by
-1
if
touching
maze
?
change y by
1
if
key
right arrow
pressed?
change x by
1
if
touching
maze
?
change x by
-1
if
key
left arrow
pressed?
change x by
-1
if
touching
maze
?
change x by
1

Fixed blocks.

If you want velocity:

when clicked
set
x-vel
to
0
set
y-vel
to
0
set
accel
to
whatever number you want
forever
if
key
up arrow
pressed?
change
y-vel
by
accel
if
key
down arrow
pressed?
change
y-vel
by
-1
*
accel
if
key
right arrow
pressed?
change
x-vel
by
accel
if
key
left arrow
pressed?
change
x-vel
by
-1
*
accel
set
x-vel
to
0.98
*
x-vel
set
y-vel
to
0.98
*
y-vel
change x by
x-vel
if
touching
wall
?
change x by
-1
*
x-vel
set
x-vel
to
-0.5
*
x-vel
change y by
y-vel
if
touching
wall
?
change y by
-1
*
y-vel
set
y-vel
to
-0.5
*
y-vel


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

Offline

 

#7 2012-06-27 01:21:09

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: Problems with sprites bouncing off walls

Overly complicated.  tongue  He wants the code for bouncing off walls diagonally, not velocity and movement.  tongue 


fg123 wrote:

Here is what you need:
http://scratch.mit.edu/projects/fg123_tests/2637559


Hai.

Offline

 

#8 2012-06-27 09:09:45

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

Re: Problems with sprites bouncing off walls

fg123 wrote:

Overly complicated.  tongue  He wants the code for bouncing off walls diagonally, not velocity and movement.  tongue

Like pong?  Because the easiest way to have accurate collision like that is the script below.  90 degree walls are nice because the absolute value of the x and y velocities are always constant.

A lot of users simply turn the sprite 180 degrees though, which isn't accurate collision.  It's easier to simply avoid angles all together and use x and y velocities.

when clicked
forever
change x by
x-speed
if
touching
wall
?
set
x-speed
to
-1
*
x-speed
change x by
x-speed
change y by
y-speed
if
touching
wall
?
set
y-speed
to
-1
*
y-speed
change y by
y-speed

Last edited by MoreGamesNow (2012-06-27 09:26:29)


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

Offline

 

#9 2012-06-30 11:35:52

THEphish
New Scratcher
Registered: 2012-06-23
Posts: 15

Re: Problems with sprites bouncing off walls

Thanks MoreGamesNow I used that and it works really well! I've credited you in the project notes. My game is now online at:
http://scratch.mit.edu/projects/THEphish/2645424
It isn't perfect yet, and graphically looks bad, but I'm gonna improve it later. Sorry fg123, that wasn't for me.

Offline

 

Board footer