how do I change the rotation of a vector?

Right now I am trying to create a projectile that splits into two additional projectiles, with the two extra projectiles being slightly rotated from the original.

My first projectile is done by creating a vector from my player position to where I clicked my mouse and this works perfectly:

private float dirX = targetX - x;
private float dirY = targetY - y

public void vectorShoot(){
	
	double dl = Math.sqrt(dirX * dirX + dirY * dirY);
	newDirX = (float) (dirX/dl);
	newDirY = (float) (dirY/dl);
	

	x = x + (newDirX * speed);
	y = y + (newDirY * speed);

}

now I try to create two additional vectors and rotate them based off the initial vectors angle:

public void calculateAngles(){
//first I calculate the initial angle from the initial Vector
initialAngle = (Math.atan2(targetY - initialPosY, targetX - initialPosX));
//then I create two new angles and rotate them by 15 degrees counterclockwise & clockwise
CCWAngle = initialAngle - Math.toRadians(15);
CWAngle = initialAngle + Math.toRadians(15);

	//then I create new vectors and change the angle
	tx =  (float) (dirX * Math.cos(CCWAngle) - dirY * Math.sin(CCWAngle));
	ty = (float) (dirX * Math.sin(CCWAngle) - dirY * Math.cos(CCWAngle));
	
	tx2 =  (float) (dirX * Math.cos(CWAngle) - dirY * Math.sin(CWAngle));
	ty2 = (float) (dirX * Math.sin(CWAngle) - dirY * Math.cos(CWAngle));
	
	
	
	split();
}

    //then I just create two new projectiles with the new headings
public void split(){
	handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, initialPosX, initialPosY, tx, ty));
	handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, initialPosX, initialPosY, tx2, ty2));
}

  what am I doing wrong?
   results: one of my new projectiles seems to just head towards 0.53 no matter where I click while another projectile seems to rotate slightly

No need to compute an angle with atan. Just use simple 2D rotation matrix to rotate the direction vector:


float angle = (float) Math.toRadians(15);
float cos = (float) Math.cos(angle);
float sin = (float) Math.sin(angle);
// rotate counter-clockwise:
tx  =  dirX * cos - dirY * sin;
ty  =  dirX * sin + dirY * cos;
// rotate clockwise:
tx2 =  dirX * cos + dirY * sin;
ty2 = -dirX * sin + dirY * cos;

See: https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

Thanks for the insight, I have implemented this and still have an issue where the additional projectiles do not seem to be anywhere near my initial projectile, nor do they maintain a constant distance from eachother

Okay, looking further at your code, I’ve got two questions:

  1. What is initialPosX and initialPosY?
    I assume that this is the (unmodified) position where you start shooting your first bullet from.
    In that case, you obviously cannot just change the angle and fire off your split bullets from that same initial position of the first bullet.
    You of course have to start the splitted bullets from where your first bullet is now.
    So, your simulation must feedback the current position of your first bullet to you so that you can start off your splitted bullets right from that point.

  2. Why do you assume that the bullets splitted at an angle to each other will maintain a constant distance to each other? Or what exactly do you mean by “constant distance from each other”?
    The next thing you should probably do is to sketch/draw/paint how you want the result to look like. Because it becomes harder to understand what you are actually trying to do.

Thank’s Ill get on this and my second point didn’t make much sense, I expect the distance between the projectiles to increase, but at the same rate, currently they seem to just be fairly random, but that’s probably because of my initialPos variables?,

here’s my constructor:

private float initialPosX, initialPosY;
public SplitBullet(Handler handler, float x, float y, float targetX, float targetY) {
	super(handler, x, y, targetX, targetY);
	
	initialPosX = x;
	initialPosY = y;
	
	bulletBounds.x = -6;
	bulletBounds.y = -6;
	bulletBounds.width = 12;
	bulletBounds.height = 12;
	
	
}

I was trying to point to your first code example with:


new SplitBullet(handler, initialPosX, initialPosY, tx, ty)

where within your first bullet you split the two other bullets. This is where initialPosX/initialPosY MUST be changed to the current position of the first bullet.

I was just showing you what the initialPosX and initialPosY were,

I’ve replaced them now anyway with the initial bullets current position, not much has changed in my sim
handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx, ty));
handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx2, ty2));

also here’s an image

Imgur

bump, anyone else have an idea?

Is the solution here?: http://www.java-gaming.org/index.php?topic=22081.0

What if you made the second projectile have a lower velocity

This is pretty much what I’m already doing but my projectiles seem to just go in random directions, not related to the angle of my initial projectile at all

	public void calculateAngles(){
		float angle = (float) Math.toRadians(20);
		float cos = (float) Math.cos(angle);
		float sin = (float) Math.sin(angle);
		
		float dirX2 = targetX - x;
		float dirY2 = targetY - y;
	
		// rotate counter-clockwise:
		tx  =  dirX2 * cos - dirY2 * sin;
		ty  =  dirX2 * sin + dirY2 * cos;
		// rotate clockwise:
		tx2 =  dirX2 * cos + dirY2 * sin;
		ty2 = -dirX2 * sin + dirY2 * cos;
		
		handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx, ty));
		handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx2, ty2));
	}

So I realised I wasn’t adding the rotation centre to my code, I fixed it with:

public void calculateAngles(){
	float angle = (float) Math.toRadians(15);
	float cos = (float) Math.cos(angle);
	float sin = (float) Math.sin(angle);
	
	float dirX2 = (float) (targetX - x);
	float dirY2 = (float) (targetY - y);

	// rotate counter-clockwise:
	tx  =  dirX2 * cos - dirY2 * sin + x;
	ty  =  dirX2 * sin + dirY2 * cos + y;
	// rotate clockwise:
	tx2 =  dirX2 * cos + dirY2 * sin + x;
	ty2 = -dirX2 * sin + dirY2 * cos + y;
	
	handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx, ty));
	handler.getWorld().getBulletManager().addBullet(new SplitBullet(handler, (int) x, (int) y, tx2, ty2));
}