math question
Displaying 1-8 of 8 total.
1
locke
|
I failed math about 15 years ago... so I need some math help from one or two of you smart folks.
I'm (clearly by now) doing an "Asteroids" type deal, and I now have the rotation down... but I'm having trouble figuring out how to move a ship in said direction.
So let's say I have a variable called "player_dir"... and let's assume that I'm moving it in increments of 6. I can, of course, change that if it helps at all, but the rotational speed works ok at 6.
...anyway... I can turn the thing, but when I want to move it forward, what exactly am I doing? I'm basically calculating the vector, and then traveling the vector. But... and here's where my lack of math starts showing... how do I do that given only a 360 degree-based direction?
Man... when is that mapping code going to come out? I need to get back to RPGs. ;|
Thanks.
-l
Posted on 2004-02-24 23:43:18
|
andy
|
Every vector can be cut into x and y components. sin(theta) will return the y component of a vector of length 1 (aka a normal vector). cos(theta) returns the x component.
Since VC is all ints all the time, sin and cos return fixed point 16.16 integers. Basically, this means that 65536 == 1.0, 32767 == 0.5, and so forth.
What I would do is store all your positions in this 16.16 position, then divide by 65536 (or shift right 16 bits) when you draw.
A consequence of the 16.16 fixed point thing is that multiplication will yield a result that's 16 bits too big. You have to divide again after every multiply.
So, you could do something like:
#define FIXED int
struct Point
{
FIXED x;
FIXED y;
}
Point shipPosition;
int shipAngle;
FIXED shipSpeed;
void moveShip()
{
FIXED x = (cos(shipAngle) * shipSpeed) >> 16;
FIXED y = (sin(shipAngle) * shipSpeed) >> 16;
shipPosition.x += x;
shipPosition.y += y;
}
Posted on 2004-02-25 02:10:06 (last edited on 2004-02-25 02:30:31)
|
locke
|
Oooo. That's tasty.
You're smart. Thanks, SB.
...well... hmm. It produces some seriously odd results... but I think with some tinkering I might be able to hack through this. You definitely got the ship moving. :)
-b
Posted on 2004-02-25 02:32:07 (last edited on 2004-02-25 02:44:41)
|
blues_zodiakos
|
Oh gods, things like sin and cos hurt my brain in ways that should be illegal.
-edit
My spelling should be illegal too.
Posted on 2004-02-25 08:46:49 (last edited on 2004-02-25 08:48:42)
|
andy
|
It's really not that complicated. If you draw a unit circle, and draw a line from the center to a point on that circle, at some angle theta, then sin(theta) will be the y coordinate of the point. cos(theta) will be the x coordinate of the point.
That's really all there is to it. :)
Posted on 2004-02-25 15:01:06
|
blues_zodiakos
|
theta?
Posted on 2004-02-25 19:00:31
|
anonymous
|
theta, greek symbol assigned to the word angle
Posted on 2004-02-25 19:06:17
|
TomT64
|
theta is the angle. Like 90, 180, 45, etc
Posted on 2004-02-25 19:06:56
|