简体   繁体   English

移动对象帮助

[英]Moving Objects Help

I want to know how is it possible,I could have an Object drawn at a certain point and move to the point that is touched on the screen.我想知道怎么可能,我可以在某个点绘制 Object 并移动到屏幕上触摸的点。 I am trying to use it for my game where when the user touches on the screen, the gun fires from the position of the player, but the player is stationary.我正在尝试将它用于我的游戏,当用户触摸屏幕时,枪从玩家的 position 开火,但玩家是静止的。

Thanks in advance.提前致谢。

PS附言

Is there a visual graphic of some sort that shows where every plot is on android.是否有某种视觉图形显示每个 plot 在 android 上的位置。

I don't know what kind of library you're using to draw all of your things, but that basically doesn't matter since you only need to know two things in order to do this:我不知道你用什么样的库来绘制你所有的东西,但这基本上没关系,因为你只需要知道两件事就可以做到这一点:

Without going into specifics on vector geometry:无需详细说明矢量几何:

1. You need to calculate the direction (x and y component) that the projectile moves in depending on your mouses position. 1. 您需要根据您的鼠标 position 计算弹丸移动的方向(x 和 y 分量)。 You get this direction by simply subtracting the position of the mouse from the position of the player:您只需从播放器的 position 中减去鼠标的 position 即可得到这个方向:

//x component of direction
float direction_x = mousePosition.x - playerPosition.x;
//y component of direction
float direction_y = mousePosition.y - playerPosition.y;

In order to just get a direction instead of adding a velocity component to this vector, you need to normalize it (so it has a length of 1):为了获得方向而不是向该向量添加速度分量,您需要对其进行归一化(因此它的长度为 1):

float length =(float) Math.sqrt(direction_x*direction_x + direction_y*direction_y);
direction_x /= length;
direction_y /= length;

You then need to update the projectiles position by adding the direction_x and direction_y components to it, multiplied by the speed that you want the projectile to have (This process is called linear interpolation, by the way):然后你需要更新弹丸 position,方法是向它添加 direction_x 和 direction_y 分量,乘以你希望弹丸具有的速度(顺便说一下,这个过程称为线性插值):

projectile_x += direction_x*speed;
projectile_y += direction_y*speed;

If you have some way of measuring the time between two frames, the speed variable should depend on the elapsed time between those frames, in order to create smooth movements on different platforms.如果您有某种方法可以测量两帧之间的时间,则速度变量应取决于这些帧之间经过的时间,以便在不同平台上创建平滑的运动。

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

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