Original Post
OK, so I have a round UFO which fires off shots from its center towards a point where the mouse is clicked. My problem is that the shots don't get fired off in the right direction. It's in the correct quadrant at least and the deviation is small. Actually it's based on the speed of the shot. The faster the shot goes, the more accurate is its direction. Here are my calculations: I'm not really sure if this is the problem but delta.get_ticks() / 1000.f seem t be around 0.008 and I think it might be too low. SHOT_VEL is around 300. It seems that when I raise the SHOT_VEL value to a higher value like 3000 the shots' directiong et more accurate, not 100% though. Also, when the ship shoots att exactly 45 degree angle the shots move in the correct direction. I hate these kind of problems and I'm sure it's just some small thing I've overlooked. Anyone got any ideas? Thanx a lot
int mx = event.button.x; //get x-coordiante of mouse
int my = event.button.y; //get y-coordinate of mouse
//this line calculates the vector length from the center of the ship
//to the point where the mouse is clicked
float vec = sqrt(pow(mx-(offset.x+32), 2) + pow(my-(offset.y+32),2));
//normalise the vector components
float cx = (mx-(offset.x+32)) / vec;
float cy = (my-(offset.y+32)) / vec;
//multiply the vector components to a constant shot speed
xVel = cx * SHOT_VEL;
yVel = cx * SHOT_VEL;
//calculate the new positions based on a timer
//to get fps independant movement
x += xVel * (delta.get_ticks() / 1000.f);
y += yVel * (delta.get_ticks() / 1000.f);