Change the direction of the entity image toward the target [SOLVED]


(Zouhair Elamrani Abou Elassad) #1

Hi Guys,

I’ve been working on some missile following the mouse code, i managed to make the missile to follow the mouse, what i hope to achieve now is to change the angle of the missile entity image to point toward the direction of the mouse, i tried to change the Image.angle but it didn’t seem to work :stuck_out_tongue: this is what i managed to do so far :

override public function update():void 
	{
		super.update();

		var angle:int = FP.angle(x, y, world.mouseX, world.mouseY)
		
		
		x += SPEED * Math.cos(angle * FP.RAD) * FP.elapsed
		y += SPEED * Math.sin(angle * FP.RAD) * FP.elapsed
		
		image.angle = angle; // not working
	}

(Zouhair Elamrani Abou Elassad) #2

I managed to make it work by changing the value of the image angle to :

image.angle = angle - 90;

Still don’t undrestand why it worked though.


(Martí Angelats i Ribera) #3

It will deppend of your original image. That angle is the angle of the line between the two points (the first two parameters define the first and the last one the second).

If you want the image to use image.angle = angle you have to make the image point to the right (0º angle). I guess you have it to the top (which is 90º) so that’s why you need to substract 90 every time.


(Zouhair Elamrani Abou Elassad) #4

… Yes, that would be it, the image angle wasn’t set to 0° at first :smile: