Shoot bullet from rotating canon(s)

Started by
11 comments, last by JohnnyBlomgren 10 years, 6 months ago


Wow, I feel bullied here. You two pounding at me without me even replying haha...

Yeah, sorry about that. We should have used softer gloves.

I second Paradigm Shifter's opinion about drawing diagrams and fixing your understanding when there are unexpected results, instead of just tweaking the code until it seems to work. I feel very strongly about this, because I used to just tweak the code until it seemed to work, and that got me in trouble later on.

Yeah I get that. I really do want to know whats happening but in this particular case I just need this to work, and I'm happy. I'm pretty sure I can learn every row of logic when it's up and working... And I really wanna do this one without Matrices and such...

I have tweaked the code a bit now after reading previous posts but I still get very shady results. The spawn of the bullets seem to follow a much larger radius than the ship.

Code for rotating player towards direction (now with Math.Atan2(Y,X) instead of XY and no alter to the rotation with Math.PI:


				theta = Math.atan2(
						shootingPad.getKnobY() - shootingPad.getHeight() / 2, shootingPad.getKnobX() - shootingPad.getWidth() / 2);

				rotation = (float)Math.toDegrees(theta);

But it seems like it's still here the spawn position gets really screwed (still):


	private void shoot(){
		double angle = rotation;
		float x2 = x;
		float y2 = y;
		gunRightTipX = getCenterX() + 48;
		gunRightTipY = getCenterY() + 36;
		
		float bulletX = x2 + gunRightTipX * (float)Math.cos(Math.toRadians(angle)) - gunRightTipY * (float)Math.sin(Math.toRadians(angle));
		float bulletY = y2 + gunRightTipX * (float)Math.sin(Math.toRadians(angle)) + gunRightTipY * (float)Math.cos(Math.toRadians(angle));
		
		Bullet b = new Bullet(new TextureRegion(Assets.bullet), bulletX, bulletY, (float)angle, 5f);
		bulletList.add(b);
	}

Am I referring to something crazy here?

Advertisement

The short description of what we are proposing (removing all the confusing math as much as I can) is that instead of saving the rotation as `angle', you keep it as `rotationX' and `rotationY'. `rotationX' plays the roll of `(float)Math.cos(Math.toRadians(angle))' in your code, and similarly for `rotationY' and `sin'.


float directionX = shootingPad.getKnobX() - shootingPad.getWidth() / 2;
float directionY = shootingPad.getKnobY() - shootingPad.getHeight() / 2;
float length = Math.sqrt(directionX*directionX + directionY*directionY);
rotationX = directionX / length;
rotationY = directionY / length;
 
// ...
 
float bulletX = towerCenterX + gunRightTipX * rotationX - gunRightTipY * rotationY;
float bulletY = towerCenterY + gunRightTipX * rotationY + gunRightTipY * rotationX;

I am not sure what (x,y) and (getCenterX(),getCenterY()) are, but gunRightTipX should probably be just 48 and gunRightTipY should probably be just 36.

[EDIT: Oh, when you create the bullet, instead of passing an angle and a speed, you should pass it a velocity (that is, velocityX and velocityY, which are simply rotationX*speed and rotationY*speed).]

The short description of what we are proposing (removing all the confusing math as much as I can) is that instead of saving the rotation as `angle', you keep it as `rotationX' and `rotationY'. `rotationX' plays the roll of `(float)Math.cos(Math.toRadians(angle))' in your code, and similarly for `rotationY' and `sin'.


float directionX = shootingPad.getKnobX() - shootingPad.getWidth() / 2;
float directionY = shootingPad.getKnobY() - shootingPad.getHeight() / 2;
float length = Math.sqrt(directionX*directionX + directionY*directionY);
rotationX = directionX / length;
rotationY = directionY / length;
 
// ...
 
float bulletX = towerCenterX + gunRightTipX * rotationX - gunRightTipY * rotationY;
float bulletY = towerCenterY + gunRightTipX * rotationY + gunRightTipY * rotationX;

I am not sure what (x,y) and (getCenterX(),getCenterY()) are, but gunRightTipX should probably be just 48 and gunRightTipY should probably be just 36.

[EDIT: Oh, when you create the bullet, instead of passing an angle and a speed, you should pass it a velocity (that is, velocityX and velocityY, which are simply rotationX*speed and rotationY*speed).]

And as soon as you said it like this, it worked! :D

I sincerely thank you for this!

This topic is closed to new replies.

Advertisement