gpt4 book ai didi

java - 在android中计算圆周围的点

转载 作者:行者123 更新时间:2023-12-01 18:38:23 24 4
gpt4 key购买 nike

我正在 Android 中编写一个游戏,其中敌方船只需要以星形图案围绕其自身开火。基本上,对于我设置敌舰开火的任意 X 次射击,我希望敌舰将自身周围的 360 度除以射击次数,并以精确的等边角度进行射击。因此,将发射 3 发子弹,第一发射击为 360 度,第二发射击为 120 度,第三发射击为 240 度。 4 个镜头将是 360,90,180,270 等等。然后每个镜头都会沿对角线向外移动,直到击中屏幕边缘(或途中的其他物体)。到目前为止,一切都很好。这是我的代码 - 我显然做错了什么,因为虽然四枪会向四个相同的方向(北、南、东、西)射击,但三枪或六枪或八枪(等)会向错误的方向射击。只是角度不正确。我很感激对此的任何帮助,因为我已经为此苦苦挣扎了很长一段时间。在下面的示例中,iShotNumber 是 Volley 中的射击次数。距离是镜头在一个“刻度”内移动的距离(以像素为单位)。炮弹以阵列形式围绕船的圆周添加,然后通过每个“滴答声”将炮弹向外推进一步。

围绕船的圆周绘制镜头的逻辑:

public void addShot(Context ctx, int iShotNumber, float distance) {



for (int iShot=1; iShot<=iShotNumber; iShot++) {

double
dAngle =0, //angle of the shot
dCosFactor=0, //factor on the x-axis
dSinFactor=0; //factor on the y-axis

float
fNewX = 0, //new position on the x-axis
fNewY =0; //new position on the y-axis

dAngle = 360/iShotNumber;
dAngle = dAngle*iShot;

if (iShotNumber == 1) {dAngle=180;} //if there's only one shot then fire straight down

if (dAngle!=360 && dAngle!=180) //if its 360 or 180 then fire straight up or straight down - no need for X
{
fNewX = (float) (getShipRadius()*Math.cos(Math.toRadians(dAngle)));

if (dAngle<=180) {fNewX=fNewX+distance;} else {fNewX=fNewX-distance;}

fNewX=fNewX+getXLocation();

}
else {fNewX=getXLocation();}

if (dAngle!=90 && dAngle !=270) //if its 90 or 270 then fire straight right or straight left - no need for Y
{
fNewY = (float) (getShipRadius()*Math.sin(Math.toRadians(dAngle)));

if (dAngle<=90||dAngle>=270) {fNewY=fNewY+distance;} else {fNewY=fNewY-distance;}

fNewY=fNewY+getYLocation();
}
else {fNewY=getYLocation();}


if (dAngle>=90&&dAngle<=180) {dSinFactor = Math.sin(Math.toRadians(dAngle)-90); dCosFactor = Math.cos(Math.toRadians(dAngle)-90);}
else if (dAngle>=181&&dAngle<=270) {dSinFactor = Math.sin(Math.toRadians(dAngle)-180); dCosFactor = Math.cos(Math.toRadians(dAngle)-180);}
else if (dAngle>=271&&dAngle<360) {dSinFactor = Math.sin(Math.toRadians(dAngle)-270); dCosFactor = Math.cos(Math.toRadians(dAngle)-270);}
else if (dAngle==360) {dSinFactor = Math.sin(Math.toRadians(dAngle)-271); dCosFactor = Math.cos(Math.toRadians(dAngle)-271);}
else {dSinFactor = Math.sin(Math.toRadians(dAngle)); dCosFactor = Math.cos(Math.toRadians(dAngle));}

//dSinFactor = Math.sin(Math.toRadians(dAngle)); dCosFactor = Math.cos(Math.toRadians(dAngle));

//if (dSinFactor<=0){dSinFactor = dSinFactor*-1;} //neutralize negative angles on upshots
//if (dCosFactor<=0){dCosFactor = dCosFactor*-1;} //neutralize negative angles on rightshots

if ( MainActivity.iDebugLevel >= 1) {Log.d("EnemyShip-addShot","add shot number " +String.valueOf(iShot)
+ " with dAngle " +String.valueOf(dAngle) +" cosan was " +String.valueOf(dCosFactor) +" sinan was " +String.valueOf(dSinFactor));}
if ( MainActivity.iDebugLevel >= 2) {Log.d("EnemyShip-addShot","add shot number " +String.valueOf(iShot) + " with fNewX " +String.valueOf(fNewX));}
if ( MainActivity.iDebugLevel >= 2) {Log.d("EnemyShip-addShot","add shot number " +String.valueOf(iShot) + " with fNewY " +String.valueOf(fNewY));}

if (dAngle==90||dAngle==270) {newShot = new ShotClass(ctx, fNewX, fNewY, dCosFactor /*x-angle*/, 0 /*y-angle*/);} //fire straight to the right or left
else if (dAngle==360||dAngle==180) {newShot = new ShotClass(ctx, fNewX, fNewY, 0 /*x-angle*/, dSinFactor /*y-angle*/);} //fire straight up or down
else {newShot = new ShotClass(ctx, fNewX, fNewY, dCosFactor /*x-angle*/, dSinFactor /*y-angle*/);} //fire at an angle

if ( dAngle <= 90 || dAngle >= 270) {
newShot.setShotGoingUp(true);
}
else
{
newShot.setShotGoingUp(false);
}

if ( dAngle <= 180 ) {
newShot.setShotGoingRight(true);
}
else
{
newShot.setShotGoingRight(false);
}

if ( MainActivity.iDebugLevel >= 1) {Log.d("EnemyShip-addShot","add shot number " +String.valueOf(iShot) + " with goingup " +String.valueOf(newShot.getShotGoingUp()) +" with goingright " +String.valueOf(newShot.getShotGoingRight()));}

arShots.add(newShot);
if ( MainActivity.iDebugLevel >= 2) {Log.d("EnemyShip-addShot","add shot number " +String.valueOf(iShot) + " with position " +String.valueOf(getXLocation()) +" " +String.valueOf(getYLocation()) +" firing params: " +String.valueOf(dCosFactor) +" " +String.valueOf(dSinFactor) +" angle was " +String.valueOf(dAngle));}
}

以对角线发送镜头的逻辑:

        inpDistance = inpDistance * .2f; //slow down the shot to one fifth speed

for (int iShotInTheSequence=1;iShotInTheSequence<=inpNumberShots;iShotInTheSequence++) {

fFactor = (float) (inpDistance * getYFiringAngle());
if ( getShotGoingUp() ) { //shot is going up

fYLocation = fYLocation - fFactor;
} //shot is going up
else {//shot is going down

fYLocation = fYLocation + fFactor;
} //shot is going down

fFactor = (float) (inpDistance * getXFiringAngle());
if ( getShotGoingRight() ) { //shot is going right
fXLocation = fXLocation + fFactor;
} //shot is going right
else {//shot is going left
fXLocation = fXLocation - fFactor;
} //shot is going left


}

最佳答案

大多数编程语言(包括 Java)中的三角函数都以弧度而不是角度来测量参数。有一个辅助函数可以进行转换,因此无论您在哪里调用 sincos,都可以添加对 toRadians 的调用:

Math.cos(Math.toRadians(dAngle))

关于java - 在android中计算圆周围的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com