Question

I am using OpenGL to create the 3D space. I have a spaceship which can fire lasers.

Up until now I have had it so that the lasers will simply to deeper into the Z-axis once fired. But I am attempting to make a proper aiming system with crosshairs so that you can aim and shoot in any direction, but I have not been successfull in trying to update the laser's path.

I have a directional vector based off the lasers end tip and start tip, which is gotten from the aiming.

How should I update the laser's X,Y,Z values (or vectors) properly so that it looks natural?

Was it helpful?

Solution

I think I see.

Let's say you start with the aiming direction as a 3D vector, call it "aimDir". Then in your update loop add all 3 (x, y and z) to the projectile "position". (OK, at the speed of light you wouldn't actually see any movement, but I think I see what you're going for here).

void OnUpdate( float deltaT )
{
    // "move" the laser in the aiming direction, scaled by the amount of time elapsed
    // since our last update (you probably want another scale factor here to control
    // how "fast" the laser appears to move)

    Vector3 deltaLaser = deltaT * aimDir;  // calc 3d offset for this frame
    laserEndpoint += deltaLaser;           // add it to the end of the laser
}

then in the render routine draw the laser from the firing point to the new endpoint:

void OnRender()
{
    glBegin(GL_LINES);
    glVertex3f( gunPos.x, gunPos.Y, gunPos.z );
    glVertex3f( laserEndPoint.x, laserEndPoint.y, laserEndPoint.z );
    glEnd();
}

I'm taking some liberties because I don't know if you're using glut, sdl or what. But I'm sure you have at least an update function and a render function.

Warning, just drawing a line from the gun to the end of the laser might be disappointing visually, but it will be a critical reference for adding better effects (particle systems, bloom filter, etc.). A quick improvement might be to make the front of the laser (line) a bright color and the back black. And/or make multiple lines like a machine gun. Feel free to experiment ;-)

Also, if the source of the laser is directly in front of the viewer you will just see a dot! So you may want to cheat a bit and fire from just below or to the right of the viewer and then have in fire slightly up or in. Especially if you have one one each side (wing?) that appear to converge as in conventional machine guns.

Hope that's helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top