# OpenGL Mounting weapon on spaceship (2D) [Solved]

This topic is 3790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a 2D spaceship with a position and a rotation. To draw it, I first translate then rotate the modelmatrix (OpenGL). The spaceship is basically just a quad with an applied texture. Now, if I wanted to mount the weapon at (20, 20) on the spaceship. How can I calculate those coordinates after the ship has been transformed? Thank you, unleeb [Edited by - Unleeb on March 2, 2008 2:51:03 PM]

##### Share on other sites
Hi,

You might find it easier to place the weapons first (in object space), then transform both the weapons and the ship by the ship's rotation and position. That should keep the weapons in the right place when the ship is transformed.

##### Share on other sites
Hmm, that sounded a lot easier than it actually was.

The ship is in world space. The weapon is in the ships local space. The projectiles are in world space. All of these three classes are derived from entity. This means, they all got a position vector, a velocity vector, and an angle. (The weapons angle is, for now, just set to the ships angle)

Using your method, I have been able to transform the ship and the weapon correctly :)

Now, the projectiles remain. Since they are in world space contrary to the weapon, I find it hard to calculate the position (and I assume the angle is just as hard). Would it help if I had a matrix class? Is there some good documentation on how to achieve such effects?

Later ships will have several mount points for engines, weapons, etc. and they will also have bounding boxes (again, with a relative position and angle). I feel that there must be a better way to do this, than what I can think of.

EDIT: I think I will write a matrix class anyways. It seems it will help with my problem also. I'm just not not sure how, yet.

Thank you,
unleeb

[Edited by - Unleeb on March 2, 2008 8:38:08 AM]

##### Share on other sites
As I thought, It was rather easy. All you have to do, is calculate where the weapon is located (code below), relative to the ships center, using the specified angle. Then just add the ships position:

float ca = cos(angle*3.1416f/180);float sa = sin(angle*3.1416f/180);float x = ca * weapon.x - sa * weapon.y + ship.x;float y = ca * weapon.y + sa * weapon.x + ship.y;glTranslatef(x, y, 0.0f);glRotatef(angle, 0.0f, 0.0f, 1.0f);

Its always a simple solution when you think its hard :S

unleeb

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631741
• Total Posts
3001977
×