簡體   English   中英

Libgdx / Box2D是否向身體施加推動力?

[英]Libgdx/Box2D Apply a Push to the Body?

我目前正在開發一個小地牢爬行者。 我的“騎士”課應該有特殊的能力。 他應該能夠推動並突破敵人。 我目前正在為敵人和玩家使用Box2D實體...任何想法如何使玩家實體朝一個方向發展?

我已經測試過:

entityBody.applyLinearImpulse(100f, 0, entityBody.getWorldCenter().x,    entityBody.getWorldCenter().y, true);          

它起作用了,但問題是,身體被傳送到那個方向,而不是“向前推動”。 快速移動時他也不會碰撞...有什么想法嗎?

看看Wiki怎么說: https//github.com/libgdx/libgdx/wiki/box2d#impulsesforces

您可能對“ 玩家移動示例”部分感興趣。 他們是否向左或向右施加脈沖,具體取決於是否按下了哪個鍵以及哪個鍵未達到最大速度。

Vector2 vel = this.player.body.getLinearVelocity();
Vector2 pos = this.player.body.getPosition();

// apply left impulse, but only if max velocity is not reached yet
if (Gdx.input.isKeyPressed(Keys.A) && vel.x > -MAX_VELOCITY) {          
     this.player.body.applyLinearImpulse(-0.80f, 0, pos.x, pos.y, true);
}

// apply right impulse, but only if max velocity is not reached yet
if (Gdx.input.isKeyPressed(Keys.D) && vel.x < MAX_VELOCITY) {
     this.player.body.applyLinearImpulse(0.80f, 0, pos.x, pos.y, true);
}

為了避免由於身體移動太快而發生碰撞的問題,您可能需要在身體的BodyDef上設置項目符號標記:

    bodyDef.bullet = true;

您還將x脈沖設置為100,這基本上意味着將x速度設置為每秒100米...相當快(大約224 mph)。 如果需要的話,請以超級跑車的速度隨意打擊敵人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM