This site requires JavaScript, please enable it in your browser!
Greenfoot back
mattrayner
mattrayner wrote ...

2013/2/25

Doing 'real' hit testing on circles

mattrayner mattrayner

2013/2/25

#
Hi guys, I'm trying to do wit testing on objects that I don't know the width or height or in advance (data driven). I've got code that randomly places generated 'balls' onto the screen, I was wondering if anyone knew of a way of hit detecting the surface of the circle? If worse comes to worse I could iterate over a bufferedImage to get the pixel values of anything with alpha > 0 but that seems like a pain. I'd love any help. Matt P.S. This is what I'm trying to stop:
danpost danpost

2013/2/25

#
You can use: getObjectsInRange(getImage().getWidth()/2, Ball.class) to detect collisions.
mattrayner mattrayner

2013/2/26

#
Won't that just test in a box around the circles?
Jonas234 Jonas234

2013/2/26

#
I am not really sure if it is working but what you want to do is search for intersections. So you could do some Maths for a real circle it would be Pretty easy with: (x-m_x)^2+(y-m_y)^2 = r (m_x and m_y are the coordinates of your center point and r the radius of your Circle) Now you can just do sth. like Gaussian elimination to get the solution and you would have your result. For an ellipse i am not sure but i guess it should be sth. like (x-m_x)^2+(a*y-m_y)^2 = xRadius (m_x and m_y the same and xRadius would be the distance between the edge at y=0 and the center Point. a should be sth. like xRadius/yRadius) If you do the Gaussian Elimination on this to get your intersections you should be done. I really don't know how complicated it is to write sth like this. But at least it should be a solution for your Problem. Jonas
danpost danpost

2013/2/26

#
mattrayner wrote...
Won't that just test in a box around the circles?
Actually, this does check for objects within a circular area around the object. However, the radius will have to be increased to account for the other objects size. If your largest orb is of size, let us say 80, then:
for(Object obj : getObjectsInRange(getImage().getWidth()/2+80/2, Ball.class))
{
    Ball ball = (Ball) obj;
    if (getObjectsInRange(getImage().getWidth()/2+ball.getImage().getWidth()/2, Ball.class).contains(ball))
    {
        // collision with this 'ball' object
    }
}
danpost danpost

2013/2/26

#
And for the bounce:
double angle = Math.atan2(getY() - ball.getY(), getX() - ball.getX()) * 180.0 / Math.PI;
int newRotation = (int) angle * 2 + (540 - getRotation());
setRotation(newRotation);
If the other ball is in motion as well, then the rotation for it will have to be likewise adjusted.
mattrayner mattrayner

2013/2/26

#
Thanks for all your help guys - I managed to find a getIntersectingObjects(Class cls) call inside of Actor that takes into account the graphical composition of your image - Thanks for all your help! (I managed to miss it when I was looking before) Matt
danpost danpost

2013/2/26

#
The 'getIntersectingObjects(Class cls)' method will return any possible intersector whose horizontal distance is equal to or less than 'this.getImage().getWidth()/2+intersector.getImage().getWidth()/2' and whose vertical distance is equal to or less than 'this.getImage().getHeight()/2+intersector.getImage().getHeight()/2' regardless of the composition of the images. In other words, all objects whose rectangular image intersects this objects rectangular image will be returned.
mattrayner mattrayner

2013/2/26

#
In that case, I need to change it :P
You need to login to post a reply.