I'm developing a 2D game. In the game, all entities can rotate 360 degrees. Each entity holds an angle
value, and every frame it is drawn rotated around it's center by it's current angle value. For example:
An entity can 'shoot' another entity, a projectile. The Projectile
is instantiated with a position and an angle. The position is calculated like so:
radius = entity.width / 2
angle = entity.angle
projectileX = entity.centerX + cos(angle) * radius
projectileY = entity.centerY + sin(angle) * radius
Example positions this calculation gives:
The angle the Projectile
is instantiated with is the angle of the entity creating it. So if the entity's current angle
value was 45
, the angle of the new Projectile
is going to be 45
.
All of this creates a problem. I will demonstrate the steps leading to this problem:
1- An entity decides to shoot a Projectile
. It's position is calculated like shown above.
2- A new Projectile
is added to the game. It is instantiated with the position calculated in the previous step and with the angle
of the entity instantiating it, for this example 45
.
3- Every frame, each entity is drawn rotated around it's center by it's current angle
value.
The angle
of the new Projectile
is 45
, so it is drawn rotated around it's center by 45
degrees.
As you can see: after the rotation, the Projectile
is no longer aligned correctly with the entity that created it. They are both rotated by the same angle, but the Projectile
isn't 'pointing from the center' of the entity. It seems like it 'moved' a little to the side.
To clarify:
My question: how can I make the Projectile
align correctly with the entity that created it? How can I make it still 'point from the middle' of the entity that created it, and not mess this up when rotating it around it's center?