Custom trig functions needed.

Started by
13 comments, last by Grimfate126 17 years, 5 months ago
Quote:Original post by Bob Janova
You don't need trig to do that:
scalefactor = speed / sqrt(sqr(cursor_x - player_x) + sqr(cursor_y - player_y));traX = (cursor_x - player_x) * scalefactor;traX = (cursor_y - player_y) * scalefactor;


Of course, which is faster depends on whether cos and sin beat sqrt and sqr. But the chances are they don't.


ok, i tried that, and it work fine, BUT, sometimes the bullets are far away from the center of my cursor. sometimes they are perfectly in the middle, sometimes they are off by 30 pixels. my code:

pistol[c].x = player->x;
pistol[c].y = player->y - 3.7;
pistol[c].angle = 7 / sqrt( ( (cursor->x - player->x) * (cursor->x - player->x) )+ ( (cursor->y - player->y) * (cursor->y - player->y) ));
pistol[c].trajectoryX = (cursor->x - player->x) * pistol[c].angle;
pistol[c].trajectoryY = (cursor->y - player->y) * pistol[c].angle;


what wrong??
Advertisement
That isn't linear interpolation. It's actually equivalent to using the sine and cosine functions and can be derived using trigonometric identities.

One problem is that you're placing the bullet with

pistol[c].y = player->y - 3.7

and then calculating the velocity as if the bullet was starting from the same position as the player. This will make the bullet's path off by up to 3.7 units.
Quote:Original post by Vorpy
That isn't linear interpolation. It's actually equivalent to using the sine and cosine functions and can be derived using trigonometric identities.

One problem is that you're placing the bullet with

pistol[c].y = player->y - 3.7

and then calculating the velocity as if the bullet was starting from the same position as the player. This will make the bullet's path off by up to 3.7 units.



ok, i changes it to pistol[c].y = player->y; and it still does the same thing. one thing i noticed though. it gets worse when the cursor is BELOW the player. weird..

any solutions?
va = Cursor - Player //va is a vector
distance = sqrt(va DOT va) //DOT as dot product
framesVisible = distance / speed

ShowIt = va * (frame * speed / distance)

And obviously you precompute speed/distance because division is expensive.

How precise are floating point numbers on your platform.
i figured it out. i was using int's for my X and Y, and that screwed it up. changed them to doubles, and it works beautifully.

ok, one (hopefully) last thing:

everything is working now, except for one small glitch. when i HOLD the R key, my player shoots a bullet whereever the cursor is. if i move him WHILE holding R, the bullets automatically change direction. NOW, if im holding R, and i move my analog, which moves the cursor, my player stops shooting for about a second, and then starts up shooting again. why is this? the most confusing part is why it works when i move my player, but not my cursor. anybody know the problem? if you want code, just ask.

This topic is closed to new replies.

Advertisement