views:

147

answers:

2

So I'm trying to implement a test where a oval can connect with a circle, but it's not working.

edist = (float) Math.sqrt(
    Math.pow((px + ((pwidth/2) )) - (bx + (bsize/2)), 2 ) + 
    Math.pow(-((py + ((pwidth/2)) ) - (bx + (bsize/2))), 2 )
);

and here is the full code (requires Slick2D):

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class ColTest extends BasicGame{


    float px = 50;
    float py = 50;
    float pheight = 50;
    float pwidth = 50;



    float bx = 200;
    float by = 200;
    float bsize = 200;

    float edist;

    float pspeed = 3;
    Input input;

    public ColTest()
    {
        super("ColTest");
    }

    @Override
    public void init(GameContainer gc)
            throws SlickException {

    }

    @Override
    public void update(GameContainer gc, int delta)
            throws SlickException
    {
        input = gc.getInput();

        try{    
            if(input.isKeyDown(Input.KEY_UP))   
                py-=pspeed;

            if(input.isKeyDown(Input.KEY_DOWN)) 
                py+=pspeed;

            if(input.isKeyDown(Input.KEY_LEFT)) 
                px-=pspeed;

            if(input.isKeyDown(Input.KEY_RIGHT))    
                px+=pspeed;
        }

        catch(Exception e){}
    }

    public void render(GameContainer gc, Graphics g)
            throws SlickException
    {   
            g.setColor(new Color(255,255,255));
            g.drawString("col: " + col(), 10, 10);
            g.drawString("edist: " + edist + " dist: " + dist, 10, 100);

            g.fillRect(px, py, pwidth, pheight);
            g.setColor(new Color(255,0,255));
            g.fillOval(px, py, pwidth, pheight);
            g.setColor(new Color(255,255,255));
            g.fillOval(200, 200, 200, 200);

    }

    public boolean col(){

        edist = (float) Math.sqrt(Math.pow((px + ((pwidth/2) )) - (bx + (bsize/2)), 2) + Math.pow(-((py + ((pwidth/2)) ) - (bx + (bsize/2))), 2));

        if(edist <= (bsize/2) + (px + (pwidth/2)))
            return true;

        else
            return false;
    }

    public float rotate(float x, float y, float ox, float oy, float a, boolean b)
    {
         float dst = (float) Math.sqrt(Math.pow(x-ox,2.0)+ Math.pow(y-oy,2.0));

         float oa = (float) Math.atan2(y-oy,x-ox);

         if(b)
            return (float) Math.cos(oa + Math.toRadians(a))*dst+ox;

         else
            return (float) Math.sin(oa + Math.toRadians(a))*dst+oy;

    }

    public static void main(String[] args)
            throws SlickException
    {
         AppGameContainer app =
            new AppGameContainer( new ColTest() );

         app.setShowFPS(false);
         app.setAlwaysRender(true);
         app.setTargetFrameRate(60);
         app.setDisplayMode(800, 600, false);
         app.start();
    }
}
A: 

Finding the intersection is harder than you think. Your col() method is a bit off, but that approach will at best be able to tell you if a single point is within the circle. It won't be able to really detect intersections.

I Googled up some code for computing the actual intersections. I found one in JavaScript that's really interesting and really complicated. Take a look at the source.

If you wanted something a bit simpler (but less accurate), you could check a few points around the ellipse to see if they're within the circle.

private boolean isInCircle(float x, float y) {
    float r = bsize / 2;
    float center_x = bx + r;
    float center_y = by + r;
    float dist = (float) Math.sqrt(Math.pow(x - center_x, 2) + Math.pow(y - center_y, 2));

    return dist < r;
}

public boolean col() {
    return 
        isInCircle(px + pwidth / 2, py              ) || // top
        isInCircle(px + pwidth    , py + pheight / 2) || // right
        isInCircle(px + pwidth / 2, py + pheight    ) || // bottom
        isInCircle(px             , py + pheight / 2);   // left
}
dave
A: 

Is using ovals an absolute requirement? You can approximate collisions between fancier shapes by representing them with multiple circles. That way you can use very a simple collision detection between circles and still achieve a high level of accuracy for the viewer.

collision(c1, c2) {
  dx = c1.x - c2.x;
  dy = c1.y - c2.y;
  dist = c1.radius + c2.radius;

  return (dx * dx + dy * dy <= dist * dist)
}

alt text

Daniel X Moore