Pages: 1
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
Is there a sprite that is in the center?
Offline
Determine the center of the circle and use the distance formula.
Offline
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 endWhere 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.
Last edited by TorbyFork234 (2012-07-04 22:28:38)
Offline
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
Offline
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)
Offline
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
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
Pages: 1