1

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Saiaku
  • 57
  • 2
  • 6
  • I'm not sure what you mean. The XYZ value gotten from aiming *is* the laser's path. I mean, there's no drop or anything like with a projectile - it just continues in the aiming direction, effectively forever. – Mark Stevens Nov 22 '12 at 00:16
  • Sorry if I've been unclear, but what I mean is that when you shoot at an angle, I can't simply adjust the Z-value of the laser so that it goes deeper into the space, I also need to do something with the X and Y, and I am not sure what to do with them. Otherwise it will just be an angled laser moving in one direction, while it should move a little bit on X and Y depending on the angle. The angle should be available from calculating with the start vector and end vector of the laser beam, I guess. – Saiaku Nov 22 '12 at 00:21
  • If it starts at position `(x0, y0, z0)` and has a velocity vector `(x, y, z)`, then every time you update you just add `(x, y, z)` to the position. So after the first update you'd be at `(x+x0, y+y0, z+z0)`, etc. Does that make sense? Edit: also, note that the aiming and velocity vectors should be the same. – Xymostech Nov 22 '12 at 00:28
  • Is the velocity vector the difference between the lasers' start and end tips. laser beam: start<-------->end like that? and so I add that difference to the start vector and end vector for each update? – Saiaku Nov 22 '12 at 00:32

1 Answers1

0

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.

Mark Stevens
  • 2,366
  • 14
  • 9
  • Ah, yes I understand now. I made it work and it works as intended. Many thanks for the help! :) – Saiaku Nov 22 '12 at 00:53