亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 libGDX 中以相反的方式發射子彈

如何在 libGDX 中以相反的方式發射子彈

慕森卡 2023-06-14 14:22:32
我創建了一個游戲的功能,即子彈以恒定的速度進入十字準線。但是我有一個問題:我怎樣才能讓子彈與它的射擊方向相反?讓我解釋一下我想做什么。當子彈接觸到特定材料時,它會朝相反的方向移動。就像這張圖片:如您所見,子彈正向相反的方向“彈跳”。這是我的功能,如果需要,我可以在其中設置子彈的線速度:public void direccion(float xx, float yy) {        float mousex = Gdx.input.getX();        float mousey = 0;        if (shoot.getAngle() > 6.1 && shoot.getAngle() < 9.6) {             mousey = Gdx.input.getY() - 5;        } else {            mousey = Gdx.input.getY();        }        Vector3 mousePos = new Vector3(mousex, mousey, 0);        jugador.camera.unproject(mousePos);        float speed = 60f;        float velx = mousePos.x - xx;        float vely = mousePos.y - yy;        float length = (float) Math.sqrt(velx * velx + vely * vely);        if (length != 0) {            velx = velx / length;            vely = vely / length;        }        shoot.setLinearVelocity(velx * speed, vely * speed);希望你能理解我的想法。誰能幫我這個?
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

在思考我該怎么做之后,我有了一個主意。

根據矢量數學,子彈在接觸到有問題的材料后可以采用這些值進行反彈......

如圖所示:

http://img1.sycdn.imooc.com//64895cc700011ca706510306.jpg

經過這次分析,我已經適應了一個簡單的代碼。


        vectorPart = shoot.getLinearVelocity();

        if ((vectorPart.x > 0 && vectorPart.y < 0) || (vectorPart.x > 0 && vectorPart.y > 0)

                || (vectorPart.x < 0 && vectorPart.y < 0) || (vectorPart.x < 0 && vectorPart.y > 0)) {

            vectorPart = new Vector2(vectorPart.x, vectorPart.y * -1);

        } else {

            vectorPart = new Vector2(vectorPart.x * -1, vectorPart.y * -1);

        }

        shoot.setLinearVelocity(vectorPart.x, vectorPart.y);

我評估了 linearVelocity 向量,然后修改了它的符號。


使用此代碼和 anylisis 一切正常!


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號