Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsI always used hitTests and they worked fine, but now in my new physics games I have a hitTestObject for two circles touching, which is obviously shit, as it shows them as touching when they aren't. What's another method? Something really really simple though, as they are just circles and I will not be using this for any other shapes.
Cheers guys :)
Don't use hit test. Duh.
Use extremely basic math to calculate the overlap. Which happens to be the easiest possible form of collision detection.
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
I'll give you a hint:
radius1 + radius2 < distance between centers
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
At 6/16/11 10:22 AM, mooseisloose wrote: I always used hitTests and they worked fine, but now in my new physics games I have a hitTestObject for two circles touching, which is obviously shit
You're making a physics game but don't even know this?
Calculate distance (squared) between centres of circles
Compare to sum of radii of circles (squared)
Redshift: thanks for sticking me along the right lines. This is what I've come up with, it works in my head but when I try it, flash traces it as NaN, I want it to just show the distance for now:
trace (Math.sqrt(elephant1.x - elephant2.x)*(elephant1.x - elephant2.x) + (elephant1.y - elephant2.y)*(elephant1.y - elephant2.y));
UnknownFury: yes, this will be my 3rd physics game (the other two are still waiting for official release from the sponsor), but I use box2d so don't usually have to worry about any of the maths behind it as it's already done.
Got it, nvm. Didn't have the sqrt-ing in brackets:
trace(Math.sqrt((elephant1.x - elephant2.x)*(elephant1.x - elephant2.x) + (elephant1.y - elephant2.y)*(elephant1.y - elephant2.y)));
How would you detect the point where they collide using this method?
Use Paul Bourke as a documentation for these kinds of things:
At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?
Well the normal to the collision plane will be the vector (v) between the 2 centres.
So pick a circle centre, and add on this direction vector with the radius as its length:
centre + v.normalised() * radius
Try out the Collision Detection Kit, works wonders.
At 6/16/11 05:17 PM, SantoNinoDeCebu wrote:At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?Well the normal to the collision plane will be the vector (v) between the 2 centres.
So pick a circle centre, and add on this direction vector with the radius as its length:
centre + v.normalised() * radius
No, because the 2 circles might be intersecting, in which case the point becomes harder to find, as there are 2 intersection points, and you should find their average as the collision point.
Also, do NOT use the Collision Detection Kit. It relies on BitmapData methods which are quite slow, and a lot slower than some simple math equations.
At 6/16/11 07:14 PM, PSvils wrote:At 6/16/11 05:17 PM, SantoNinoDeCebu wrote:No, because the 2 circles might be intersecting, in which case the point becomes harder to find, as there are 2 intersection points, and you should find their average as the collision point.At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?Well the normal to the collision plane will be the vector (v) between the 2 centres.
So pick a circle centre, and add on this direction vector with the radius as its length:
centre + v.normalised() * radius
Also, do NOT use the Collision Detection Kit. It relies on BitmapData methods which are quite slow, and a lot slower than some simple math equations.
I missed the intersection bit for simplicity...
And you probably wanna correct the intersection instead of just averaging or else your circles might get stuck within each other if they don't get repelled far enough.
Correcting intersection between 2 circles is still very simple (of course you're better off preventing it in the first place but that's another matter).
The penetration distance will be, if you think about it: the shortest distance between the 2 centres minus the actual distance between the 2 centres, ideally this would come to 0!
distance = radius1 + radius2 - v.magnitude(). Where the v is the vector between the 2 centres.
Lets say 1 circle was static, so you would want to translate the other one along vector v by this distance, and hey presto you got your single collision point on the surface of both circles.
The method I posted shows the distance, I just run an "if" statement so if that sum < (ball1.width + ball2.width)/2 then theyre touching. i know you guys will probably say how its wrong in some way but it works perfectly for me =D
thanks a lot for all the help guys, it worked perfectly although my flash file went corrupt so now ive gotta start the whole game from scratch :(