Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm drawing my ship by the following code and wanted to draw a missile at the tip of the ship, so that when I click spacebar it fires that missile. I don't know if what I'm doing is correct or not. Is that the "correct" way to do the job of drawing a missile then firing it by a space bar? or there is a "better" way? Is the transformation correct here, I mean writing hard coded values and just stick the missile at the tip of the ship by trial and error?

void Ship::Draw( )
{

    gl::pushMatrices();
    gl::translate(Vec2f(m_Pos.x,m_Pos.y));
    gl::rotate(Vec3f(0,0,m_Angle));

    gl::color(ci::Color(1,0,0));

    gl::drawLine(Vec2f(-43,-52),Vec2f(59,1));
    m_Bullet.Draw(59,-8);
    gl::drawLine(Vec2f(-14,-34),Vec2f(-14,38));
    gl::drawLine(Vec2f(59,1),Vec2f(-43,54));
    gl::popMatrices();

}
void Bullet::Draw(int x, int y)
{
    gl::pushMatrices();
    gl::translate(x,y);
    gl::drawSolidRect(Rectf(0,0,16,16));
    gl::popMatrices();
}
share|improve this question
    
Can you edit your question to include a question? Thanks. –  Byte56 Jul 30 '12 at 21:07
add comment

1 Answer 1

up vote 0 down vote accepted

I am not certain if I exactly understand this question, and I do not know OpenGL. That being said, my interpretetion here is that you are trying to draw the missile dependant on the ship's position, similarely to determining where to create a bullet object from a gun, when it is not directly in its center, and is offset either a direct amount from the center on one axis, or on both. (further changing this when set object rotates).

First, I am personally a fan using variables over hard-coded (magic, perhaps) numbers. First, allows easier modification if you change it, and, well, I think might be easier to work with, but your choice. For exampple, let's say each logic frame, you incriment the ship's position. If you suddenly decide to change the speed from, say, 3 to 4, using hard-coded numbers you need to change all of them. Or, you could have them all use speed, and just change the value of speed. This might be useful if said variables change during runtime, something which hard coded numbers cannot, but perhaps a runtime transformation works just as well.

As per positioning it, I know you can find the translation on each axis given a speed and direction, where the speed is length of the hypothesis and direction is an angle, where you can then use trigenometry to determine the actual incriments. Using this, you could, say, find a point relative from the ship where you want the missile, and position it accordingly.

Is this what you were looking for?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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