繁体   English   中英

Java LibGDX 基于角度和位置的相对值加法

[英]Java LibGDX relative value addition based on angle and position

我创建了一个类似于经典 diepio 的 2d 游戏。 我创建了一个系统,用于将玩家的枪管定位在特定方向。 更新的角度被发送到服务器。 当玩家点击时,服务器会创建一个导弹。 这只有在枪管连接到玩家身体中心时才能正常工作。 当我想将枪管从身体中心移开时,出现了问题。 我不知道如何更新弹丸产生的服务器端位置。

在下图中,枪管围绕玩家身体的中心旋转。 我用红线标记了导弹的飞行路径。
图像1

在这张图片上,枪管在玩家的中心也有一个旋转轴,但是已经移到了一边。 绿线标出了导弹应该走的路线。 不幸的是,我不知道如何正确地做到这一点。
图2

如何根据玩家的旋转角度和他的位置,从基本距离(如果枪管未移动)将弹丸的生成点更新给定距离(例如 10)?

弹丸生成方法:

float angle = (float) ((player.getRotation() + 90) * Math.PI / 180f);

float forceX = (float) Math.cos(angle);
float forceY = (float) Math.sin(angle);

spawnProjectile(player.getPosition().x + (forceX * 3f), player.getPosition().y + (forceY * 3f));

如果我正确理解了您的问题,您希望在下图中找到以橙色标记的两点:

在此处输入图像描述

由于您知道导弹应该飞行的方向(红线),与中心位置的距离(例如 10),并且您知道导弹的运动矢量(红线)与导弹的两个起始位置之间的连接线(在图像中标记为黑线),您可以像这样计算结果位置:

float angle = (float) ((player.getRotation() + 90) * Math.PI / 180f);

float forceX = (float) Math.cos(angle);
float forceY = (float) Math.sin(angle);

// the center point (start of the red line in the image)
float centerX = player.getPosition().x + (forceX * 3f);
float centerY = player.getPosition().y + (forceY * 3f);

float offsetFromCenterDistanceFactor = 1f; // increase this value to increase the distance between the center of the player and the starting position of the missile

// the vector towards one of the starting positions
float offsetX1 = forceY * offsetFromCenterDistanceFactor;
float offsetY1 = -forceX * offsetFromCenterDistanceFactor;

// the vector towards the other starting position
float offsetX2 = -offsetX1;
float offsetY2 = -offsetY1;

//spawn the upper missile
spawnProjectile(centerX + offsetX1, centerY + offsetY1);
//spawn the lower missile
spawnProjectile(centerX + offsetX2, centerY + offsetY2);

有关正交向量计算的更多详细信息,请参见此答案

暂无
暂无

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

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