2013-07-01 28 views
1

我想創建簡單的網球比賽。每邊都有一堵牆。還有一個盒子和一個球。我的環境沒有重力。盒子和球沒有任何速度。當球接觸球時(我用不同的速度通過鼠標移動球),球(球)只是改變其位置而不繼續移動,有時這些物體不會相互碰撞:Box2D(LibGDX) - 球的奇怪行爲

enter image description here

我希望盒子可以用不同的角度和力量擊球。

enter image description here

我怎樣才能做到這一點?我應該改變球和盒子的屬性?

代碼片斷如下:

public void createBall(Vector2 position, Vector2 velocity, float angle, Object userData){ 
    // First we create a body definition 
    BodyDef bodyDef = new BodyDef(); 
    // We set our body to dynamic, for something like ground which doesnt move we would set it to StaticBody 
    bodyDef.type = BodyType.DynamicBody; 
    // Set our body's starting position in the world 
    bodyDef.position.set(position); 

    // Create our body in the world using our body definition 
    Body body = world.createBody(bodyDef); 

    body.setUserData(userData); 
    // Create a circle shape and set its radius to 6 
    CircleShape circle = new CircleShape(); 
    circle.setRadius(10f); 

    PolygonShape poly = new PolygonShape();  
    poly.setAsBox(12, 12); 

    // Create a fixture definition to apply our shape to 
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = circle; 
    fixtureDef.density = 0.0f; 
    fixtureDef.friction = 0.2f; 
    fixtureDef.restitution = 1f; // Make it bounce a little bit 
    fixtureDef.isSensor=false; 

    // Create our fixture and attach it to the body 
    Fixture f = body.createFixture(fixtureDef); 
    f.setUserData("ball");  
    circle.dispose(); 
} 

    private Body createBox(World world, float width, float height, float density) { 
     BodyDef def = new BodyDef(); 
     def.type = BodyType.KinematicBody; 
     Body box = world.createBody(def); 

     PolygonShape poly = new PolygonShape(); 
     poly.setAsBox(width/2, height/2); 

     FixtureDef fixtureDef = new FixtureDef(); 
     fixtureDef.shape = poly; 
     fixtureDef.density = 0.0f; 
     fixtureDef.friction = 0.3f; 
     fixtureDef.restitution = 0.1f; // Make it bounce a little bit 
     fixtureDef.isSensor=false; 

     // Create our fixture and attach it to the body 
     Fixture f = box.createFixture(fixtureDef); 
     f.setUserData("platform"); 
     poly.dispose(); 

     return box; 
    } 
     public void createWall(World world, Vector2 position, float hx, float hy){ 
    // Create our body definition 
    BodyDef groundBodyDef =new BodyDef(); 
    // Set its world position 
    groundBodyDef.position.set(position); 
     groundBodyDef.type=BodyType.StaticBody; 
    // Create a body from the defintion and add it to the world 
    Body groundBody = world.createBody(groundBodyDef); 

    // Create a polygon shape 
    PolygonShape groundBox = new PolygonShape(); 
    // Set the polygon shape as a box which is twice the size of our view port and 20 high 
    // (setAsBox takes half-width and half-height as arguments) 
    groundBox.setAsBox(hx, hy); 
    // Create a fixture from our polygon shape and add it to our ground body 
    groundBody.createFixture(groundBox, 0.0f); 
    // Clean up after ourselves 
    groundBox.dispose(); 
} 

回答

3

您正在通過更改其位置來移動您的包廂。但碰撞解決的一個重要部分是速度。而且你的盒子的速度總是爲零(從box2d的角度來看)。因此你會遇到奇怪的碰撞分辨率

+0

如果我想用鼠標移動,我該怎麼辦?我應該使用什麼樣的功能? – Nolesh

+0

@Ares:看看b2MouseJoint。你可以將它與身體的固定旋轉結合起來。你也可以以某種方式計算平臺的速度,而不是從鼠標的位置。 – Andrew

+0

你能分享一個鏈接嗎? – Nolesh

0

的Box2D不喜歡體,其燈具具有密度爲0。平時,模擬不知何故執行,但行爲是不正確的。例如,嘗試0.3的值。

重疊問題可以通過設置標誌b2BodyDef :: bullet來解決。從Box2D的參考有描述:

布爾b2BodyDef ::子彈

這是應該由隧穿其他移動體來防止快速移動的身體嗎?請注意,所有物體都不能穿過運動和靜態物體。此設置僅在動態主體上考慮。

警告: 您應該謹慎使用此標誌,因爲它會增加處理時間。

+0

密度是什麼意思? – Nolesh

+0

不幸的是,它不起作用。同樣的行爲。 – Nolesh

1

我認爲你的屏幕寬度和高度太大...如果是這種情況嘗試使用世界的寬度和高度... 20x12單位..不是800x480東西。

+2

這幫了我一次 –

+0

你是說我應該將'OrthographicCamera'的寬度和高度分別設置爲20和12個單位? – Nolesh

+0

是試試....和所有的對象相應...像800/40 = 20和480/40 = 12 ...所以你的所有對象的寬度和高度應該除以40 ...嘗試它值得一個鏡頭 –