I want some of he AI to look directly at the player, but just like how I have the player set up, I don't want the AI to just change direction, I want them to swing that direction. I obviously do that by setting the direction number to go to, then add or subtract 1 until the AI hits that direction then stop. The only problem I have is calculating that direction. The detection I'm using is a mathematical circle that sits around the AI, so when the player is within say 100 pixels of the AI, the AI swings around to that direction, so it most likely going to need some kind of formula to determin what angle the character is entering from.
4th January, 2009 at 20:46:55 -
Sorry, I explained this like a retard, I've been up for 2 days working on this.
create an invisible detector with full 32 directions for each enemy and make it follow it (only possible via fastloop).
Make it aim in the players direction all the time and by the time you want your enemy swing around just subtract 1 of the direction until the same value of the detector is reached.
I guess this is far to complicated if the object can do the same but it is possible
If you dont want to have a hundred enemys on screen at the same time, the detectors wont leed to a lag.
Advanced Direction extension is the easy way. The pure maths way looks something like this:
Object1 = Enemy Object (the one doing the searching)
Object2 = Player Object (the target)
Angle1 = Current angle
Angle2 = Angle to target object
RotateDir = Angle to target object, in range -180 to 180 (ie. degrees clockwise/anticlockwise)
RotateSpeed = Degrees to rotate per frame
Set Angle2 to: ATan2( Y("Object2")-Y("Object1"), X("Object1")-X("Object2"))
Set RotateDir to: ((Angle1 - Angle2 + 540) mod 360) - 180
if RotateDir < RotateSpeed:
Set Angle1 to: Angle1 + RotateSpeed
if RotateDir > 0 - RotateSpeed:
Set Angle1 to: Angle1 - RotateSpeed
Edited by Sketchy
5th January, 2009 at 18:55:14 -
I tried that, and I also stumbled upon the direction calculator which worked in one line of code. Which one is overall alot better and is most likely not going to cause as much problems in the future?
It's better use an extension for this - it's easier and faster (in terms of not lowering the games framerate).
The big downside to it, is that the rotation must be a whole number. Occassionally you'll find that you need more precise control, and then you'll have to do it the hard way.
I don't remember exactly what the Direction Calculator does, and I only have my laptop at the moment, so I can't check. If it *can* handle floats and works for duplicates, then it's probably the way to go.
Incidentally, the complicated way I suggested still needs a line or two to be perfect - at the moment, the margin of error is equal to RotateSpeed.
I suppose another possible solution would've been having an invisible clone of your object and having it constantly look towards whatever you want. Then when the time comes, do something like:
Dir("Original") < Dir("Clone") -> add/subtract 1 to Dir("Original")