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
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.
Offline
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)
Offline
Here is what you need:
http://scratch.mit.edu/projects/fg123_tests/2637559
Offline
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
Offline
Overly complicated.
He wants the code for bouncing off walls diagonally, not velocity and movement.
fg123 wrote:
Here is what you need:
http://scratch.mit.edu/projects/fg123_tests/2637559
Offline
fg123 wrote:
Overly complicated.
He wants the code for bouncing off walls diagonally, not velocity and movement.
![]()
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)
Offline
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