为什么这个简单的 Box2D 东西不起作用?

发布于 2024-11-26 09:52:19 字数 1540 浏览 0 评论 0原文

当我运行这个时,什么也没有发生。方块停留在 (10.0,-10.0) 并且不会掉落。为什么?

这是我的代码:

import processing.core.PApplet;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;  

public class PhysicsWorld extends PApplet {
    
    private World world;
    private Body block;
    private static final float RATIO = 30f;
    
    public void createWorld() {
        Vec2 gravity = new Vec2(0.f,-9.8f);
        world = new World(gravity, true);
        world.setGravity(gravity);
        
    }
    
    private void createBlock(){
        BodyDef blockDef;
        PolygonShape blockShape;
        
        blockDef = new BodyDef();
        blockDef.position.set(300.f / RATIO, -300.f / RATIO);
        
        blockShape = new PolygonShape();
        blockShape.setAsBox(20/RATIO, 25/RATIO);
        
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = blockShape;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.8f;
        fixtureDef.restitution = 0.3f;
        
        block = world.createBody(blockDef);
        block.createFixture(fixtureDef);
    }
    
    public void setup(){
        createWorld();
        createBlock();
    }
    
    public void draw(){
        world.step(1/60, 8, 3);
        //world.clearForces();
        System.out.println(block.getPosition() +" " + world.getGravity());
        
    }

}

When I run this, nothing happens. The block stays at (10.0,-10.0) and doesn't fall. Why?

Here's my code:

import processing.core.PApplet;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;  

public class PhysicsWorld extends PApplet {
    
    private World world;
    private Body block;
    private static final float RATIO = 30f;
    
    public void createWorld() {
        Vec2 gravity = new Vec2(0.f,-9.8f);
        world = new World(gravity, true);
        world.setGravity(gravity);
        
    }
    
    private void createBlock(){
        BodyDef blockDef;
        PolygonShape blockShape;
        
        blockDef = new BodyDef();
        blockDef.position.set(300.f / RATIO, -300.f / RATIO);
        
        blockShape = new PolygonShape();
        blockShape.setAsBox(20/RATIO, 25/RATIO);
        
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = blockShape;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.8f;
        fixtureDef.restitution = 0.3f;
        
        block = world.createBody(blockDef);
        block.createFixture(fixtureDef);
    }
    
    public void setup(){
        createWorld();
        createBlock();
    }
    
    public void draw(){
        world.step(1/60, 8, 3);
        //world.clearForces();
        System.out.println(block.getPosition() +" " + world.getGravity());
        
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-12-03 09:52:19

默认情况下,主体是静态的,因此只需将主体类型设置为 b2_dynamicBody 即可。

Bodies are static by default, so you just need to set the body type to b2_dynamicBody.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文