gpt4 book ai didi

java - setAngularVelocity 旋转非常慢

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:19 26 4
gpt4 key购买 nike

我正在使用基本的 libgdx box2d 来管理游戏的物理操作。除了旋转之外,一切都工作正常:即使我设置了

anyobject.body.setAngularVelocity(someLargeConstant);

无论“someLargeConstant”是多少,对象的旋转速度都非常慢(并且几乎以相同的速度)。除非我使用较小的数字作为参数,否则它会旋转得更慢。因此,我认为我的世界对象内部有一个最大角速度常数,应该将其设置为某个小值。

(我之前也遇到过类似的线速度问题,我通过调整像素/米比例解决了这个问题。所以问题不太可能是缩放问题。)

如何使对象旋转得更快?

这是我使用的代码:

private static World world = new World(new Vector2(0, 0), true);   //Create a world with no gravity

创建一个对象,我调用另一个类

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

关于java - setAngularVelocity 旋转非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com