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 gf clicked
forever
if <up key pressed>
  change y by (1)
if <touching [maze v]>
change y by (-1)
end
end
if <down key pressed>
  change y by (-1)
if <touching [maze v]>
change y by (1)
end
end
if <right key pressed>
  change x by (1)
if <touching [maze v]>
change x by (-1)
end
end
if <left key pressed>
  change x by (-1)
if <touching [maze v]>
change x by (1)
end
end
end

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 gf clicked
forever
if <key [up arrow v] pressed?>
  change y by (1)
if <touching [maze v]?>
change y by (-1)
end
end
if <key [down arrow v] pressed?>
  change y by (-1)
if <touching [maze v]?>
change y by (1)
end
end
if <key [right arrow v] pressed?>
change x by (1)
if <touching [maze v]?>
change x by (-1)
end
end
if <key [left arrow v] pressed?>
  change x by (-1)
if <touching [maze v]?>
change x by (1)
end
end
end

Fixed blocks.

If you want velocity:

when gf clicked
set [x-vel v] to (0)
set [y-vel v] to (0)
set [accel v] to [whatever number you want]
forever
if<key [up arrow v] pressed?>
change [y-vel v] by (accel)
end
if<key [down arrow v] pressed?>
change [y-vel v] by ((-1)*(accel))
end
if<key [right arrow v] pressed?>
change [x-vel v] by (accel)
end
if<key [left arrow v] pressed?>
change [x-vel v] by ((-1)*(accel))
end
set [x-vel v] to ((0.98)*(x-vel))
set [y-vel v] to ((0.98)*(y-vel))
change x by (x-vel)
if<touching [wall v]?>
change x by ((-1)*(x-vel))
set [x-vel v] to ((-0.5)*(x-vel))
end
change y by (y-vel)
if<touching [wall v]?>
change y by ((-1)*(y-vel))
set [y-vel v] to ((-0.5)*(y-vel))
end


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 gf clicked
forever
change x by (x-speed)
if<touching [wall v]?>
set [x-speed v] to ((-1)*(x-speed))
change x by (x-speed)
end
change y by (y-speed)
if<touching [wall v]?>
set [y-speed v] to ((-1)*(y-speed))
change y by (y-speed)
end

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