简体   繁体   English

HTML5 Canvas,如何显示两点之间的动画攻击

[英]HTML5 Canvas, How to show animated attacks between two points

I want to show attack animation between two points (source & target) on HTML5 canvas, similar to http://map.ipviking.com/ 我想在HTML5画布上显示两点(源和目标)之间的攻击动画,类似于http://map.ipviking.com/

Below code making missile movement too slow at the end when distance remain short and does not seems real like a missile attack 下面的代码使导弹的运动在距离短时结束时太慢,并且看起来不像导弹攻击那样真实

velocityX = target.x - missile.shift.x;
velocityY = target.y - missile.shift.y;
missile.shift.x += velocityX * .001;
missile.shift.y += velocityY * .001;

Please help! 请帮忙!

At row 1-2 you make the velocities depend on the distance between missile and target, shorter distance implies lower speed. 在第1-2行,速度取决于导弹与目标之间的距离,距离越短意味着速度越低。

I guess what you seek is the direction, normalized, and then to choose the speed by yourself. 我想您要寻找的是方向,将其标准化,然后自己选择速度。

Let me show a simple example. 让我展示一个简单的例子。

Target is at: [2, 3]
Missile is at: [4, 8]

Velocities (by your formula) are: [2, 5] (the diff on x and y separate) The "normalizationFactor" is calculated by the "pythagoras formulae": 速度(根据您的公式)是:[2,5](x和y上的差异分开)“ normalizationFactor”由“毕达哥拉斯公式”计算:

var normalizationFactor= Math.sqrt(2*2 + 5*5);

To normalize, you need to shorten both velocityX and velocityY by normalizationFactor . 要进行规范化,您需要通过normalizationFactor来同时缩短velocityXvelocityY After that, you can chose which number to like to choose your speed with. 之后,您可以选择喜欢的号码来选择自己的速度。 (you use 0.001 by now) (您现在使用0.001)

This is my attempt to patch your solution, haven't tested it, but you should get the idea: 这是我试图修补您的解决方案的尝试,尚未测试,但是您应该了解一下:

velocityX = target.x - missile.shift.x;
velocityY = target.y - missile.shift.y;
var normalizationFactor= Math.sqrt(velocityX*velocityX + velocityY*velocityY);
var yourSpeed = 0.001;
missile.shift.x += yourSpeed * velocityX / normalizationFactor;
missile.shift.y += yourSpeed * velocityY / normalizationFactor;

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

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