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
whenclicked
foreverifup key pressedchange y by1iftouchingmaze ▼change y by-1ifdown key pressedchange y by-1iftouchingmaze ▼change y by1ifright key pressedchange x by1iftouchingmaze ▼change x by-1ifleft key pressedchange x by-1iftouchingmaze ▼change x by1
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:
whenclicked
foreverifkeyup arrow ▼pressed?change y by1iftouchingmaze ▼?change y by-1ifkeydown arrow ▼pressed?change y by-1iftouchingmaze ▼?change y by1ifkeyright arrow ▼pressed?change x by1iftouchingmaze ▼?change x by-1ifkeyleft arrow ▼pressed?change x by-1iftouchingmaze ▼?change x by1
Fixed blocks.
If you want velocity:
whenclicked
setx-vel ▼to0sety-vel ▼to0setaccel ▼towhatever number you wantforeverifkeyup arrow ▼pressed?changey-vel ▼byaccelifkeydown arrow ▼pressed?changey-vel ▼by-1*accelifkeyright arrow ▼pressed?changex-vel ▼byaccelifkeyleft arrow ▼pressed?changex-vel ▼by-1*accelsetx-vel ▼to0.98*x-velsety-vel ▼to0.98*y-velchange x byx-veliftouchingwall ▼?change x by-1*x-velsetx-vel ▼to-0.5*x-velchange y byy-veliftouchingwall ▼?change y by-1*y-velsety-vel ▼to-0.5*y-vel
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.
whenclicked
foreverchange x byx-speediftouchingwall ▼?setx-speed ▼to-1*x-speedchange x byx-speedchange y byy-speediftouchingwall ▼?sety-speed ▼to-1*y-speedchange y byy-speed
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