Such a simple little thing, I thought. That's now been bugging me for a week. So clearly not so simple. Or staring me in the face. I don't know.
I'm seeking a way to calculate the 'side' that one object is to relative to another object. The purpose is to calculate which side a target has been hit when another target shoots at it.
So here, the green object has 'fired' at the red object (the target). It would be a right side hit.
I have had the system working in a few ways. Previously I just used 'within' and '90 degrees' while plugging in the angle between the two objects, and this seemed to work for calculating front and rear hits. But I need to bring in left and right sides as well, and I can't figure out how to do so. Again, I can calculate a static target, but this system also needs to take into account the rotation of the target.
Develop games in your browser. Powerful, performant & highly capable.
Can't you just use 'within' and '45 degrees' instead?
On impact simply compare the angle of bullet and target, eg:
( Bullet.Angle - Target.Angle ) is within 45 degrees of angle 180 = Front
( Bullet.Angle - Target.Angle ) is within 45 degrees of angle 270 = Left
( Bullet.Angle - Target.Angle ) is within 45 degrees of angle 90 = Right
( Bullet.Angle - Target.Angle ) is within 45 degrees of angle 0 = Rear
You might need to switch your left/right on that, but something similar should work.
Cheers! My issue was that I didn't even realise there was a base system angle within where I was only using an object's.
I used angle(target.X, target.Y, shooter.X, shooter.Y) instead of ( Bullet.Angle - Target.Angle ) since I wasn't sure how that ended with the value I needed, also simply by adding the target's angle onto the second angle I took into account the rotation of the target.