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

#1 2013-03-22 02:31:03

parent2nj
New Scratcher
Registered: 2012-10-21
Posts: 9

Getting stuck on a wall

I'm working on my first scratch project "duel". Two players try to shoot each other while moving around in a maze.  My wall detection logic is very similar to the wiki's, but I've an extra problem.  If the player turns (changes direction) when too close to the wall they can get stuck.  They can get unstuck by turning again.

I think this is because my sprite is rectangular.  At start, it may not touch the wall, but when it changes direction, the longer edge could now overlap the wall.  At that point, I detect sprite is touching wall color and don't let it move.  One work around is to change the shape of the sprite to be a square or a circle.  Any other suggestions?

Thanks!

Offline

 

#2 2013-03-22 19:37:30

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Getting stuck on a wall

parent2nj wrote:

I'm working on my first scratch project "duel". Two players try to shoot each other while moving around in a maze.  My wall detection logic is very similar to the wiki's, but I've an extra problem.  If the player turns (changes direction) when too close to the wall they can get stuck.  They can get unstuck by turning again.

I think this is because my sprite is rectangular.  At start, it may not touch the wall, but when it changes direction, the longer edge could now overlap the wall.  At that point, I detect sprite is touching wall color and don't let it move.  One work around is to change the shape of the sprite to be a square or a circle.  Any other suggestions?

Thanks!

Yes, I do have another suggestion. Instead of changing the shape of your character, you could always add in two more colors into your sensor (on the short sides). This way, you can sense when your character is inside of a wall, and from there, change your character's velocity or position to get out of the wall.

Another way you can tackle this is whenever your character sense that it's in a wall, you could make it move left/up the necessary amount of steps to get out of the wall, and if it's still in it, then you could double distance while moving right/down.

I hope that this helps!

With regards,

ErnieParke


http://i46.tinypic.com/35ismmc.png

Offline

 

#3 2013-03-23 01:03:34

parent2nj
New Scratcher
Registered: 2012-10-21
Posts: 9

Re: Getting stuck on a wall

Brilliant, simply brilliant!  I'll have to cogitate on its implementation, but intuitively this makes sense.

Thanks for help!

Offline

 

#4 2013-03-23 01:09:06

parent2nj
New Scratcher
Registered: 2012-10-21
Posts: 9

Re: Getting stuck on a wall

[Thread hijack alert]

And I just noticed from your infinite scroll engine project you can update a project with duplicating it!  Learned two things from you in one day.

[/Thread hijack alert]

Offline

 

#5 2013-03-23 11:48:21

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Getting stuck on a wall

parent2nj wrote:

[Thread hijack alert]

And I just noticed from your infinite scroll engine project you can update a project with duplicating it!  Learned two things from you in one day.

[/Thread hijack alert]

Well that's good! Two birds with one stone. Very nice! Also, I'm glad to have helped.  smile

With regards,

ErnieParke


http://i46.tinypic.com/35ismmc.png

Offline

 

#6 2013-03-24 04:25:22

parent2nj
New Scratcher
Registered: 2012-10-21
Posts: 9

Re: Getting stuck on a wall

I got this working!  With side colors different from each other and knowing the sprite's direction, I knew where the wall was and could re-position accordingly.  The weird thing was that the color detection didn't always work.  I ended up extending the side color  1 pixel into the sprite and a couple of pixels beyond the sprite.  For some reason this makes the color detection consistent.

Now to use that nifty update feature to upload a new version! :-)

Offline

 

#7 2013-03-24 04:33:31

parent2nj
New Scratcher
Registered: 2012-10-21
Posts: 9

Re: Getting stuck on a wall

Forgot to add the fixed version is at:  http://scratch.mit.edu/projects/parent2nj/3195343


parent2nj wrote:

I got this working!  With side colors different from each other and knowing the sprite's direction, I knew where the wall was and could re-position accordingly.  The weird thing was that the color detection didn't always work.  I ended up extending the side color  1 pixel into the sprite and a couple of pixels beyond the sprite.  For some reason this makes the color detection consistent.

Now to use that nifty update feature to upload a new version! :-)

Offline

 

Board footer