Sin Cos Question

TLFTN

New member
Joined
Jun 9, 2014
Messages
5
Hi all,

I'm creating a PC game in which I have to shoot out a bullet from the pipe of a gun.
As I am not to good with trigonometry I had to improvise a little... then I came up with this formula:

position in x-axis: 14 * Sin(RotationAngle)- 37 * Cos(RotationAngle)
position in y-axis: 37 * Sin(RotationAngle)- 14 * Cos(RotationAngle)

and in a more mathematical form:

x-axis: c * Sin(a) - z * Cos(a)
y-axis: z * Sin(a) - c * Cos(a)
NOTE: z and c are variables

so here comes the question: Is there an easier way to write this function? (easier would equal faster in programming languages, it is faster to calculate one trig function than two)

The best way would either be one sin or one cos function: ex. Sin(v) and the next best would be s * Sin(v)

Thanks in advance :)
 
hmm

All I can see to "speed up some" is:
u = SIN(a), v = COS(a) ; so:
c*u - z*v
z*u - c*v

Saves calculating SIN(a) and COS(a) once each...

Actually that is the exact way I have written it in code :), but for demonstration I put it in a different form.
If you can't simplify my function, then is there an easier way of finding out the position of "a"?

X=player position

Rotation angle=0(radians), difference in Y-axis=14 difference in X-axis=37

a
--X

Rotation angle=pi/2(radians), difference in X-axis=-14, Y-axis=37

--a
X

Rotation angle=pi(radians), difference in X-axis=-37, Y-axis=-14

X
--a

Rotation angle=3*pi/2(radians), difference in X-axis=14, Y-axis=-37

--X
a

BTW, the difference is calculated like this: X=PlayerPos.X-TargetPos.X and Y=PlayerPos.Y-TargetPos.Y , and the x-axis goes from (-infinity to infinity) from left to right and y-axis goes from (-infinity to infinity) from top to bottom. Graphically explained:

(-10)
|
(0)------(10)
|
(10)

If need be I can post real images :D
 
Last edited:
Clarification?

Actually that is the exact way I have written it in code :), but for demonstration I put it in a different form.
If you can't simplify my function, then is there an easier way of finding out the position of "a"?

X=player position

Rotation angle=0(radians), difference in Y-axis=14 difference in X-axis=37

a
--X

Rotation angle=pi/2(radians), difference in X-axis=-14, Y-axis=37

--a
X

Rotation angle=pi(radians), difference in X-axis=-37, Y-axis=-14

X
--a

Rotation angle=3*pi/2(radians), difference in X-axis=14, Y-axis=-37

--X
a

BTW, the difference is calculated like this: X=PlayerPos.X-TargetPos.X and Y=PlayerPos.Y-TargetPos.Y , and the x-axis goes from (-infinity to infinity) from left to right and y-axis goes from (-infinity to infinity) from top to bottom. Graphically explained:

(-10)
|
(0)------(10)
|
(10)

If need be I can post real images :D

Where are you getting your rotation angle from, is that a calculation from a previous to a present position location. Is the pipe gun blower moving as he shoots? I assume the target is moving. From your explanation it seems that there is no rotation angle input just pixel locations. (?) M. Denis is your expert here but my questions seems pertinent.
 
Where are you getting your rotation angle from, is that a calculation from a previous to a present position location. Is the pipe gun blower moving as he shoots? I assume the target is moving. From your explanation it seems that there is no rotation angle input just pixel locations. (?) M. Denis is your expert here but my questions seems pertinent.

I'm sorry that I didn't specify clearer :) , but the TargetPos (the "a") is the point from where the bullet is supposed to fly out from(the pipe). To make things even more clear I'm attaching a few images.

Some clarification on what the dots and lines mean:
-The red dot = position of player
-The blue dot = position of bullet
-The blue line = the difference in x-axis
-The red line = the difference in y-axis

And as I couldn't fit the angle inside the pictures I'll give them now:

top-left = pi/2
top-right = pi
bottom-left = 3*pi/2
bottom-right = 0

and the differences in the axises:

top left: in X-axis = -14, Y-axis = 37
top right: in X-axis = -37, Y-axis = -14
bottom left: in X-axis = 14, Y-axis = -37
bottom right: in X-axis = 37, Y-axis = 14

NOTE: If you want to check these by yourself use the smaller picture! :)
 

Attachments

  • Help.png
    Help.png
    13.4 KB · Views: 21
  • HelpBig.jpg
    HelpBig.jpg
    14.2 KB · Views: 0
