Use drawLine to render a bullet(Slick2D).

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.

Well??
What do you want us to do about it?

We can’t do anything about it unless you tell us what you want and give us the neccessary information.

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.

I cant figure out how to get a correctly rotated line.

You’re probably going to have to use the Math functions to accomplish this. But, you still didn’t give enough information on what you are looking for to help further.


	float nx = (float) ((x * Math.cos(Math.toRadians(rotation))) - (y * Math.sin(Math.toRadians(rotation))));
	float ny = (float) ((x * Math.sin(Math.toRadians(rotation))) + (y * Math.cos(Math.toRadians(rotation))));

Just translate before rotation and translate back after.

If you don’t know how to translate the coordinates, google it.

I know how to use translate. May I ask how I calculate the rotation variable?

Here

Basically, I’m here to sum it up:

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


Vec2 playerPos;
Vec2 targetPos;

[icode]Vec2[/icode]'s simply store [icode]public float x, y;[/icode]s.

To get the degree angle between those two vectors:


// atan2 is a magical function. It was invented by god... or probably a mathematician, but I'm not sure 'bout that...
float degrees = 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:


// assume this:
Vec2 bulletPos;
// Then we have this update code:
float deltaX = Math.cos(Math.toRadians(degrees)) * speed; 
float deltaY = 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.

Finally we can optimize it by removing the [icode]Math.toDegrees[/icode] at the atan2 and the [icode]Math.toRandians[/icode] from the sine and cosine.
Now instead of converting from
delta vector -> direction (radians) -> direction (degrees) -> direction (radians) -> delta*, we now do
delta vector -> direction (radians) -> delta*.

Marvel at the mighty code:


// Get direction between two positions:
public float getDirectionRad(Vec2 v0, Vec2 v1) {
	return Math.atan2(v1.x - v0.x, v1.y - v0.y);
}
// How to use:
float directionInRadians = getDirectionRad(playerPos, targetPos);
// Get the deltaX and deltaY translation for a given direction:
float deltaX = Math.cos(directionInRadians);
float deltaY = Math.sin(directionInRadians);
// Translating the bullet's position:
bulletPos.x += deltaX;
bulletPos.y += deltaY;

I hope I summed it up so people understand it :slight_smile:

Doesn’t [icode]atan2()[/icode] only return values that (when converted to degrees) are between 0.0000000000000…001 & 89.99999… ?

?
I use it in WorldOfCube (see signature) and it works perfectly, as you can probably see there. (Arm rotation)

Must be my bad usage.

Never mind, I was trying to calculate the angle from one vector (a velocity vector).
Maybe if I tried again with the proper math…

(Off topic: Notice how I changed my avatar to reflect my temporary ranking ;))

You guys are overcomplicating this. No need for angles here, you have a velocity vector! Just draw a line between (x, y) and (x - velocityX, y - velocityY)!

But but the angles, we need to use the radians stat or the cosines will get lonely!

Oh. I thought he’d have a sprite :X

Ifhe has a bullet, he knows where it’s going.