GLBasic forum

Main forum => GLBasic - en => Topic started by: Bursar on 2010-Aug-28

Title: Bit of maths help please...
Post by: Bursar on 2010-Aug-28
I'm in need of a bit of maths help. I have an vague notion of what's needed, but not how to implement it. I have a player (created using the Vector Editor by MrTaToad). It sits staionary in the middle of the screen, but as I move the mouse around, the player rotates to face the pointer. When I click, the player fires a bullet (also a Vector Editor object) towards the pointer. The bullet is correctly angled, and moves in the right direction.

So the bit I need help with... When the player is facing right, the bullet needs to be offset 11 pixels * scale in order to appear at the end of the player gun (so offset is x=11*scale, y=0*scale). Now I can't just add 11*scale as it all goes wrong as the player rotates. I'm guessing I need to use SIN and COS, but they confuse the heck out of me, so a point in the right direction (no pun intended!) would be appreciated :)

Ofcourse the other option is that I have a 'handle' on the player (that I create with the Vector Editor) at the gun location, and I can retrieve it's actual screen position and place the bullet there.
Title: Re: Bit of maths help please...
Post by: MrTAToad on 2010-Aug-28
Originally, I would have used the special flags for things like that when I was thinking of doing a vector based program.  However, if you dont want to use those, you could use something like :

bx=x+(11.0*sin(angle))
by=y+(11.0*cos(angle))

to position bullet 11 pixels around the object - I may have sin & cos the wrong way around.

The angle is the angle the player is facing.
Title: Re: Bit of maths help please...
Post by: Bursar on 2010-Aug-28
Had to swap SIN and COS around as you suggested, but it works like a champ. Thanks.
Title: Re: Bit of maths help please...
Post by: MrTAToad on 2010-Aug-29
No problem!