简体   繁体   English

在libgdx Java中基于手臂旋转的射击对象

[英]Shooting object based on arm rotation in libgdx java

I am trying to shoot an object(a spell) depending on the rotation of the players arm. 我试图根据玩家手臂的旋转来射击一个物体(一个咒语)。 The spell is supposed to come out of the hand and shoot towards where the mouse cicked(the arm rotates and points to where the mouse is). 该咒语应该从手中出来,朝着老鼠c动的地方射击(手臂旋转并指向老鼠所在的位置)。 This is how the arm rotates in game. 这就是手臂在游戏中旋转的方式。

    public boolean mouseMoved(int screenX, int screenY) {
    tmp.x = screenX;
    tmp.y = screenY;
    tmp.z = 0;
    cam.unproject(tmp);
    rot = MathUtils.radiansToDegrees * MathUtils.atan2((float)tmp.y - (float)player.getArmSprite().getY() - player.getArmSprite().getHeight(),
            tmp.x -player.getArmSprite().getX() - player.getArmSprite().getWidth());
    if (rot < 0) rot += 360;

    //Last right or left means if hes looking left or right
    if(player.lastRight)
        player.setObjectRotation(rot + 80);
     if(player.lastLeft)
        player.setObjectRotation(-rot - 80);

And this is how the spell is supposed to shoot based off rotation 这就是法术应该根据旋转来射击的方式

    //destination is a vector of where on screen the mouse was clicked
    if(position.y < destination.y){
        position.y += vel.y * Gdx.graphics.getDeltaTime();
    }
    if(position.x < destination.x){
        position.x += vel.x * Gdx.graphics.getDeltaTime();
    }

However this is very wonky and never really reacts the way it supposed to with a few exceptions. 但是,这非常奇怪,除了少数例外,它从未真正按照预期的方式做出反应。 It fires from the hand and then if the y axis is equal it completely evens out and goes till it reaches the x position, I want it to fire from the hand to the position clicks perfectly straight from point a to point b, this is clearly a rotation problem that I just can't seem to figure out how to tackle. 它从手发射,然后如果y轴相等,它会完全平整并一直到达x位置,我希望它从手发射到从点a到点b完全笔直单击的位置,这很明显我似乎无法弄清楚如何解决轮换问题。

Here is an image of what is happening 这是正在发生的事情的图像

Example image 范例图片

The red indicates where I clicked, as you can see it reached the x pos first and now is traveling to the y when it should have reached the x and y pos of where I clicked first 红色表示我单击的位置,如您所见,它首先到达x位置,而当它应该到达我首先单击的位置的x和y位置时,现在正在移动到y

Any help with this problem is extremely appreciated! 非常感谢您提供有关此问题的帮助!

I'm pretty bad at radians and tangents but luckily we have vectors. 我在弧度和切线方面很不好,但是幸运的是我们有矢量。

Since you have the rot ation in degrees of the arm. 既然你有rot的程度手臂通货膨胀。 I advice to use Vectors to use for any Vector related math now. 我建议现在将Vector用于所有与Vector相关的数学。

//A vector pointing up
Vector2 direction = new Vector2(0, 1);

//Let's rotate that by the rotation of the arm
direction.rotate(rot);

Now direction is the direction the arm is pointing. 现在,方向是手臂指向的方向。 If your rot ation is calculated where up = 0 . 如果你的rot通货膨胀的计算,其中up = 0 So you might need to rotate it 180, 90 or -90 degrees. 因此,您可能需要将其旋转180、90或-90度。 Or in the case you did something silly any degrees. 或者,如果您在任何程度上都做了愚蠢的事情。

Your spell should have a Vector too for it's position. 您的咒语也应该具有一个向量,以确保其位置。 Set that to the hand or wherever you want to start from. 将其设置到手或要从哪里开始。 Now all you need to do is scale that direction since it's currently has a length of 1. If you want to move 5 units each frame you can do direction.scl(5) now it is of length 5. But technically speaking it's no direction anymore now everybody calls it velocity so let's do. 现在您需要做的就是缩放该direction因为它当前的长度为1。如果要每帧移动5个单位,则可以执行direction.scl(5)现在它的长度为5。但是从技术上讲,这是没有方向的现在每个人都称它为velocity所以我们开始吧。

//when you need to fire
float speed = 5;
Vector2 velocity = direction.cpy().scl(speed);

//update
position.add(velocity);
draw(fireballImage, position.x, position.y);

I copied direction first, otherwise it would also be scaled. 我先复制了方向,否则它也会缩放。 Then I just added the velocity to the position and draw using that Vector. 然后,我只是将速度添加到该位置并使用该Vector进行绘制。

And to show Vectors are awesome you should see this awesome badlogic vs mouse program I created. 为了显示Vectors很棒,您应该看到我创建的这个很棒的badlogic vs鼠标程序。 https://github.com/madmenyo/FollowMouse there are just a view lines of my own code. https://github.com/madmenyo/FollowMouse只有我自己的代码的观点。 It just takes a little bit of vector knowledge and it's very readable. 它只需要一点点矢量知识,并且可读性强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM