setAngularVelocity 旋转真的很慢
setAngularVelocity rotates really slowly
我正在使用基本的 libgdx box2d 来管理游戏的物理操作。一切正常,除了轮换:即使我设置
anyobject.body.setAngularVelocity(someLargeConstant);
无论 'someLargeConstant' 是什么,物体旋转得非常慢(并且几乎以相同的速度)。除了当我使用小数字作为参数时,它可以旋转得更慢。因此,我认为我的世界对象内部有一个最大 angular 速度常数,应该将其设置为某个较小的值。
(我之前也有过类似的线速度问题,我通过调整pixels/meter比例解决了它。所以问题不太可能是比例问题。)
如何让物体旋转得更快?
这是我使用的代码:
private static World world = new World(new Vector2(0, 0), true); //Create a world with no gravity
创建一个对象我调用另一个 class
public Object(World world, short category, short mask, float x, float y, float radius, Sprite image,
float maxSpeed, float frictionStrength, float linearDamping, float angularDamping, boolean movable,
float elasticity, float mass){
this.world = world;
this.category = category;
this.mask = mask;
// We set our body type
this.bodyDef = new BodyDef();
if(movable==true){bodyDef.type = BodyType.DynamicBody;}else{bodyDef.type = BodyType.StaticBody;}
// Set body's starting position in the world
bodyDef.position.set(x, y);
bodyDef.linearDamping = linearDamping;
bodyDef.angularDamping = angularDamping;
// Create our body in the world using our body definition
this.body = world.createBody(bodyDef);
// Create a circle shape and set its radius
CircleShape circle = new CircleShape();
circle.setRadius(radius);
// Create a fixture definition to apply our shape to
fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = (float) (mass/(Math.PI*radius*radius));
fixtureDef.friction = frictionStrength;
fixtureDef.restitution = elasticity;
fixtureDef.filter.categoryBits = category;
fixtureDef.filter.maskBits = mask;
// Create our fixture and attach it to the body
this.fixture = body.createFixture(fixtureDef);
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();
... unrelated functions after that
}
这里我只是试着让它快速旋转:
tempBall.body.setAngularVelocity(20000);
angularvilocity
用于设置旋转方向,当它与 actionListener
一起使用时,如键或鼠标列表器,这里是一个使用示例:
case KeyEvent.VK_RIGHT:
ball.setAngularVelocity(-20); // Directly set the angular velocity
case KeyEvent.VK_LEFT:
ball.setAngularVelocity(20); // Directly set the angular velocity
就像你在这里看到的那样,代码使球体在 Key_Right 按下时向右旋转,在 Key_Left 按下时向左旋转,我可以玩弄它的参数来增加或降低旋转速度,它对我来说效果很好,这是我的 body
定义尝试应用相同的值并且它必须没有问题:
private Body createObject(Shape shape, BodyType type, Vec2 position, float orientation, Sprite sprite) throws InvalidSpriteNameException {
for(Sprite s:spriteList) {
if(s.getName().equals(sprite.getName())) {
throw new InvalidSpriteNameException(sprite.getName()+" already used.");
}
}
Body body = null;
FixtureDef fixDef = new FixtureDef();
fixDef.shape = shape;
fixDef.density = 0.1f;
fixDef.isSensor = false;
fixDef.restitution = 0.1f;
BodyDef bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.angularDamping = 0.1f;
bodyDef.linearDamping = 0.1f;
bodyDef.fixedRotation = false;
bodyDef.gravityScale = 1f;
bodyDef.linearVelocity = new Vec2(0,0);
bodyDef.angularVelocity = 0;
bodyDef.position = new Vec2(position);
bodyDef.angle = orientation;
bodyDef.allowSleep = true;
spriteList.add(sprite); // Save the sprite to the list (sprites must be serialiazed in the PhysicalWorld)
bodyDef.userData = sprite; // Link the body and the sprite
do {
body = jBox2DWorld.createBody(bodyDef);
} while(body== null); // Wait until the object is really created
sprite.linkToBody(body); // Link the body to the sprite (this link is not serialiazed)
body.createFixture(fixDef);
return body;
}
我刚刚发现了问题,而且很简单。我只是要 post 在这里为未来的 googlers:
对象实际上是正确旋转的,问题出在我的绘图方法上,我没有在我的 batch.draw 中使用弧度与度数之间的转换,它以弧度解释所有内容。我知道,这是一个业余的错误!非常感谢您的宝贵时间。
我正在使用基本的 libgdx box2d 来管理游戏的物理操作。一切正常,除了轮换:即使我设置
anyobject.body.setAngularVelocity(someLargeConstant);
无论 'someLargeConstant' 是什么,物体旋转得非常慢(并且几乎以相同的速度)。除了当我使用小数字作为参数时,它可以旋转得更慢。因此,我认为我的世界对象内部有一个最大 angular 速度常数,应该将其设置为某个较小的值。
(我之前也有过类似的线速度问题,我通过调整pixels/meter比例解决了它。所以问题不太可能是比例问题。)
如何让物体旋转得更快?
这是我使用的代码:
private static World world = new World(new Vector2(0, 0), true); //Create a world with no gravity
创建一个对象我调用另一个 class
public Object(World world, short category, short mask, float x, float y, float radius, Sprite image,
float maxSpeed, float frictionStrength, float linearDamping, float angularDamping, boolean movable,
float elasticity, float mass){
this.world = world;
this.category = category;
this.mask = mask;
// We set our body type
this.bodyDef = new BodyDef();
if(movable==true){bodyDef.type = BodyType.DynamicBody;}else{bodyDef.type = BodyType.StaticBody;}
// Set body's starting position in the world
bodyDef.position.set(x, y);
bodyDef.linearDamping = linearDamping;
bodyDef.angularDamping = angularDamping;
// Create our body in the world using our body definition
this.body = world.createBody(bodyDef);
// Create a circle shape and set its radius
CircleShape circle = new CircleShape();
circle.setRadius(radius);
// Create a fixture definition to apply our shape to
fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = (float) (mass/(Math.PI*radius*radius));
fixtureDef.friction = frictionStrength;
fixtureDef.restitution = elasticity;
fixtureDef.filter.categoryBits = category;
fixtureDef.filter.maskBits = mask;
// Create our fixture and attach it to the body
this.fixture = body.createFixture(fixtureDef);
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();
... unrelated functions after that
}
这里我只是试着让它快速旋转:
tempBall.body.setAngularVelocity(20000);
angularvilocity
用于设置旋转方向,当它与 actionListener
一起使用时,如键或鼠标列表器,这里是一个使用示例:
case KeyEvent.VK_RIGHT:
ball.setAngularVelocity(-20); // Directly set the angular velocity
case KeyEvent.VK_LEFT:
ball.setAngularVelocity(20); // Directly set the angular velocity
就像你在这里看到的那样,代码使球体在 Key_Right 按下时向右旋转,在 Key_Left 按下时向左旋转,我可以玩弄它的参数来增加或降低旋转速度,它对我来说效果很好,这是我的 body
定义尝试应用相同的值并且它必须没有问题:
private Body createObject(Shape shape, BodyType type, Vec2 position, float orientation, Sprite sprite) throws InvalidSpriteNameException {
for(Sprite s:spriteList) {
if(s.getName().equals(sprite.getName())) {
throw new InvalidSpriteNameException(sprite.getName()+" already used.");
}
}
Body body = null;
FixtureDef fixDef = new FixtureDef();
fixDef.shape = shape;
fixDef.density = 0.1f;
fixDef.isSensor = false;
fixDef.restitution = 0.1f;
BodyDef bodyDef = new BodyDef();
bodyDef.type = type;
bodyDef.angularDamping = 0.1f;
bodyDef.linearDamping = 0.1f;
bodyDef.fixedRotation = false;
bodyDef.gravityScale = 1f;
bodyDef.linearVelocity = new Vec2(0,0);
bodyDef.angularVelocity = 0;
bodyDef.position = new Vec2(position);
bodyDef.angle = orientation;
bodyDef.allowSleep = true;
spriteList.add(sprite); // Save the sprite to the list (sprites must be serialiazed in the PhysicalWorld)
bodyDef.userData = sprite; // Link the body and the sprite
do {
body = jBox2DWorld.createBody(bodyDef);
} while(body== null); // Wait until the object is really created
sprite.linkToBody(body); // Link the body to the sprite (this link is not serialiazed)
body.createFixture(fixDef);
return body;
}
我刚刚发现了问题,而且很简单。我只是要 post 在这里为未来的 googlers:
对象实际上是正确旋转的,问题出在我的绘图方法上,我没有在我的 batch.draw 中使用弧度与度数之间的转换,它以弧度解释所有内容。我知道,这是一个业余的错误!非常感谢您的宝贵时间。