使动态物体下落 JBox2D
我在使用 JBox2D 时遇到一些问题。我已经创建了一个球和世界以及据我所知正确的所有内容,但是当我调用 world.step() 方法时,它不会使球从重力下落。
这是我的对象创建:
package objects;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.lwjgl.opengl.GL11;
import util.GLColor;
import util.Location;
import util.Size;
public class PlayerCircle extends DefaultObject{
GLColor c = new GLColor(25,255,255);
public PlayerCircle(Location l,Size s){
super(l,s);
load(l.getX(),l.getY());
}
BodyDef bd = new BodyDef();
FixtureDef fd = new FixtureDef();
public static CircleShape cd = new CircleShape();
public void load(double x, double y){
bd.type = BodyType.DYNAMIC;
// bd.position = new Vec2(0.0f, -10.0f);
cd.m_radius = s.getRadius()/Main.Main.scaleX;
//cd.m_p.set((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
bd.position = new Vec2((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
fd.friction = .1f;
fd.density = 2f;
fd.shape = cd;
Main.Main.getWorld().createBody(bd).createFixture(fd);
// bd.allowSleep = false;
}
}
这是主循环所在的代码:
public void run() throws LWJGLException, InterruptedException{
Vec2 gravity = new Vec2(2f, -.1f);
world = new World(gravity, true);
Display.setDisplayMode(new DisplayMode(900,700));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, scaleX, 0, scaleY, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
level = new Level1();
level.load();
level.start();
float tempStep = 1.0f/60.0f;
int VI = 6;
int POSI = 2;
int a = 0;
while(!done){
//System.out.println(world.getContactCount() + " "+world.getGravity().x);
world.step(tempStep, VI, POSI);
Display.update();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
logic();
if (Display.isVisible() || Display.isDirty()) {
render();
}
if(Display.isCloseRequested()){
done = true;
}
Thread.sleep(1);
}
}
}
打印的测试类
package test;
import objects.PlayerCircle;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import util.Location;
import util.Size;
public class Test {
static Vec2 gravity = new Vec2(2f, -.5f);
static float tempStep = 1.0f/60.0f;
static int VI = 6;
static int POSI = 2;
public static World world = new World(gravity,true);
public static void main(String args[]){
PlayerCircle pc = new PlayerCircle(new Location(400,5000),new Size(10));
while(true){
world.step(tempStep, VI, POSI);
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(pc.bd.position.y);
}
}
}
结果
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
I'm having some problems with JBox2D. I've created a ball and world and everything as far as I can tell correctly, but when I call the world.step() method it dosen't make the ball fall from gravity.
Here is my object creation:
package objects;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.lwjgl.opengl.GL11;
import util.GLColor;
import util.Location;
import util.Size;
public class PlayerCircle extends DefaultObject{
GLColor c = new GLColor(25,255,255);
public PlayerCircle(Location l,Size s){
super(l,s);
load(l.getX(),l.getY());
}
BodyDef bd = new BodyDef();
FixtureDef fd = new FixtureDef();
public static CircleShape cd = new CircleShape();
public void load(double x, double y){
bd.type = BodyType.DYNAMIC;
// bd.position = new Vec2(0.0f, -10.0f);
cd.m_radius = s.getRadius()/Main.Main.scaleX;
//cd.m_p.set((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
bd.position = new Vec2((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
fd.friction = .1f;
fd.density = 2f;
fd.shape = cd;
Main.Main.getWorld().createBody(bd).createFixture(fd);
// bd.allowSleep = false;
}
}
Heres the code where the main loop is:
public void run() throws LWJGLException, InterruptedException{
Vec2 gravity = new Vec2(2f, -.1f);
world = new World(gravity, true);
Display.setDisplayMode(new DisplayMode(900,700));
Display.create();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, scaleX, 0, scaleY, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
level = new Level1();
level.load();
level.start();
float tempStep = 1.0f/60.0f;
int VI = 6;
int POSI = 2;
int a = 0;
while(!done){
//System.out.println(world.getContactCount() + " "+world.getGravity().x);
world.step(tempStep, VI, POSI);
Display.update();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
logic();
if (Display.isVisible() || Display.isDirty()) {
render();
}
if(Display.isCloseRequested()){
done = true;
}
Thread.sleep(1);
}
}
}
Test Class
package test;
import objects.PlayerCircle;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import util.Location;
import util.Size;
public class Test {
static Vec2 gravity = new Vec2(2f, -.5f);
static float tempStep = 1.0f/60.0f;
static int VI = 6;
static int POSI = 2;
public static World world = new World(gravity,true);
public static void main(String args[]){
PlayerCircle pc = new PlayerCircle(new Location(400,5000),new Size(10));
while(true){
world.step(tempStep, VI, POSI);
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(pc.bd.position.y);
}
}
}
Result from print
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Body 对象获取 y 位置,而不是从 BodyDef 对象获取。
为此,请存储 Main.Main.getWorld().createBody(bd) 返回的 Body 对象,并通过 pc.yourBodyVariableNameHere.m_xf.position.y 访问其位置
Get the y position from the Body object, not from the BodyDef object.
To do this, store the Body object returned by Main.Main.getWorld().createBody(bd) and access its position by pc.yourBodyVariableNameHere.m_xf.position.y