Less Precision For Better Looks

(Zouhair Elamrani Abou Elassad) #1


I managed to make the missile follow the target with a high precision using the code :

   var angle:int = FP.angle(x, y, _player.x, _player.y)
   x += SPEED * Math.cos(angle * FP.RAD) * FP.elapsed
   y += SPEED * Math.sin(angle * FP.RAD) * FP.elapsed

   image.angle  = angle;

What i hope to achieve is to have a Less precision for better looks just like this :

missile.swf (2.5 KB)

(Jean) #2

You’ll have to control how much the angle can turn every time this function is called, if you just set the perfect angle everytime its target moves, then it will be too much precise.

Now, what numbers you need to use to multiply by FP.elapsed it’s up to you to decide, but start trying to make it 10, it will be a dumb missile, since it would require 18 seconds to make a return and 36 seconds to do a full spin.

Basicly it would be something like this:

var angle:int = FP.angle(x, y, _player.x, _player.y);
//needs to make the value absolute since it can turn clockwise or the other way
if(abs(angle - OldAngle) > (FP.elapsed * 10)){
  angle += FP.elapsed * 10;
x += SPEED * Math.cos(angle * FP.RAD) * FP.elapsed;
y += SPEED * Math.sin(angle * FP.RAD) * FP.elapsed;

image.angle  = angle;
oldAngle = angle;

(Zouhair Elamrani Abou Elassad) #3

Thank you, i’m checking this out, it the fact that the target is moving continuously won’t give the missile the time to seem dumb, i even tried FP.elapsed * 0.001 but i don’t get to see that dump turn :).

(Jean) #4

Ah sorry, I saw my mistake :smile:

That line: angle += FP.elapsed * 10;

Should be: angle = oldAngle + FP.elapsed * 10;

(Zouhair Elamrani Abou Elassad) #5

Thanks for the feedback, After doing that my missile is moving in linear line moving away from the player :stuck_out_tongue:

(Jean) #6

Ah crap, I totally forgot about that. It’s because the algoritm is only adding to angle and never subtract from it when needed.

It lacks optimization, but just to make things work is basically needs this workaround:

//needs to make the value absolute since it can turn clockwise or the other way
if(abs(angle - OldAngle) > (FP.elapsed * 10)){
  if(angle - OldAngle > 0){
    angle = oldAngle + FP.elapsed * 10;
    angle = oldAngle - FP.elapsed * 10;

(Zouhair Elamrani Abou Elassad) #7

I tried to make changes, still the same, i check it never enters to : angle = oldAngle - FP.elapsed * 10;

(Jean) #8

Now that I saw… your angle is an int, it will never work with decimal values, probably won’t fix the problem, but can you try to make angle and oldAngle as Number?

(Zouhair Elamrani Abou Elassad) #9

I made the changes, now it’s much better, but there is one problem, the missile goes around and around an old position of the target, even of the target moves, the missile won’t follow, i also had to change the code to this :

        if(Math.abs(angle - oldAngle) > (FP.elapsed * 30)){
		  if(angle - oldAngle > 0){
				angle = oldAngle - FP.elapsed * 30; // A Minus here
			else {
				angle = oldAngle + FP.elapsed * 30; // A Plus here

(Jean) #10

Did you forgot to add oldAngle = angle;? This is the only reason I can think that could make the missile forget that it’s target moved and start to move in circles.

PS: Good job seeing that I inverted the signals, it’s hard to code without testing :smile:

(Zouhair Elamrani Abou Elassad) #11

Thanks, nope i actually add it just like this :

 image.angle  = angle;

 oldAngle = angle;

I think it doesn’t enter the second condition : else { }, i’m gonna try to play a bit with the code and hopefully make it work, from another hand if you could test it or if you have any advice i’m all ears :smile:.