Jump to content
  • Advertisement
Sign in to follow this  
Unleeb

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!