Public Group

# 3d Angle of facing issues

This topic is 2520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites
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); mowX = (float) -sin(angleInRads); mowY = (float) cos(angleInRads); 

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)), (float) (Math.cos(radY)*Math.cos(radX)), (float) (Math.sin(radX)), 

where radX is verticalAxis and radY is horizontalAxis (some name fixing upcoming!)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013231
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!