0
votes

I have a big problem when I try to rotate my projectiles along with the ship's rotation.

I want my spaceship shoot two shots at once. So I used my first array of projectiles and placed them at the right side of the ship (one shot on the left and one on the right). So far so good.

But when I shoot with my spaceship and I turn around the rotations of the projectiles look very strange. It's hard to describe so here's an image that shows the error in action.

http://imageshack.us/photo/my-images/841/projectilebug.jpg

I have huge problems rotating the projectiles ALONG with the spaceship so that it looks really good. Currently there is one single shot only. My shooting gets screwed up when I rotate and fire at once. The goal is to create a dual firing cannon like in the picture shown.

Here is some code that places the projectile at the left of the ship (that doesn't really work):

var projectileRadians:Number = (player.frame / 180) * 3.14159;

tempProjectile.x = (player.point.x + 6) + Math.cos(projectileRadians);
tempProjectile.y = (player.point.y + 3) + Math.sin(projectileRadians);

The velocity code:

tempProjectile.nextX = tempProjectile.x;
tempProjectile.nextY = tempProjectile.y;
tempProjectile.dx = rotationVectorList[player.frame].x;
tempProjectile.dy = rotationVectorList[player.frame].y;

This updates the position:

nextX += (dx * (speed + Math.abs(xAdjust))) * step;
nextY += (dy * (speed + Math.abs(yAdjust))) * step;
1
The location where the projectile starts is only half the story. You also need to look at the velocity vector of the projectile. Want to post that code, too?Tom Zych
tempProjectile.nextX = tempProjectile.x; tempProjectile.nextY = tempProjectile.y; tempProjectile.dx = rotationVectorList[player.frame].x; tempProjectile.dy = rotationVectorList[player.frame].y; This updates the position: nextX += (dx * (speed + Math.abs(xAdjust))) * step; nextY += (dy * (speed + Math.abs(yAdjust))) * step;drpelz
ok. I'm so sorry for my late reply but in the meantime I had to do some other things. I figured out the problem!:) It was my fault. My rotation-point had incorrect values so it didn't rotate correctly. I modified and adjusted my rotation-point and now it's working. Thanks for your help anyway!:)drpelz

1 Answers

1
votes

The velocity code looks ok. You don't seem to be calculating the gun position correctly.

Edit: we're not using the standard trig frame of reference; instead, we're using one where angle=0 means up and angles increase as you rotate clockwise. So we take the standard usage and swap sin and cos.

Here, gx and gy mean the coordinates of a gun relative to the coordinates around which the ship moves. We need to rotate the vector <gx, gy> by the angle before adding it to the ship coords:

cs = Math.cos(projectileRadians);
sn = Math.sin(projectileRadians);
tempProjectile.x = player.point.x + gx * sn - gy * cs;
tempProjectile.y = player.point.y + gx * cs + gy * sn;

It would be a good optimization to restrict the ship's angle to certain values (perhaps every 5 degrees), and put all the trig functions into a table ahead of time. Trig functions are expensive to compute.

Note that this also assumes that positive Y is "up". Computer graphics often start with (0, 0) in the upper left corner and increase Y as you move down, so you may have to switch the sign of Y.