help with firing arcs

Started by
16 comments, last by Cruis.In 11 years, 3 months ago
I dunno why you are using trig for this, use vectors. Dot product of your normalised facing direction and the normalised vector to a target gives you the cosine in radians of the angle between your facing direction and the target direction.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

Paradigm: I understand somewhat that you have said, but not being much of a mathmetician I need it more layman.

How do I calculate the dot product of my normalised facing (which I believe is my ships angle) and how do I calculate the dot product of the vector to my target (which I believe is the angle of the target from me?)

Once done I am assuming from what you said, this gives the angle from the enemy vector to my facing vector. Once I have that angle, I can then allow "fire" once that angle falls within a certain value. And am I correct in saying that this would mean that once I am facing the enemy, the value of the angle between my facing direction and the target direction will be = to 0

Others

no the cone isn't for range, it merely looks like a cone because of the lines etc.

the lines are just imaginary they do are drawn, it's just showing the vectors of the limits of your firing arc, merely to show the width of the arc I was seeking.

So basically its a front arc. If the target is in front of you, only then can you fire. Its top down 2d view. the 'camera' is focused on your ship which remains at the centre of the screen. You can fly about the game world, and rotate, similar to asteroids, but your ship remains drawn at the centre of your screen even tho u are moving, so unline asteroids in that aspect.

So imagine asteroids, imagine that you could only fire if the enemy was in front of you.

But I realise I think I have over complicated it and kind of fooled myself. Since it doesn't make sense to only be ABLE to fire if an enemy is in front of you. You should be able to fire all the time from the front guns on your ship.

What really complicated this is that at first I am using the mouse pointer as the aimer. So I can getting the angle of the mouse pointer relative to your ship, and offsetting it with your current direction of facing. So if the mouse pointer falls within your frontal firing arc, you can fire in that direction, which is the front. So the front of the ship has a 60 degree arc. -30 to the left and 30 to the right. So the angle of the mouse cursor to my ship + the offset of my current ship angle was being calculated and held in a variable. I then stipulated that if that value is = within a certain range you can fire. These range is = to a small 60 degree arc.

So now I changed it to be independent of the mouse pointer, but instead to calculate where the enemy ship angle is relative to your ship, and if the enemy ship falls within those specified arcs to the front, then you can fire. Which really is a work around, because this means you cannot fire AT ALL unless an enemy ship is in front.

What I could do is allow you to fire all the time if the enemy is out of arc, but set the projectile to go straight ahead.

THen if the enemy is within the arc, the projectile is aimed at the vector of the enemy from your ship. So your allowed to fire all the time, whether a target is directly in front of you or not, just that the projectile will go straight. Now once an enemy peeps his little nose in front of your ship, the projectile will go for him.

Thank you guys all for your help, regardless of whether I rap my head around it I do understand more. Sorry to waste your times.

If you are facing northeast, your direction vector would be in the direction of (+1, +1), normalised this gives (1/sqrt(2), 1/sqrt(2)).

The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 5-0) = (-5, 5), normalised (-1/sqrt(2), 1/sqrt(2)).

Dot product between those vectors is -1/2 + 1/2 = 0.

This is the cosine of the angle between you, and cos(90degrees) = 0, so they are perpendicular to your facing.

If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.

All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.

Hope that helps.

EDIT: Maths fail, oops ;) Fixed now
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

disregard

ok to normalize a vector

divide its x,y by its length

the vector in our example is -5,5 (vector towards target from player)

length = sqr( (-5 * 5) + (5 * 5))

length = sqr ( 25 + 25)

length = sqr (50)

length = 7.07

didn't you make a mistake in your magnitude calculation? since the brackets must be added before you find the square root?

normalized = -5/7.07 = -0.7

5/7.07 = 0.7

This is the cosine of the angle between you, and cos(90degrees) = 0, so they are perpendicular to your facing.

now that part I do not get. Could you word that better?

The dot product of the two (normalized) vectors is the cosine of the angle you want.
So after you have taken the dot product, you feed the result to acos()/arccos()/whatever, and you have the angle.
It will be in radians instead of degrees; if you are more comfortable thinking in degrees, you can convert between the two.
Full circle is 2*pi radians or 360 degrees, so if angle X is "Xdeg" in degrees and "Xrad" in radians, Xdeg = 360 / (2 * pi) * Xrad, and Xrad = 2*pi / 360 * Xdeg.

thanks for the reply,

ok im nearly there.....

Thanks for all the help guys! Helped me wrap my head around it.

Haven't done this kind of math in like 20 years now. After many fullscaps later, I understand it a lot better!

This topic is closed to new replies.

Advertisement