Believe me, there nothing to benefit from in the elastic’s code. It’s so horrible it could be a perfect example of how not to code. Go along with the shootorials if you haven’t already.
The mechanics are quite easy to write.
The square goes like this:
dist_x = (root.player.x-x)*speedModifier;
dist_y = (root.player.y-y)*speedModi…
show moreBelieve me, there nothing to benefit from in the elastic’s code. It’s so horrible it could be a perfect example of how not to code. Go along with the shootorials if you haven’t already.
The mechanics are quite easy to write.
The square goes like this:
dist_x = (root.player.x-x)*speedModifier;
dist_y = (root.player.y-y)*speedModifier;
xspeed += distx;
yspeed += disty;
xspeed *= friction;
yspeed *= friction;
_x += xspeed;
_y += yspeed;
It’s really easy. It just finds the distance and then adds it to speed, of course multiplied by speedModifier (0.0X), otherwise, it would be instant.
The laser aiming towards square is also quite easy, although I think I used the wrong function:
angle = Math.atan(Math.sqrt((Ax-Bx))/Math.sqrt((Ay-By)(Ay-By)))/0.0174532925;
It’s just simple maths, that are hidden behind code. This is simply tan-1 of angle equals to opposite divided by adjacent. Simple trig. There are the squareroots etc to always keep it positive (maybe there is some function for it, dunno) and the /0.017.. to change it to degrees from radians.
The problem with this is that it doesn’t really say in what corner of the imaginary circle the square is, so I had to use this code to find that out:
if (By > Ay)
{
if (Ax > Bx)
{
angle = 180 + angle;
}
else
{
angle = 180 – angle;
}
}
else
{
if (Ax > Bx)
{
angle = 360 – angle;
}
else
{
}
}
I’ve read about Math.atan2 and how it solves this. I didn’t really understand but it clearly described this problem so I guess it really is the solution and I just didn’t use it right.
Thats pretty much evertyhing, anything else can be learned from the shootorials.
show less