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

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

问题描述

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

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.

这是我现在拥有的..

    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.

我看到的另一个问题是,当数字确实发生变化时,它们总是以一致的速度变化.换句话说,假设北为 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.

推荐答案

你的问题来自于:

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

这样就能解决问题了.

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

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