android - Box2D(LibGDX) - 球的奇怪行为

标签 android box2d libgdx physics

我正在尝试创建简单的网球游戏。每边都有一面墙。还有一个盒子和一个球。我的环境没有重力。盒子和球没有任何速度。当盒子接触到球(我用鼠标以不同的速度移动它)时,它(球)只是改变了它的位置而不会继续移动,有时这些物体不会相互碰撞:

enter image description here

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

enter image description here

我该怎么做?我应该更改球和盒子的哪些属性?

代码 fragment 如下:

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

最佳答案

你正在通过改变它的位置来移动你的盒子。但是碰撞解决的一个重要部分是速度。而且你的盒子的速度总是为零(从 box2d 的角度来看)。因此你会遇到奇怪的碰撞解决

关于android - Box2D(LibGDX) - 球的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401118/

相关文章:

java - 忽略来自 Android APK 的文件

java - 从 Firebase 存储检索图像下载 url

java - LibGDX Box2D 播放器绊倒

android - Box2d 和 Andengine 物理教程

ios - 如何根据iOS设备定义PTM_RATIO

java - box2d libgdx - 'binding' Sprite 或其他对象的 box2d 主体

Java Android 应用程序 - 德州扑克的简单 AI

java - Libgdx Scene2d 进度条永远不会为空

java - LibGdx : Keep one spriteBatch as singleton in Game? 或每个屏幕一个 spriteBacth?

java - Android 在绘制之前获取 View 的位图?