[英]How to shoot a bullet in the opposite way in libGDX
I have created the function of a game that a bullet goes to the way of the crosshair with a constant speed. 我创建了游戏的功能,使子弹以恒定的速度进入十字准线的方向。 But I have a question: how can I make the bullet go the opposite way to the way it has been shot? 但是我有一个问题:我怎样才能使子弹与射击方式相反?
Let me explain what I am trying to do. 让我解释一下我要做什么。 When the bullet touches a specific material, it goes the opposite way. 当子弹碰到特定材料时,它的方向相反。 Like this image: 喜欢这张图片:
As you can see, the bullet is "bouncing" to the opposite way. 如您所见,项目符号正以相反的方式“弹跳”。
Here is my function where I am setting the linear velocity from the bullet if you need it: 这是我的函数,如果需要,我可以在其中设置子弹的线速度:
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);
Hope you could understand my idea. 希望你能理解我的想法。 Can anyone help me with this? 谁能帮我这个?
After thinking about how can i do this, I had an idea. 在考虑了我该怎么做之后,我有了一个主意。
According to vector mathematics, the bullet can take these values after it touches the material in question for the rebound ... 根据向量数学,子弹在碰到有问题的材料以反弹时可以取这些值...
As you can see in the picture: 如您在图片中看到的:
After this analysis, i have adapted to a simple code. 经过分析,我适应了一个简单的代码。
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);
I evaluated the linearVelocity vector and then i modified the signs of it. 我评估了linearVelocity向量,然后修改了它的符号。
With this code and anylisis everything works fine!. 使用此代码和anylisis,一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.