More clarification ?

Oh, so your pics are looking down on the character from the top. I don't see a blue bullet but I presume it is some distance out front of the gun muzzle.

Now you have the location of the player, red dot as some (x,y) with respect to the screen (possibly one corner of the screen) and you know the gun muzzle is pointing in the direction that the character faces and located 14 unit to his right and 37 units forward and so ... can the character turn continuously through a full rotation (probably) or only in jumps of 90 degrees, if continuous how do you aim the character. The input from the joystick to the program must be a pair of coordinates (xp,yp) , is that right?

It seems to me that the location of the muzzle lies on a fixed circle about the players position (red dot) of radius SQRT(14^2 + 37^2) and you want what then, just the location of the muzzle or the line running outward along the axis of the gun barrel.

Still trying to get clear your basic input and desired output:

Input to the program from the joystick results in a pair of coordinates indicating the characters position, and a second pair of coordinates indicating the direction that the player is facing, and you want to calculate an equation for either the position of the muzzle or the equation of a line emanating from the barrel. Is that it?

Also do you actually use a trig function to look up Your sines and cosines or calculate them yourself from your coordinate data. I seem to recall that hardware only implements addition and multiplication everything else is look up table or additional calculation.

Well, you might well have figured everything by this point anyway.
 
Q1=can the character turn continuously through a full rotation (probably).
Q2=The input from the joystick to the program must be a pair of coordinates (xp,yp) , is that right?

Q3=It seems to me that the location of the muzzle lies on a fixed circle about the players position (red dot) of radius SQRT(14^2 + 37^2) and you want what then, just the location of the muzzle or the line running outward along the axis of the gun barrel.

Q4=Input to the program from the joystick results in a pair of coordinates indicating the characters position, and a second pair of coordinates indicating the direction that the player is facing, and you want to calculate an equation for either the position of the muzzle or the equation of a line emanating from the barrel. Is that it?

Q5=Also do you actually use a trig function to look up Your sines and cosines or calculate them yourself from your coordinate data. I seem to recall that hardware only implements addition and multiplication everything else is look up table or additional calculation.

I'll start by answering these questions first.
A1 = the character can turn 360 degrees and as little as wanted at a time, so in theory there are an infinite number of angles(not really). Full rotation it is.
A2= Not quite. The input is: WalkForward, WalkBackward, TurnLeft(decrease rotation angle), TurnRight(increase rotation angle). These commands are given with the keyboard arrows(Each command respectively in this order: Up, down, left, right).
A3=I only want the location of the muzzle, but the distance to the muzzle is not enough as I also need to know difference in x and in y. As you can not just say PlayerPosition+SQRT(14^2+37^2), because PlayerPosition is in fact a two dimensional vector and you can't add constants to that.
A4=See A2 for input and A3 for muzzle position.
A5=I am using a trig function, so I can not answer this question 100% accurate, but I believe that the trig function has the most basic angles in a lookup table. An example of an angle that is being used in game: player.RotationAngle = 1.85000288 (radians), this is an angle that I believe has too many decimals to be a lookup value.

my current function as it stands in code:
ProjectileList.Add(new Bullet(dmg, speedOfBullet, range, new Vector2(Game1.player.position.X + sin14 - cos37, player.position.Y - sin37 - cos14), player.RotationAngle + myAngle));

sin14, cos37, sin37, cos14 all stand for numbers*trigFunction(palyer.rotationAngle). Ex. sin14= 14*Sin(player.rotationAngle)
new Vector2 just equals two values, one in x and one in y (x on the left side of the comma and y on the right)
range and dmg are un-necessary variables for calculatin position

speedOfBullet = the value what the bullet will travel with, this means that after the position has been calculated the bullets position will change with this formula: position.X -= (e * speedX); position.Y -= (e * speedY);

speedX=speedOfBullet*Math.Cos(Bullet.RotationAngle) and speedY=speedOfBullet*Math.Sin(Bullet.RotationAngle)
e= time elapsed since last time, should be on my computer 1/60 seconds
and Bullet.RotationAngle stands for: player.RotationAngle + myAngle, where myAngle = a random value that makes the gun not to fire all the bullets in the exact same direction.

And while this is working I'd like it to work better as my true goal is to publish the game to mobile devices and as they are not as powerful as my computer, no optimizations are bad :)
 
Speculation

