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

#1 2012-07-04 19:44:05

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Formula for determining if a sprite is withing a circles radius

Hello everybody, I am in the making of a physics based game that uses lists for objects instead of sprites themselves.

I have 3 lists so far, one to determine the radius of a circle, and the other two to determine the x and y coordinates of a circle. I'm just stuck on how to check and see if an object is in the circle, or going to collide with it. is there some sort of formula to determine this?

Offline

 

#2 2012-07-04 20:12:05

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

Re: Formula for determining if a sprite is withing a circles radius

Is there a sprite that is in the center?


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

Offline

 

#3 2012-07-04 20:52:21

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Formula for determining if a sprite is withing a circles radius

No, there is not

Offline

 

#4 2012-07-04 21:39:26

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: Formula for determining if a sprite is withing a circles radius

Determine the center of the circle and use the distance formula.


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#5 2012-07-04 22:16:30

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

Re: Formula for determining if a sprite is withing a circles radius

This only works if the x and y coordinates are the center of the circle

drawing script
go to x: <<item (the item) of [x's v]> - (radius of circle)> y: <<item (the item) of [y's v]> + (radius of circle)>
set [start.x v] to <x position>
set [start.y v] to <y position>
set [end.x v] to  <<item (the item) of [x's v]> + (radius of circle)>
set [end.y v] to <<item (the item) of [y's v]> - (radius of circle)>
repeat <<[abs v] of (start.y)>+<[abs v] of (end.y)>>
set [array adder v] to [ ] //nothing
repeat <<[abs v] of (start.x)>+<[abs v] of (end.x)>>
if <touching color [#FFFFFF]?> //the stage's color
set [array adder v] to <(array adder) join [0]>
else
set [array adder v] to <(array adder) join [1]>
end
change x by (1)
end
set x to (start.x)
change y by (-1)
insert (array adder) at (last v) of [Circle v] //this block of script scans the drawn circle
end
forever  //this block of script senses
if <<<(x position) < (start.x)> and <(x position) > (end.x)>>and <<(y position) < (start.y)> and <(y position) > (end.y)>>>
set [where v] to [outside circle ]
end
if <<<(x position) > (start.x)> and <(x position) < (end.x)>>and <<(y position) > (start.y)> and <(y position) < (end.y)>>>
set [Ray.x v] to (x position)
set [Ray.y v] to (y position)
set [Ray.dir v] to <pick random (0) to (180)> //just pick a random number here
set [counter v] to [0] //set to 1 if you don't want circle walls to count
repeat until <<<letter (Ray.x) of <item (Ray.y) of [Circle v]>>=[1]> or <(counter)=<(radius of Circle)*(2)>>>
change [Ray.x v] by <<[sin v] of (Ray.dir)>*(1)>
change [Ray.y v] by <<[cos v] of (Ray.dir)>*(1)>
change [counter v] by [1]
if <<letter (Ray.x) of <item (Ray.y) of [Circle v]>>=[1]>
set [where v] to [inside circle v] 
end
end
end
Where x position and y position in the scanning script is for the drawing sprite and the x position and the y position in the sensing script is for the object that you're trying to see is in the circle or not.
That script involves a scanning script that I use in many projects, where it will detect something that is not the stage and determine that it is wall, and have the color of the stage be empty space. If you open the "Circle" list, you would see a picture of your circle in 1's and 0's. Then for the sensing, if the object is within bounds of the list, it uses a raycaster trick (which is why it's called Ray.x, Ray.y, and Ray.dir), where it moves 1 step, checks if it's touching a "wall" (1), and continues.
WARNING: offline, this will be very slow, as it requires Flash Player's Turbo Mode, to scan at a reasonable rate. Offline is very slow.

Last edited by TorbyFork234 (2012-07-04 22:28:38)

Offline

 

#6 2012-07-05 09:26:28

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

Re: Formula for determining if a sprite is withing a circles radius

It's simpler than that.

forever
set [distance v] to ([sqrt v] of ((((item (wanted) of [xs v]) - (playerx)) * ((item (wanted) of [xs v]) - (playerx))) + (((item (wanted) of [ys v]) - (playery)) * ((item (wanted) of [ys v]) - (playery)))))
if <(distance) < (item (wanted) of [radii v])>
collide


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

Offline

 

#7 2012-07-05 10:19:29

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Formula for determining if a sprite is withing a circles radius

It depends on what the other sprite is.  If it is a circle too, you find the distance between their respective centers and see if is is less than their combined radii:

distance<(radius1 + radius2)

distance = √(∆x^2+∆y^2)

If the other sprite isn't a circle, we need to know what it is.  Is it a square, rectangle, circle, etc.  or can it only be described by some complex polygon?  If it is the last, it could prove rather difficult.  If it can be accurately described by a circle itself, then the formula above works.

Edit: if speed is an issue, I would square both sides.  I'm not sure if this is true for however Scratch does square roots, but computers in general preform multiplication much faster than square roots.

(∆x^2+∆y^2) < (r1+r2)^2

Last edited by MoreGamesNow (2012-07-05 21:00:32)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#8 2012-07-05 13:23:21

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

Re: Formula for determining if a sprite is withing a circles radius

The math required to find the exact time of a collision is rather complicated.  You can take the (much) easier approach and record a collision whenever circles are intersecting; that is, the distance between them is less than the sum of their radii.  I made a project like this awhile ago that uses circles all of the same size.  Here's the link:

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

Offline

 

#9 2012-07-05 23:09:46

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Formula for determining if a sprite is withing a circles radius

Thanks for all the replies, guys! I tested out all of your ideas, which worked really well, but ended up using zammer990's method. It seemed to be the fastest and easiest to understand. Amcerbu, your were a little bit to complicated for me, but if there is a reason your formulas are more accurate, I would love to use them. Again, thanks for all the help!

Offline

 

Board footer