开发者

update coordinates based on angle and speed

开发者 https://www.devze.com 2023-03-31 00:40 出处:网络
I see plenty of answers to this riddle on here, but every time I try and implement the answers, they don\'t seem to work. I wanted to reply to some of the answers, but couldn\'t. Perhaps it\'s because

I see plenty of answers to this riddle on here, but every time I try and implement the answers, they don't seem to work. I wanted to reply to some of the answers, but couldn't. Perhaps it's because of my 'reputation' level?

Anyway, it's a simple coordinates problem for a game. Simple for most, but not for me. (suck at math.. hardcore)

I've got a spaceship in the middle of the screen. It does not move apart from rotation (user.fAngle). It does have velocity (user.dVelocity) that is used purely for calculating its location in the world.

Right now I have the ship moving at a velocity of 1. Which means wherever it 'goes', it's moving at that velocity (again, purely for map coordinate purposes). Doesn't slow down or 开发者_如何转开发speed up just yet.. but will eventually. So velocity will eventually be a changing variable.

Here is what I have now..

    double radians = (Math.PI / 180) * ( user.fAngle); 
    worldX = (int)(worldX + user.dVelocity * Math.cos(radians)); 
    worldY = (int)(worldY + user.dVelocity * Math.sin(radians));

user.fangle of course = 0-359

worldX and worldY start at 0 when you begin the game. I am trying to modify worldX and worldY each frame based on the ship's angle and hardcoded velocity.

For the most part, this works. But what's odd is that at certain times, the coordinates will freeze. Or they'll stop at 0 and not go into the negatives. Again, this only happens at certain times and at certain angles.

The other issue I'm seeing is that when the numbers do change, they are always changing at consistent speeds. In other words, let's say north is 0. If I'm moving at an angle of 5, the worldY variable should be changing a lot, but worldX should be changing a little. This is not happening.

In my debugging, I've made sure that user.fAngle is indeed within the range of 0-359. I'm not sure what else to check.

Thanks for reading, and I appreciate the help.


Your problem comes from the fact that :

int x = 0;
x = x + 0.5; // rounding down : x == 0
x = x + 0.5; // rounding down : x == 0

You need a float variable to memorize the position.

float x = 0;
x = x + 0.5; // x == 0.5
worldX = (int)x; // worldX == 0
x = x + 0.5; // x == 1
worldX = (int)x; // worldX == 1

That will solve the problem.


Not sure if this would work, but try multiplying the angle by PI before dividing by 180 when converting the angle to radians.

double radians = (Math.PI * user.fAngle) / 180; 

EDIT:

I just noticed that worldX and worldY have type int. You should change their type to double, since the displacements are most likely fractional.


"...let's say north is 0. If I'm moving at an angle of 5, the worldY variable should be changing a lot, but worldX should be changing a little. This is not happening."

The formulas you gave for the X and Y motions actually assume that north is 90 degrees.

In trigonometry, an angle of 0 degrees is along the +X axis, which would correspond to due east on a map. Also, in trigonometry, increasing the angle moves it counter-clockwise around the origin. If you want your angles to act like compass headings, with north at 0 degrees, east at 90 degrees, and so on, you should define radians like this:

double radians = (Math.PI / 180) * ( 90.0 - user.fAngle);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号