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

#1 2012-07-05 14:48:45

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Circle detection

Probably not the best name...  Anyways, I need a script that allows sprite to detect and turn away from a circle if they are touching it...


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#2 2012-07-05 18:02:51

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Circle detection

Can you explain further?

Offline

 

#3 2012-07-05 18:09:39

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: Circle detection

My project bounce engine has circular bouncing.


http://i47.tinypic.com/2iaa73k.png

Offline

 

#4 2012-07-05 20:27:22

sonicgames20
Scratcher
Registered: 2012-06-13
Posts: 100+

Re: Circle detection

Use the following script and make the balls blue:

when gf clicked
forever
 if <touching color [#0000FF]>
  turn cw (180) degrees
   repeat until <touching color [#0000FF]>
    move (10) steps
   end
 end
end


if there is no I in TEAM and there  is no U in TEAM then who's on the team?
I am-http://blocks.scratchr.org/API.php?user=sonicgames20&amp;action=onlineStatus&amp;type=square-green=online grey=offline

Offline

 

#5 2012-07-06 07:24:23

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Circle detection

You can detect a circle by measuring the distance between the object and the circle's center.

Offline

 

#6 2012-07-07 09:38:22

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

How would I do that?  And sonicgames20's script won't work.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#7 2012-07-08 15:26:39

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

bump


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#8 2012-07-08 15:30:50

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

Assuming you have a sprite for a sircle, or a set of coords, you can work out the distance (scratch has a block for sprite distance, but the formula for coords is harder), do this:
sqrt of (playerx - circlex)^2 + (playery - circley)^2
Effectively the Pythagoras theorem. After you have the distance you check if the distance is less than the radius, then simply do your turning script (change direction, bounce off etc)


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#9 2012-07-08 15:32:36

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

Can I see the raw script?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#10 2012-07-08 15:33:12

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Circle detection

Implementation of amercbu's method. circle's center is the circle sprite, and padding is the amount of extra space you want between the circle and the sprite. Circle radius is the radius of the circle.

when gf clicked
forever
if <(distance to [circle's center v]) < (circle's radius)>
change x by (([cos v] of (direction)) * ((distance to [circle's center v]) + (padding)))
change y by (([sin v] of (direction)) * ((distance to [circle's center v]) + (padding)))
end
end

Last edited by bobbybee (2012-07-08 15:35:29)


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#11 2012-07-08 15:38:23

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

Testing...


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#12 2012-07-08 15:43:39

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

Wait.  What's padding?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#13 2012-07-08 15:53:37

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Circle detection

When the circle bounces, it needs to know how far it should bounce extra. You can set it to 0 if you want to make the circle a boundary, but if you make it 10 if you want it to bounce 10px automatically.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#14 2012-07-08 15:54:06

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

After it bounces, how far will it move away


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#15 2012-07-08 15:58:10

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

This is too choppy... is there any way to make it smoother?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#16 2012-07-08 16:02:04

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

when gf clicked
forever
if <([distance v] to [circle centre v]) < (radius)>
point in direction ((direction) * (-1)
move (10) steps
a very simple way, velocities would make it smoother


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#17 2012-07-08 16:08:58

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

But how does it point away from the circle?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#18 2012-07-08 16:10:24

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

By point away, do you mean in the opposite direction, or just off to the side


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#19 2012-07-08 16:18:09

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

opposite of the circle.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#20 2012-07-08 16:26:11

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

point in direction ((direction) * (-1))


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#21 2012-07-08 23:53:52

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Circle detection

You can also point towards the center of the circle and turn 180 degrees for a roughly approximate bounce.  The actual physics are a bit more complicated (although I can explain them, if you want).

Offline

 

#22 2012-07-09 00:01:36

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Circle detection

amcerbu wrote:

You can also point towards the center of the circle and turn 180 degrees for a roughly approximate bounce.  The actual physics are a bit more complicated (although I can explain them, if you want).

Well you could do that with a bit more complexity:
1) store your direction in variable (old)
2) point towards the center of the circle
3) turn (direction - old) degrees

That should work for a pretty accurate bounce.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#23 2012-07-09 08:43:28

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Circle detection

^^ Yeah, that's the bounce method that I used for collision with circles in one of my old (and failed) pinball projects:

http://scratch.mit.edu/projects/amcerbu/1004827

Last edited by amcerbu (2012-07-09 08:43:50)

Offline

 

#24 2012-07-09 13:48:23

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Circle detection

There was one SDS project with circle bouncing.  What was that script?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#25 2012-07-09 14:09:28

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Circle detection

If we don't know the project, we can't say. Just try some of the ones posted and see if they're what you want


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

Board footer