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

#1 2008-02-14 01:47:08

minnvikings26
Scratcher
Registered: 2008-02-14
Posts: 22

Walking through walls

I created a demo for a stealth game called prison break that I'm making.  The player has scripts on it so that if it walks into certain colors (specifically black which makes up most of the walls) it should turn 180 degrees and move 10 steps so that it appears to be bouncing off of the wall just a little bit.  It works fine if you move against the wall straight on but if you approach the walls at certain angles, you can pass right through them.  I have some ideas about how to fix this but I'd like some suggestions.  I've only been working with Scratch for about a month since my interactive computing class started so I'm not 100% familiar with some of the popular techniques.

Here is a link to the project...   http://scratch.mit.edu/projects/minnvikings26/98972

If you can help me out with this bug, or you find any other bugs, help me out. 
Also, comment on the game instead of on this page, it'll be easier for me to locate the comment that way.

Offline

 

#2 2008-02-14 04:53:30

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

Re: Walking through walls

I've run into this problem a number of times...what is happening is that the motion actions generated from the arrow keys are over-riding the "turn-around and walk away from the wall" command.  The best solution I've been able to come up with is to keep track of the last spot where the player wasn't touching a wall and then send the player back there if a wall is touched.  This gets around the whole "which direction is away from the wall" problem.  If you decide to go with this approach, you will find it easier if you collect all the arrow key scripts into a single loop, so all the motion is being handled in one spot.  Then, use variables to store the last spot where you weren't touching a wall.

I can make you a small demo, if you want.


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

Offline

 

#3 2008-02-14 05:42:12

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

Re: Walking through walls

The problem is often that the "if touching wall" script continues to fire when a turn does not get the sprite away from the wall.

Fix:
In the loop that has "if touching wall, turn around" add "move X steps" immediately after the turn around part.

(Of course, X must be replaced with a number - whatever he default step size of your sprite is should be adequate)

That should get the sprite of the wall.


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

 

#4 2008-02-14 11:02:46

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Walking through walls

I've used Paddle2See's idea, which is very robust: You store the last safe location (x1, y1). Then you move in response to user input. Then you test whether you are touching the wall. If you are, you go back to the last safe location. It's not as smooth but it's pretty foolproof.

Offline

 

#5 2008-02-14 13:32:21

minnvikings26
Scratcher
Registered: 2008-02-14
Posts: 22

Re: Walking through walls

Paddle2See, could you make me a small demo?  I think I know what you mean but I'm not sure.  Thanks. 

Mayhem, your suggestion is what I currently have, the sprite turns 180 degrees and moves 5 steps.

Offline

 

#6 2008-02-14 14:50:02

minnvikings26
Scratcher
Registered: 2008-02-14
Posts: 22

Re: Walking through walls

nvm, i've solved it using your technique

Offline

 

#7 2008-02-18 02:17:12

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Walking through walls

Paddle2See wrote:

The best solution I've been able to come up with is to keep track of the last spot where the player wasn't touching a wall and then send the player back there if a wall is touched.

I agree that's an excellent approach. It only works flawlessly with perfectly symetrical (circular) sprites, though, or if you never rotate the sprite. With rotatable excentrical sprites I prefer something like:

   (in "player" sprite):

   forever
      repeat until touching "wall"
         move ...
      end
      turn ...
      repeat until not touching "wall"
         move ... (in reverse direction)
      end
   end

This will also make for smoother movement, I guess.


Jens Mönig

Offline

 

#8 2008-02-18 06:25:24

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

Re: Walking through walls

Jens wrote:

It only works flawlessly with perfectly symetrical (circular) sprites, though, or if you never rotate the sprite. With rotatable excentrical sprites I prefer something like:

   (in "player" sprite):

   forever
      repeat until touching "wall"
         move ...
      end
      turn ...
      repeat until not touching "wall"
         move ... (in reverse direction)
      end
   end

This will also make for smoother movement, I guess.

The method I put forth will work with non-symetrical and rotating sprites - if your step size is greater than the largest radius your sprite will describe.  In other words, if you step back far enough, you will not be able to contact the wall regardless of the orientation of the sprite.  If necessary, you can keep track of the last safe position two moves old and go back there when you contact a wall.

Alternatively, you can keep track of the sprite orientation as well as it's position and return the sprite to it's old spot and it's old direction after contact with the wall.  This is probably the simplest approach.

I have tried various versions of the approach you suggest; the Repeat until Not Touching Wall approach, and have not had great success with it.  It is usually possible to fool it somehow by either approaching the wall diagonally, backing into the wall or rotating the sprite into the wall - which results in very strange actions.  The strange actions include quick trips through the wall or very long translations along the wall.

If you have an example of a fool-proof routine that uses your logic, I would love to see it.

Last edited by Paddle2See (2008-02-18 06:30:49)


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

Offline

 

#9 2008-02-18 07:43:22

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Walking through walls

Paddle2See wrote:

The method I put forth will work with non-symetrical and rotating sprites - if your step size is greater than the largest radius your sprite will describe.

You're right, that's an excellent solution to the problem I described! You're also right about Scratch's collision detection sometimes behaving tricky and unpredictable, which is especially regrettable when it comes to letting moving sprites bounce off other, or letting a moving sprite bounce off a moving/animated obstacle, because then a "backtracking" mechanism won't help much.


Jens Mönig

Offline

 

Board footer