[英]LibGDX with Box2D, can't understand Velocity().y
我正在使用LibGDX學習Box2D的基礎知識。 我今天來到你們這里是因為我很難理解Velocity的工作原理。 我實際上使用了兩個主體:一個是DynamicBody(用於播放器),另一個是StaticBody(地面)。 在圖像中看起來像這樣: game_screen現在,沒有任何問題,我接下來添加了一個ControlSystem,該控件允許玩家使用方向箭頭移動(顯然是向上,向左和向右)。 問題出在我想向左/向右走時,每當使用Body.setLinearVelocity(somevalue,0)時,它移動得很好,但是我仍然有一點點我不希望的Y速度 。 我高度認為這是由地面引起的(某種程度上是由摩擦或類似的東西引起的),但是我對一般物理學一無所知,所以我不能說真的。 我想知道是否有一種方法可以將Y LinearVelocity設置為0左右移動播放器。順便說一下,我想將此Y LinearVelocity設置為0,因為我使用的是“ StateComponent”,它將狀態設置為“ JUMPING”如果Y LinearVelocity大於0 ,則代碼如下所示:
@Override
protected void processEntity(Entity entity, float deltaTime) {
BodyComponent bodyComp = bodm.get(entity);
StateCompent state = sm.get(entity);
TextureComponent texComp = texm.get(entity);
if(bodyComp.body.getLinearVelocity().y > 0){
state.set(StateCompent.STATE_FALLING);
}
if(bodyComp.body.getLinearVelocity().y == 0){
if(state.get() == StateCompent.STATE_FALLING){
state.set(StateCompent.STATE_NORMAL);
}
if(bodyComp.body.getLinearVelocity().x != 0){
state.set(StateCompent.STATE_MOVING);
}
}
if(controller.left){
bodyComp.body.setLinearVelocity(MathUtils.lerp(bodyComp.body.getLinearVelocity().x, -2f, 0.2f),bodyComp.body.getLinearVelocity().y);
if(!texComp.region.isFlipX()){
texComp.region.flip(true, false);
}
}
if(controller.right){
bodyComp.body.setLinearVelocity(MathUtils.lerp(bodyComp.body.getLinearVelocity().x, 2f, 0.2f),bodyComp.body.getLinearVelocity().y);
if(texComp.region.isFlipX()){
texComp.region.flip(true, false);
}
}
if(!controller.left && ! controller.right){
bodyComp.body.setLinearVelocity(MathUtils.lerp(bodyComp.body.getLinearVelocity().x, 0, 0.1f),bodyComp.body.getLinearVelocity().y);
}
if(controller.up && (state.get() == StateCompent.STATE_NORMAL || state.get() == StateCompent.STATE_MOVING)){
bodyComp.body.applyLinearImpulse(0,10f, bodyComp.body.getWorldCenter().x, bodyComp.body.getWorldCenter().y, true);
state.set(StateCompent.STATE_JUMPING);
}
// Hud to delete later
hudBatch.begin();
layout.setText(font, " X velocity : " + Float.toString(bodyComp.body.getLinearVelocity().x));
font.draw(hudBatch, " X velocity : " + Float.toString(bodyComp.body.getLinearVelocity().x), Gdx.graphics.getWidth() - (layout.width + 20), Gdx.graphics.getHeight() - layout.height);
layout.setText(font, " Y velocity : " + Float.toString(bodyComp.body.getLinearVelocity().y));
font.draw(hudBatch, " Y velocity : " + Float.toString(bodyComp.body.getLinearVelocity().y), Gdx.graphics.getWidth() - (layout.width + 20), Gdx.graphics.getHeight() - (layout.height + 20));
font.draw(hudBatch, "State : " + Integer.toString(state.get()), 20, Gdx.graphics.getHeight() - 20 );
hudBatch.end();
也許如果需要的話,我的兩個身體的FixtureDef:
fixtureDef.density = 1f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.01f;
我還上傳了一個視頻 ,您可以在該視頻中看到,當我使用向左或向右箭頭鍵移動Velocity Y時,速度最高可達0,000000001。 知道為什么嗎?
大多數物理引擎自然在兩個方向上都有一點點速度,特別是在碰撞過程中。 只需將此因素納入您的跳轉檢查中即可,例如,使閾值略高於0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.