# position and rotation

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

## Recommended Posts

I'm not a mathpro but I'm willing to learn, but first I need to know what to learn. I can rotate my character but I don't know how to make the character to move in the direction I'm looking. What do I need to learn and could someone explain basically how it works?

##### Share on other sites
First of all, lacking further information, I'm presuming that this is in 2D.

I think that that depends to some degree on in what form you have your direction. If you have an angle, then I think that I'm correct in stating that it should just be a matter of sine- and cosine- functions (the exact use depending on where you measure 0 degrees from, and in what direction your angles increase, I believe).

Simply put, it should be a matter of trigonometry.

##### Share on other sites
Sry, I forgot to say it's 3D. And trigonometry involves a lot of things, no?

##### Share on other sites
Quote:
 Originally posted by HannesnisulaAnd trigonometry involves a lot of things, no?

Heheh, true - my apologies if I was a bit terse. I blame tiredness. ^^;

Quote:
 Originally posted by HannesnisulaSry, I forgot to say it's 3D.

Fair enough - but is the rotation in three dimensions, or do you just rotate about one axis?

If you do have more than one axis, is it simple angle-and-elevation (i.e. look left-and-right and look up-and-down, as in a standard first-person shooter), or does it involve much twisting, turning and rolling, as in a 3D space shooter?

Finally, in what form do you have your current view direction? A vector (I would guess not, since you're having trouble with moving in that direction)? One angle? Two angles? Quaternions? Some other form?

If you have angles, it boils down, I think, to right-angled triangles, and, as I said, sine and cosine.

For a single view direction (that is, rotation only about, for example, your up-axis), I believe that, if 0 degrees/radians corresponds to looking down the positive y-axis, then your y-component should be cos(angle), and your x-component sin(angle) (although you may find that you'll want to negate one or both of those, depending on the direction in which your angles increase (that is, clockwise or anticlockwise), and whether 0 degrees/radians does indeed correspond to looking down the positive y-axis. Finally, you should be careful of using degrees or radians as appropriate - I don't know which your functions will take.

Otherwise, more specifics might help. ^_^

##### Share on other sites
I dont know if this helps, i am new to game programming my self. In my 2D game i found out to do like this:

x = cos(direction) * velocity +/- x;
y = sin(direction) * velocity +/- y;

(direction is an angle)

the sign between velocity and x or y depends on how the coordinate system is made. in java were 0,0 is in the top left corner is it:

x = cos(direction) * velocity + x;
y = sin(direction) * velocity - y;

the problem is that velocity has to reach a ceartin value to be able to rotate i enough different directions. if velocity = 1, then it will only have like 8 way different ways to walk in. I found out that 7 were the minimum value where it looked good.

I guess that there are better methods out there. Dont know if you can use it for anything in your 3D game.

##### Share on other sites
Quote:
 Originally posted by ismetterenthe problem is that velocity has to reach a ceartin value to be able to rotate i enough different directions. if velocity = 1, then it will only have like 8 way different ways to walk in. I found out that 7 were the minimum value where it looked good.

Hum... Is your velocity value a floating point or integer number? From what you say, I would guess that it's an integer. If so, and whatever language or tool you are using allows it (which it probably will, I would think), I would suggest switching out to a floating point value (usually denoted by "float" or "double" instead of "int" or "integer", I believe). Remember that integer arithmetic tends to result in values after the decimal point being cut away.

If you're using Java, as one of your statements suggests, then a "float" value should work well, I would think.

Otherwise I don't see a reason, offhand, that the value of velocity should affect the number of unique directions in which you may move, to an easily detectable degree, at least.

That said, that looks like more or less what I described, so I concur. ^_^

##### Share on other sites
I'm trying to rotate along the Y axis to turn left and right like in a FPS. I've got the rotation in degrees and I've got velocity set but I don't know how to set the appropriate X and Z values when I move. (I'm really crappy at explaining >.<). If I'm looking 40 degrees to the right of the positive Z axis and want to move in that direction, increase X and Z position values so it actually moves in that direction, there's where I get stuck. I don't know if I could explain much more in detail since I only know all the mathematical terms in swedish:P

##### Share on other sites
I havent ever worked with 3D games... but isent the z dimension normaly the one that goes upwards? To me it sounds like it is your y dimension that goes upwards.

anyway.

i guess you can use the method i posted... just use z instead of y.

##### Share on other sites
ismetteren, the z axes is a horizontal axis, the Y-axis is the one going up.

I don't really get your method. What is X and Y? The position coordinates? Your angle, where does it start at? Is it at the Y-axis going up, clockwise?

It seems to me your velocity has to change to negative if you want to move it to the left and/or up (towars 0,0). How does it know when to change?

Sorry if my bad english confused you.

##### Share on other sites
Im not quite sure i understood yout question, but i will try to explain:

Say we have a plane with the dimensions 300 * 300 pixel's, and (0,0) is in the top left corner like this:

Then we make a point on that plane (0,0) were our actor is at the moment. Now we have an x value and an y value. x = 0 and y = 0.

Then if we have an angle witch is the direction we want to move in, 45 degrees, we can do so:

x = cos(45);
y = sin(45);

since cos(45) nearly= 0.707 and sin(45) nearly= 0.707 , x = 0.707 and y = 0.707.

There are just 2 problems.

The first problem is that this only works as long our actor is in (0,0) wich he only is the first frame.

The second problem is that Since 0 degrees is to the right and when we add degrees we goes counter clockwise(this is how sin and cos works) our new point should be (0.707 , -0.707)(wich is out of the plane) instead of (0.707 , 0.707). the reson it dosent is because that your y axis goes the 'wrong' way.

The first problem can be solved by doing so:
x = x + cos(45);
y = y + sin(45);

The second problem is solved by subtract the y value intead of adding it, since our y axis is going the wrong way:
x = x + cos(45);
y = y - sin(45);

if we want to make the actor move whit more than one pixel per frame, we simply multiply the velocity with the con or sin value:
x = x + (cos(45) * velocity);
y = y - (sin(45) * velocity);

if we want to make the actor able to move in more than one direction, we make a direction variable that stores the direction:
x = x + (cos(direction) * velocity);
y = y - (sin(direction) * velocity);

hope that help'ed.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013393
×