• Create Account

## 3d Angle of facing issues

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Sollum  Members

1090
Like
0Likes
Like

Posted 15 January 2012 - 05:30 PM

Hello,

Recently, i've finished my studies, and since i am a bit unenployed now, decided to get back to my hobby. I am having a trouble with my 3D project, at first, everything seemed fine, but after i started implementing "projectiles", buggs started popping up.

First of, heres the code bit that determines angle i am facing. Note that in code it should be angleZ not Y.
public static float angleX;
private float rotationSpeed;

xRel = Mouse.getDX(); // relativnes from last cursor position

if (xRel > 0)
{
angleY += rotationSpeed * xRel/2;
if (angleY >= 360)
angleY = 0;
}
if (xRel < 0)
{
if (angleY <= 0)
angleY = 360;
angleY -= rotationSpeed * (xRel * -1)/2;
}


Then theres this class
public final class Utils {
public static float angleConst = 100/90;
public static float angleMod = 0;

private static int quarter;
private static int procValue;
private static float placeHolder;

public static float normalizeAngle()
{
placeHolder = angleY + angleMod;
if (placeHolder > 360)
placeHolder -= 360;
else if (placeHolder < 0)
placeHolder += 360;
return placeHolder;
}

public static void setMows()
{
quarter = (int) (normalizeAngle() / 90.0f);
procValue = (int) (normalizeAngle() - 90 * quarter);

switch(quarter)
{
case 0:
mowX = 1 * ((procValue*angleConst)/100);
mowY = 1 - mowX;
mowX *= -1;
break;
case 1:
mowY = 1 * ((procValue*angleConst)/100);
mowX = 1 * (1 - mowY);
mowY *= -1;
mowX *= -1;
break;
case 2:
mowX = 1 * ((procValue*angleConst)/100);
mowY = 1 * (1 - mowX);
mowY *= -1;
break;
case 3:
mowY = 1 * ((procValue*angleConst)/100);
mowX = 1 - mowY;
break;
default:
break;
}
}


And when i press for example S this is what happens
	   	 case 's':
angleMod = 180;
setMows();
x += mowX; y += mowY;
break;


Long story short, whenever i rotate myself, and press walking button, setMows is called, which sets mowX and mowY, sort of "vector" that is latter used when i make a step, to calculate my new x and y. Problem is, that this logic is bad and i need some sort of other algorithm. Here is a picture that illustrates "bad" things. It was done by standing in the midle rotating 360 and placing a "stick" on the ground. Start point is x and y, end point is x + mowX * staticNumber and y + mowY * staticNumber.
Problem is that its not
a) Circle
b) It has blind spots

Maybe someone could redirect me towards some sort of better algorythms? I am not entirely sure i am posting in propper section, tho it seems like a begginer problem.

### #2Álvaro  Members

20268
Like
1Likes
Like

Posted 16 January 2012 - 04:22 AM

I don't understand what you are doing in setMows. If you want to convert an angle to a vector, use (cos(angle), sin(angle)). Be sure to pass the angle in radians.

Better yet, the vast majority of the times you don't need angles at all: Use vectors directly and you can avoid having any special cases for wrapping around 360.

### #3Sollum  Members

1090
Like
0Likes
Like

Posted 16 January 2012 - 05:48 AM

I don't understand what you are doing in setMows. If you want to convert an angle to a vector, use (cos(angle), sin(angle)). Be sure to pass the angle in radians.

Better yet, the vast majority of the times you don't need angles at all: Use vectors directly and you can avoid having any special cases for wrapping around 360.

Could you give some sort of example on that?

Thing is, i do not understand those vector thingies and how to use them, thus i had to create my own method to determine where should my character walk when i press buttons.
What setMows() does is that it determines quarter i am looking at (360/4), then i take procValue, which is from 0 to 90. By knowing quarter i am facing, and angle (procValue), i can adjush X and Y accordingly.
This for example
case 3:
mowY = 1 * ((procValue*angleConst)/100);
mowX = 1 - mowY;


Splits 1 step into % values of X and Y i should add, when i am doing next step. But this calculation is also wrong.

### #4Álvaro  Members

20268
Like
1Likes
Like

Posted 16 January 2012 - 10:03 AM

I already told you how to convert an angle to a step in the x direction and a step in the y direction: Use cosine and sine respectively.

### #5Sollum  Members

1090
Like
0Likes
Like

Posted 16 January 2012 - 10:28 AM

I already told you how to convert an angle to a step in the x direction and a step in the y direction: Use cosine and sine respectively.

Hell yes! Thanks! This magic works indeed.

- Ray

### #6Sollum  Members

1090
Like
0Likes
Like

Posted 17 January 2012 - 09:28 AM

Hi again.

Maybe someone could give me a hand in how to calculate vector if i am shotting for example. I need to use X, Y, Z.

For X and Y i use
angleInRads = Math.toRadians(horizontalAngle);


But if i want to use Z, i have to use verticalAngle too, thus calculations of both mowX and mowY must change + calculation of "mowZ" must appear.

Edit: Solved this up, took few hours, but was worth it. If anyone will be searching this and will stumble upon my problem, just look up into "Spherical Geomtry".

My sollution was in
           	 (float) -(Math.sin(radY)*Math.cos(radX)),