根据角度和速度更新坐标 [英] update coordinates based on angle and speed

查看:36
本文介绍了根据角度和速度更新坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了很多关于这个谜题的答案,但是每次我尝试实施这些答案时,它们似乎都没有用.我想回答一些答案,但是不能.也许是因为我的声誉"水平?

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)

我在屏幕中间有一艘太空飞船.它不会与旋转分开(user.fAngle).它确实具有速度(user.dVelocity),仅用于计算其在世界上的位置.

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.

现在,我使船以1的速度移动.这意味着无论它走到哪里",它都以该速度移动(再次,仅出于地图坐标目的).不会放慢速度或加快速度,但最终会.因此速度最终将是一个变化的变量.

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当然是0-359

user.fangle of course = 0-359

当您开始游戏时,worldX和worldY从0开始.我正在尝试根据飞船的角度和硬编码速度来修改worldX和worldY的每个帧.

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.

在大多数情况下,这是可行的.但是奇怪的是,在某些时候,坐标会冻结.否则它们将停止在0而不是负数.同样,这仅在特定时间和特定角度发生.

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.

我看到的另一个问题是,当数字确实发生变化时,它们总是以一致的速度变化.换句话说,假设north为0.如果我以5度角移动,则worldY变量应该发生很大变化,而worldX应当发生一些变化.这没有发生.

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.

在调试中,我确保user.fAngle确实在0-359的范围内.我不确定还需要检查什么.

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

您需要一个float变量来记住位置.

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

那将解决问题.

这篇关于根据角度和速度更新坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