简体   繁体   English

如何将物体从一个点以固定角度均匀地移动到另一点?

[英]How to move an object uniformly from one point to another at a fixed angle?

在此处输入图片说明

In my 2d java game, I need to move projectiles from Enemy object to Player object in a straight line. 在我的2D Java游戏中,我需要将弹丸从敌人对象直线移动到玩家对象。 I did some research and found out that I can use atan2(x, y) method of Math class to get the angle, but then how am i supposed to move the projectile in that particular angle(direction)? 我做了一些研究,发现我可以使用Math类的atan2(x,y)方法来获取角度,但是我应该如何在特定角度(方向)上移动弹丸? Any ideas would be very helpful. 任何想法都将非常有帮助。

The equation of movment with constant speed is simple 等速运动方程很简单

positionX = startPositionX + speedX * time 
positionY = startPositionX + speedY * time 

where 哪里

speedX = speed * sin(angle)
speedY = speed * cos(angle)

But most of the game use incremental aproach 但是大多数游戏都使用渐进方式

posX = posX + speedX / lengthOfTick
posY = posY + speedY / lengthOfTick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM