为什么这个简单的 Box2D 东西不起作用?
当我运行这个时,什么也没有发生。方块停留在 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,主体是静态的,因此只需将主体类型设置为 b2_dynamicBody 即可。
Bodies are static by default, so you just need to set the body type to b2_dynamicBody.