|
Trig question |
ngiacomelli
Member #5,114
October 2004
|
I have two sets of locations: x, y and t_x, t_y. When something is fired it is spawned at x, y and travels toward t_x, t_y. How do I increment the change to x,y so that eventually the object will reach t_x and t_y?
|
Richard Phipps
Member #1,632
November 2001
|
1. add dx and dy variables. |
ngiacomelli
Member #5,114
October 2004
|
I take it I would then add dx and dy to x and y?
|
Richard Phipps
Member #1,632
November 2001
|
Yes, for each frame of game logic. |
Arthur Kalliokoski
Second in Command
February 2005
|
I believe line 4 "dx - (t_y - y) / dist" should be "dx = (t_y - y) / dist" (typo) And if the object doesn't move one unit per logic cycle you'd have to multiply it by the units moved per cycle. They all watch too much MSNBC... they get ideas. |
Richard Phipps
Member #1,632
November 2001
|
Yep sorry. There was a typo there. |
ngiacomelli
Member #5,114
October 2004
|
I may be missing something here. Is dx and dy gradually increased over a period of time? Because at the moment the values for dx and dy seem to be coming up with 0, 0. EDIT: sqrt deals in double's. Will I have to cast back for blitting, etc?
|
Richard Phipps
Member #1,632
November 2001
|
You have dist as an int when it should really be a float. You'll need to use floats for the x and y values of your sprites as well. |
ngiacomelli
Member #5,114
October 2004
|
Quote: You have dist as an int when it should really be a float. Should x, y and t_x, t_y become floats also?
|
Richard Phipps
Member #1,632
November 2001
|
ngiacomelli
Member #5,114
October 2004
|
With float values the code does nothing! dist also seems to be giving out junk values.
|
Richard Phipps
Member #1,632
November 2001
|
Not sure why it's not working. Blah, just use some sin/cos/tan:
|
Frank Drebin
Member #2,987
December 2002
|
|
gnolam
Member #2,030
March 2002
|
1) Quote: gAttacks<i>.t_x - gAttacks<i>.x * gAttacks<i>.t_x - gAttacks<i>.x + gAttacks<i>.t_y - gAttacks<i>.y * gAttacks<i>.t_y - gAttacks<i>.y
Should be:(gAttacks<i>.t_x - gAttacks<i>.x)*(gAttacks<i>.t_x - gAttacks<i>.x) + (gAttacks<i>.t_y - gAttacks<i>.y)*(gAttacks<i>.t_y - gAttacks<i>.y)You know, like the Pythagorean theorem. 2) Don't recalculate dx and dy every loop cycle. Compute them once. 3) Make sure you avoid dividing with zero (if (dist > 0) ...). 4) Don't use the bad sin/cos solutions. -- |
NyanKoneko
Member #5,617
March 2005
|
You could compute the X / Y vectors and normalize them. Then you have a ratio for far on the x and y axis your sprite should move each iteration. Think in terms of triangles... (x,y)+ | + c a | + |----------(t_x,t_y) -> x vector b | V Y Vector To normalize the x and y vector, divide each by the hypotenuse of the triangle. Example: (t_y - y) --------------- sqrt(a^2 + b^2) Pretty simple huh? Since it's normalized, just multiply the x and y vector by a constant to make it go that speed. For example, if you want to move the object 5 pixels per frame... object.x = normalized_vector.x * 5; object.y = normalized_vector.y * 5;
----------------- |
Indeterminatus
Member #737
November 2000
|
On another note, I don't see you initialize values anywhere. Of course, if you already do that you can ignore me now saying to initialize, especially the int state. _______________________________ |
Johan Halmén
Member #1,550
September 2001
|
Then there's do_line()but that function returns each int coordinate of the pixels of a line drawn between the start point and end point. Only trouble is that you get different number of pixels on different directions even if the length is the same. It includes a pointer parameter to a call-back function, which you could set to create an array of all the coordinates you travel through. I once made a class that does something like that, without the callback and array stuff. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|