Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (739) Games in Android Showcase (224) games submitted by our members Games in WIP (820) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 how do I change the rotation of a vector?  (Read 1170 times) 0 Members and 1 Guest are viewing this topic.
kingAree

Senior Newbie

Exp: 4-6 months

 « Posted 2017-03-18 11:57:51 »

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
KaiHH

JGO Kernel

Medals: 445

 « Reply #1 - Posted 2017-03-18 12:11:11 »

No need to compute an angle with atan. Just use simple 2D rotation matrix to rotate the direction vector:
 1  2  3  4  5  6  7  8  9 `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
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #2 - Posted 2017-03-18 12:39:21 »

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
 Games published by our own members! Check 'em out!
KaiHH

JGO Kernel

Medals: 445

 « Reply #3 - Posted 2017-03-18 12:45:46 »

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.
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #4 - Posted 2017-03-18 12:52:18 »

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;

}
KaiHH

JGO Kernel

Medals: 445

 « Reply #5 - Posted 2017-03-18 12:54:45 »

here's my constructor:
...uninteresting SplitBullet constructor....
I was trying to point to your first code example with:
 1 `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.
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #6 - Posted 2017-03-18 13:02:12 »

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

http://imgur.com/kj7u4fh
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #7 - Posted 2017-03-18 16:47:50 »

bump, anyone else have an idea?
SteveSmith
 « Reply #8 - Posted 2017-03-18 18:07:01 »

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

Archive
 « Reply #9 - Posted 2017-03-18 18:45:28 »

What if you made the second projectile have a lower velocity

 Games published by our own members! Check 'em out!
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #10 - Posted 2017-03-18 19:07:15 »

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

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `   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));   }`
kingAree

Senior Newbie

Exp: 4-6 months

 « Reply #11 - Posted 2017-03-20 20:10:49 »

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));
}
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Ecumene (50 views) 2017-09-30 02:57:34 theagentd (75 views) 2017-09-26 18:23:31 cybrmynd (183 views) 2017-08-02 12:28:51 cybrmynd (181 views) 2017-08-02 12:19:43 cybrmynd (188 views) 2017-08-02 12:18:09 Sralse (195 views) 2017-07-25 17:13:48 Archive (745 views) 2017-04-27 17:45:51 buddyBro (879 views) 2017-04-05 03:38:00 CopyableCougar4 (1428 views) 2017-03-24 15:39:42 theagentd (1318 views) 2017-03-24 15:32:08
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org