• Advertisement
Sign in to follow this  

3d Angle of facing issues

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

If you intended to correct an error in the post then please contact us.

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.
[code]
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;
}
[/code]

Then theres this class
[code]
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;
}
}
[/code]

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

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.

Thanks in advance.

[url="http://imageshack.us/photo/my-images/26/badthings.png/"][img]http://img26.imageshack.us/img26/5555/badthings.png[/img][/url]

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1326709343' post='4903179']
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.
[/quote]

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
[code]
case 3:
mowY = 1 * ((procValue*angleConst)/100);
mowX = 1 - mowY;
[/code]

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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1326729815' post='4903270']
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.
[/quote]

Hell yes! Thanks! This magic works indeed.

- Ray

Share this post


Link to post
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
[code]
angleInRads = Math.toRadians(horizontalAngle);
mowX = (float) -sin(angleInRads);
mowY = (float) cos(angleInRads);
[/code]

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement