2
那麼我如何實現圓弧的碰撞檢測呢?我必須使用Box 2d碰撞還是可以用其他方式使用Rectangle或類似的東西?圓弧的碰撞檢測
順便說一句我討厭box2d,因爲我不明白大部分的東西,所以如果有一個解決方案排除box2d,它將非常感激。
的黃色弧線不斷在黑色圓圈旋轉。我如何在這裏實現碰撞檢測?
請幫忙!謝謝!
那麼我如何實現圓弧的碰撞檢測呢?我必須使用Box 2d碰撞還是可以用其他方式使用Rectangle或類似的東西?圓弧的碰撞檢測
順便說一句我討厭box2d,因爲我不明白大部分的東西,所以如果有一個解決方案排除box2d,它將非常感激。
的黃色弧線不斷在黑色圓圈旋轉。我如何在這裏實現碰撞檢測?
請幫忙!謝謝!
要避免使用Box2D的,你可以定義形狀爲多邊形,並使用polygon.contains(x,y)方法或使用Intersector
下面是同時使用的例子:
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Polygon;
public class Test extends ApplicationAdapter implements InputProcessor{
private ShapeRenderer sr;
private Polygon polya;
private boolean isColliding = false;
private Circle mp;
@Override
public void create() {
//define arc as polygon
// the more points used to define the shape will
// increase both required computation and collision precision
polya = new Polygon();
// create vertices
float section = 15f;
float[] newVerts = new float[200];
for(int i = 0; i < 50; i++){
newVerts[i*2] = (float)Math.sin(i/section); //x 0 to 98 even
newVerts[i*2+1] = (float)Math.cos(i/section); //y 1 to 99 odd
newVerts[199-i*2] = (float)Math.cos(i/section); //x 100 to 108
newVerts[198-i*2] = (float)Math.sin(i/section) + 0.2f; //y 101 to 199
}
polya.setVertices(newVerts);
polya.scale(50);
polya.setOrigin(1, 1);
polya.rotate(60);
//define circle to act as point for checking intersections
mp = new Circle(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,4);
// setup batchers
sr = new ShapeRenderer();
sr.setAutoShapeType(true);
Gdx.input.setInputProcessor(this);
}
@Override
public void render() {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// check collision with polygon
isColliding = polya.contains(mp.x,mp.y);
//check collision using Intersector
isColliding = Intersector.isPointInPolygon(polya.getTransformedVertices(),0,polya.getVertices().length,mp.x,mp.y);
sr.begin();
sr.setColor(Color.WHITE);
if(isColliding){
sr.setColor(Color.RED);
}
sr.polygon(polya.getTransformedVertices());
sr.circle(mp.x,mp.y,mp.radius);
sr.end();
}
@Override
public void dispose() {
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
int newy = Gdx.graphics.getHeight() - screenY;
polya.setPosition(screenX, newy);
return false;
}
(... removed unused input processor methods for clarity ...)
}
好吧,我運行你的代碼,所以我可以看看你做了什麼,所以我看到我弧你的多邊形,我發現很酷btw :)但我的問題是我旋轉我的弧線,所以我將不得不旋轉我的多邊形我怎麼做? –
您可以使用polya.rotate(度)旋轉多邊形;您可能還需要設置原點,使其圍繞右點旋轉polya.setOrigin(originX,originY);我將它們添加到示例中。 – dfour