1

所以我正在尝试实现一个椭圆可以与圆形连接的测试,但它不起作用。

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

这是完整的代码(需要 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();
    }
}
4

4 回答 4

2

使用椭圆是绝对要求吗?您可以通过用多个圆圈表示更精美的形状来近似碰撞。这样,您可以在圆之间使用非常简单的碰撞检测,并且仍然可以为查看者实现高水平的准确度。

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)
}

替代文字
(来源:strd6.com

于 2010-06-20T17:38:17.430 回答
0

找到交叉点比你想象的要难。你的col()方法有点偏离,但这种方法最多只能告诉你一个点是否在圆内。它无法真正检测到交叉路口。

我用谷歌搜索了一些计算实际交叉点的代码。我在 JavaScript 中发现了一个非常有趣且非常复杂的东西。看看源码

如果您想要一些更简单(但不太准确)的东西,您可以检查椭圆周围的几个点,看看它们是否在圆内。

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
}
于 2010-05-19T05:18:09.670 回答
0

如果您计划实现更多形状和/或需要形状之间的最小距离,您可以开始使用GJK:您只需要为每个新形状实现支持功能。如果计算时间也很关键,那么 GJK 绝对是您应该考虑的东西,但它肯定需要您进行更多的编程。

于 2011-11-13T16:10:08.657 回答
0

如果你能找到你的焦点,你可以用下面的伪代码检查碰撞。警告这仅适用于两个椭圆碰撞(椭圆和圆形碰撞也适用)。

r = length of semi major axis
a_x = x coordinate of foci 1 of the first ellipse
a_y = y coordinate of foci 1 of the first ellipse
b_x = x coordinate of foci 2 of the first ellipse
b_y = y coordinate of foci 2 of the first ellipse

c_x = x coordinate of foci 1 of the second ellipse
c_y = y coordinate of foci 1 of the second ellipse
d_x = x coordinate of foci 2 of the second ellipse
d_y = y coordinate of foci 2 of the second ellipse

p_x = (a_x+b_x+c_x+d_x)/4 // i.e. the average of the foci x values
p_y = (a_y+b_y+c_y+d_y)/4 // i.e. the average of the foci y values

if r >= ( sqrt( (p_x + a_x)^2+(p_y + a_y)^2 ) + sqrt( (p_x + a_x)^2+(p_y + a_y)^2 ) )
then collision

如果你真的想要这个的推导,请告诉我,我会提供它。但它使用的想法是椭圆的焦点和椭圆边缘上的任何点之间的距离之和是相隔一定距离(半长轴)。并求解位于两个椭球边缘的点,如果存在,则它们是碰撞。

于 2014-05-29T14:11:20.107 回答