I'm trying to make a 2D game where I have a gun that i use to shot things in the direction of the mouse resulting in an angle that I can get. I have some problems with the trajectory of the bullet because I don't know what to put in the Transpose and Rotate function. For example Point 1 has x = 20 and y = 30. I want to shot in the direction of Point 2 at x = 50 and y = 50. To do that i get an angle 't' which I use in the rotate function. But the reality is that I get the animation that I presented in the photo. I increase the x coord of the bullet because I can only shot in right side so only positive x. The bullet is moving on the 0x axis rotated at an angle t but at the same y as the gun.

enter image description here

What I want is something like

enter image description here

I'm sorry if this is a duplicate I couldn't find a solution to my problem. I think the resulting Matrix should have the form : Translate * Rotate * Translate but I can't find the right x and y.

Sorry if this seemed dumb and thank you for your time!

  • \$\begingroup\$ I can't post a full answer now, but if you look online, there are ways to find the angle of a triangle, based on its sides. From your figures, create a Point 3, that is sitting directly below Point 2 on the same y as Point 1. That will create a triangle with a square angle, and should make it easier to calculate the other two angles. \$\endgroup\$ – TomTsagk Nov 16 '20 at 14:48
  • \$\begingroup\$ If your gun is at (x0,y0) and your mouse is at (x1,y1) then you can work out the angle with atan((y1-y0)/(x1-x0)) You'd need to do some very simple Mechanics, taking into account gravity and acceleration to create a parabolic path (rx,ry) which you'd use as the position of your projectile over time t. You'd need this formula: en.wikipedia.org/wiki/… \$\endgroup\$ – Pixel Nov 16 '20 at 16:38

Let us say you are shooting from p1 with coordinates p1.x and p1.y. And you want to shoot in direction to p2 with coordinates p2.x and p2.y.

When the bullet is created, you will place it in p1. That would be something like this:

bullet.position = p1;

We need to figure out how it should move.

Let us call d the vector that goes from p1 to p2:

d = p2 - p1
d = (p2.x - p1.x, p2.y - p1.y)

If you want the angle t, it will be t = atan2(d.y, d.x) or t = atan2(p2.y - p1.y, p2.x - p1.x) if you prefer. You don't need the angle.

Let us say that the bullet should advance speed distance per unit for time. Then your velocity is:

velocity = d * speed/length(d)

That velocity describes the motion of the bullet. I'm assuming no gravity, as it was not mentioned in the question. I'm guessing this is a top down game.

I suggest to store it in the bullet:

bullet.position = p1;

var d = p2 - p1;
bullet.velocity = d * speed/length(d);

Each game cycle you will move the bullet by velocity according to the number of units of times elapsed. That would be something like this:

bullet.position = bullet.position + (bullet.velocity * delta);

where delta is the elapsed time in the same units used for l. For example l is pixels per millisecond and delta is milliseconds. If you are working with different units you need a unit conversion. For example if l is pixels per second, and delta is milliseconds, you need to divide delta by 1000 so it is in seconds.

The vector operations used:

  • Vector addition: v1 + v2 = (v1.x + v2.x, v1.y + v2.y)
  • Vector subtraction: v1 - v2 = (v1.x - v2.x, v1.y - v2.y)
  • Scalar product: v * s = (v.x * s, v.y * s)
  • Length: length(v) = sqrt(v.x * v.x + v.y * v.y)

Alright, let us suppose you are placing a model to represent the bullet, and you want to be able to apply a transformation to the model.

We want to have the orientation stored so we have use it to orient the model.

bullet.position = p1;

var d = p2 - p1;
bullet.direction = d * 1/length(d);
bullet.velocity = bullet.direction * speed;

Let us see how the rotation matrix transformation looks like:

+-                  -+
|  cos(θ)    -sin(θ) |
|                    |
|  sin(θ)    cos(θ)  |
+-                  -+

We already have those values:

+-                                          -+
|  bullet.direction.x    -bullet.direction.y |
|                                            |
|  bullet.direction.y    bullet.direction.x  |
+-                                          -+

And, of course, you would make your translation matrix from bullet.position.

There is no need to call any trigonometric function. No need to waste CPU cycles on them or introduce floating point errors due their computation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.