I'll start by answering these questions first.
A1 = the character can turn 360 degrees and as little as wanted at a time, so in theory there are an infinite number of angles(not really). Full rotation it is.
A2= Not quite. The input is: WalkForward, WalkBackward, TurnLeft(decrease rotation angle), TurnRight(increase rotation angle). These commands are given with the keyboard arrows(Each command respectively in this order: Up, down, left, right).
A3=I only want the location of the muzzle, but the distance to the muzzle is not enough as I also need to know difference in x and in y. As you can not just say PlayerPosition+SQRT(14^2+37^2), because PlayerPosition is in fact a two dimensional vector and you can't add constants to that.
A4=See A2 for input and A3 for muzzle position.
A5=I am using a trig function, so I can not answer this question 100% accurate, but I believe that the trig function has the most basic angles in a lookup table. An example of an angle that is being used in game: player.RotationAngle = 1.85000288 (radians), this is an angle that I believe has too many decimals to be a lookup value.

my current function as it stands in code:
ProjectileList.Add(new Bullet(dmg, speedOfBullet, range, new Vector2(Game1.player.position.X + sin14 - cos37, player.position.Y - sin37 - cos14), player.RotationAngle + myAngle));

sin14, cos37, sin37, cos14 all stand for numbers*trigFunction(palyer.rotationAngle). Ex. sin14= 14*Sin(player.rotationAngle)
new Vector2 just equals two values, one in x and one in y (x on the left side of the comma and y on the right)
range and dmg are un-necessary variables for calculatin position

speedOfBullet = the value what the bullet will travel with, this means that after the position has been calculated the bullets position will change with this formula: position.X -= (e * speedX); position.Y -= (e * speedY);

speedX=speedOfBullet*Math.Cos(Bullet.RotationAngle) and speedY=speedOfBullet*Math.Sin(Bullet.RotationAngle)
e= time elapsed since last time, should be on my computer 1/60 seconds
and Bullet.RotationAngle stands for: player.RotationAngle + myAngle, where myAngle = a random value that makes the gun not to fire all the bullets in the exact same direction.

And while this is working I'd like it to work better as my true goal is to publish the game to mobile devices and as they are not as powerful as my computer, no optimizations are bad :)

Well this is all that I can say, others may have more insight.

The location of the gun muzzle is the vector from a reference point (probably a corner of the screen) to the player plus the vector from the player to the muzzle. You have already worked out the equations needed to give you the muzzle location and optimized them per M. Denis's observation and your own insight.

I believe that the raw data from the key board for both the player position and the direction that the player is facing is received by the OS and stored as (x,y) coordinates which are then converted to a "rotation angle" upon demand. The only hope for further optimization of your code, as I see it, would be getting that raw data and calculating the Sin and Cos values yourself rather then the two step process of the OS calculating the rotation angle and then essentially reversing that calculation to find the Sin and Cos values.

The raw data may may not be available, I don't know, or it might be available through low level functions. Short of securing that data I think you are at your best optimization.

What you might do and perhaps have done, is to go to coding sites and ask what others have done. Good luck.
 
The raw data may may not be available, I don't know, or it might be available through low level functions. Short of securing that data I think you are at your best optimization.

Thanks anyway :). Also about that raw data thing, I know for sure that any (x,y) coordinates aren't provided in way(from user input). As the players position is increased not by coordinates, but by speed, time and rotation angle of the character.
Example for position increment in x-axis: player.position.X=player.position.X+timeElapsed*speedOfCharacter*Cos(Player.RotationAngle)
So no coordinates are given by input. And as there's a few other quite complicated things I simplified things a bit, as rotating a sprite messes everything up in an interesting way :)

Also I know it is hard to understand how other people think, but hey at least you tried :)
 
OK

Thanks anyway :). Also about that raw data thing, I know for sure that any (x,y) coordinates aren't provided in way(from user input). As the players position is increased not by coordinates, but by speed, time and rotation angle of the character.
Example for position increment in x-axis: player.position.X=player.position.X+timeElapsed*speedOfCharacter*Cos(Player.RotationAngle)
So no coordinates are given by input. And as there's a few other quite complicated things I simplified things a bit, as rotating a sprite messes everything up in an interesting way :)

Also I know it is hard to understand how other people think, but hey at least you tried :)

OK but speed is a calculated quantity (coordianteA - coordinateB)/time so unless you have an analog measuring device speed requires logging coordinates, same with rotation. I expect that the key press is checked at known sample rate and for a set speed a change of position or rotation coordinates are calculated. But this is getting down in the weeds, and yes at least I tried, so good luck. :smile:
 
Top