I have the coordinates for the weapon object, which is fire at the target object, which I also have the coordinates to.I am trying to create a bullet effect.I tried some stuff out, but they didnt work as expected ingame.

A gun fire at target. The projectile is traveling towards the target. I want to create a bullet effect, which would start at the projectiles x and y coordinate, and end... exactly where? I cant just use the projectiles x and y + 10(which would be the length of the bullet) because then would the bullet always be a vertical line rather than a rotated line.

Let's assume you have a player position and a mouse - or better - target position:

1 2

Vec2playerPos;Vec2targetPos;

Vec2

's simply store

publicfloatx, y;

s.

To get the degree angle between those two vectors:

1 2

// atan2 is a magical function. It was invented by god... or probably a mathematician, but I'm not sure 'bout that...floatdegrees = Math.toDegrees(Math.atan2(targetPos.x - playerPos.x, targetPos.y - playerPos.y));

You can then use the degrees to translate the x and y position of the bullet:

1 2 3 4 5 6

// assume this:Vec2bulletPos;// Then we have this update code:floatdeltaX = Math.cos(Math.toRadians(degrees)) * speed; floatdeltaY = Math.sin(Math.toRadians(degrees)) * speed;// speed is the speed. If speed is 1 and degrees is 0, then the bullet travels directly to the right. With step size 1 at a time.

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