[Solved] How to align bullet to gun?

       So I got my bullets spawning in the center of my character according to where it pointing right? Now I cant figure out how the diddly to align it not in the center. I want the bullets to spawn in front of my gun that is offset a bit. Changing anything in this formula shifts the location of the radius as a whole.
@Override
	public void fireWeapon() {
		float radians =  (float) Math.toRadians(playerMaster.getDirection());
		float gunPosX =  (float) (32 * Math.cos(radians) + weaponPosition.x + 32);
		float gunPosY =  (float) (32 * Math.sin(radians) + weaponPosition.y + 32);

		bulletList.add(new Bullet(gunPosX, gunPosY, playerMaster.getDirection(), playerMaster));
	}

If you could please explain the math that would be awesome, thank you.

Can you do

bulletList.add(new Bullet(gunPosX + offset, gunPosY + offset, playerMaster.getDirection(), playerMaster));

where offset is the amount you need to offset it from where it is now? You might need to do a check to determine which direction the weapon is facing, and offset accordingly.

I can do that, but that shifts the whole radius aswell. Maybe I have to do that but also do more Cosine and Sine math?

Can you just do a check for the change in radius and then change the gunposx, y accordingly? sorry I dont know much about sine and cosine.

The problem is I don’t know much about trigonometry either. Lmao.


	Vector bullet_spawn_location = new Vector(10, 0, 0);
	bullet_spawn_location = orientation_quaternion.asMatrix().multiply(bullet_spawn_location);

	Bullet bullet = new Bullet(bullet_spawn_location.x +player_location_x, bullet_spawn_location.y+ player_location_y, bullet_spawn_location.z + player_location_z, orientation_quaternion);

This is in 3D but im sure you get the gist.

Currently, you are always rotating the point (1, 0) around a circle with radius 32.
Look at this “how to rotate a vector in 2D using an angle” formula (the two equations under “So the coordinates (x’,y’) of the point (x,y) after rotation are”). You want to do exactly this computation.

I’ve tried all night to figure out what the diddly that formula means and I’m trying to implement it, but I still keep on just resizing the radius of the circular. With this code it makes a sort of asteroid orbit shape.

@Override
	public void fireWeapon() {
		float radians =  (float) Math.toRadians(playerMaster.getDirection());
		float gunPosX =  weaponPosition.x;
		float gunPosY =  weaponPosition.y;
		gunPosX += 62 * Math.cos(radians) - 20 * Math.sin(radians);
		gunPosY += 62 * Math.sin(radians) + 20 * Math.cos(radians);
				

		bulletList.add(new Bullet(gunPosX, gunPosY, playerMaster.getDirection(), playerMaster));
	}

That formula gives you the new coordinates (x’, y’) of a 2D point (x, y) when you rotate that point around the origin (0, 0) through the given angle.
Think about what you want to rotate. It should be (x, y) = (weaponPosition.x, weaponPosition.y), when the following assumptions hold:

  • the origin (0, 0) of your model coordinates is the center of your player/model
  • (weaponPosition.x, weaponPosition.y) is expressed in model coordinates relative to that origin
  • when you rotate the player/model, then it also rotates about its own origin (0, 0)

Probably upload a drawing of your problem to an image hosting platform and reference it here, so that we can give you the exact formula you need.

---------------------> the origin (0, 0) of your model coordinates is the center of your player/model <--------------------------

This is what’s done goofed me up. I appreciate your help a helluva lot, thank you. New Code that werks (weaponPosition.x + 32 is my center).

@Override
	public void fireWeapon() {
		float radians =  (float) Math.toRadians(playerMaster.getDirection());
		float gunPosX =  (float) ((float) weaponPosition.x + 32 +(30 * Math.cos(radians) - (-12) * Math.sin(radians)));
		float gunPosY =  (float) ((float) weaponPosition.y + 32 +(30 * Math.sin(radians) + (-12) * Math.cos(radians)));
				

		bulletList.add(new Bullet(gunPosX, gunPosY, playerMaster.getDirection(), playerMaster));
	}