This problem has been nagging me for quite a while. I use TGF which makes the whole thing all the more tricky, though I have a basic understanding of some of trigonometry.
I'd like to create a player character who moves based on input from the arrow keys, or WASD. However, I'd like him to always aim at the mouse cursor. This is easy to do if you don't mind the problem that it is terribly inaccurate when using the built in method.
Trigonometry is obviously the answer, but I don't know how to use it the acheive my goal, besides that I at one time could move an object based on a 360 degree angle using Sine and Cosine. (Sin and Cos, Advanced Math Object)
Assume that "player" is the player and "aimer" is an active object constantly set at the mouse cursor.
I hope you have already made moving character. Of course get Advanced Math Object.
1. Start of level
-> AdvMath -> Set Decimal Offset on 3 (10^3)
-> AdvMath -> Set Fixed Object (active object following mouse - called "Aim")
2. When you shoot some bullets, use CREATE object. When shooting give every new bullet make its A value equal:
FixedAngle( "Advanced Math object", ( X( "Aim" ) ), ( Y( "Aim" ) ) ) / 1000
Value A remembers shooting angle which is neccesary to determine, which way bullet has to take.
3. [Kernel of movement]: Always
-> Bullet.Add: Cosine( "Advanced Math object", ( A( "Bullet" ) ) ) to value B
-> Bullet.Add: - Sine( "Advanced Math object", ( A( "Bullet" ) ) ) to value C
-> Bullet.Set Position X to: X("Bullet") + B("Bullet") / 100
-> Bullet.Set Position Y to: Y("Bullet") + C("Bullet") / 100
-> Bullet.Set Value B to: B("Bullet") mod 100
-> Bullet.Set Value C to: C("Bullet") mod 100
How does these look in practice? Notice:
#1 - maximal value of cosine/sine is 1000, minimal is -1000. So... when bullet has angle 0deg (it goes on the right), then its speed is equal 10px/TGFloop. (add 1000 to B, and every 100 of this value moves bullet right).
Many thanks. The method you told me worked beautifully, though it required a bit of gentle tweaking. I was not expecting such a prompt or useful response, or at least not both in one. Once again, many thanks.
Edit:
I have two more questions I can easily work without answered, but would make everything better and easier.
First of all, what does what I put in do? I know Sine and Cosine, but what is mod?
The second relates to the first, how can I increase or decrease the speed? (Pixels traveled in one loop)