PDA

View Full Version : Laser Weapon


Noor
01-31-2003, 04:28 PM
I am trying to make a laser weapon for a space game, and I am wondering about the technique used. I would like to use one texture. As you may know, billboardeding will not work. But I want it to look like 3D, any ideas?

davepermen
02-02-2003, 05:33 AM
hm.. one method is to do it 3d (an extruded triangle, a prisma.. something like that)

the other idea could be some sort of billboard around an axis, or so.. i've once seen a page explaining it.. but i don't remember the link, sorry..

Yau
02-03-2003, 09:42 PM
Just wondering.. why won't billboarding work?? I think lots of games use billboarding to do their lasers.

Dia Kharrat
02-03-2003, 09:46 PM
spherical billboarding won't work. However, you can use cylindrical billboarding.

Yau
02-04-2003, 03:04 AM
Well if a laser is a true cylinder then if u were to only see a portion of the cyclinder flashing past ur eyes all u would see is a rectangle?? So u can use billboards to represent a laser but u don't rotate ur billboard so it always faces the viewers camera.

So if u consider a laser with 2 points, point A where is originates from (probably a gun), point B it's destination or current position. Then u draw a rectangle with a height equal to the diameter of the laser with one end at point A and the other end at point B. U can rotate the rectangle by the vector AB so that the viewer can see the maximum surface area of the rectangle.

Should the laser fire directly at the viewer then draw a circle using a billboard to create the effect.

If u use billboard u can create the 3D effects if u have really cool textures.


opps.... I said the hieght of rectangle should be the radius of the laser beam, in fact it should be the diameter.

donBerto
02-04-2003, 06:00 AM
agreed :yes:

if you want, you can use 2 [or more, maybe just 2] rectangles, have 2 different textures and have some kind of 'animation' going on. this will do nicely with yau's implementation. of course, there are other ways but i would do what yau said.

...just a thought.

CyraX
02-05-2003, 07:55 AM
What about using views based systems?
__________________________________________________
|_________________________________________________ _| <- Side B and Side A

From the side a laser is JUST a rectangle. Using clever texture mapping, create a texture that is pretty bright @ the center and pitch dark @ the edges.
Now when you have the Side A and B, these should be modelled in "circular" forms. Thus create a texture (or resuse the textre) and using alpha blending, just chop off the corners.
I guess this is te best way of doing this.
Any better way I would love to know.

davepermen
02-05-2003, 08:49 AM
and you have to orient the flat rectangle around the laserbeam axis facing the camera.. haven't i mentoyed this above yet? a quad rotating around the laserbeam direction, billboarding you? billboards don't mean always be facing the cam in all directions. it can mean as well that its a board that rotates around some axis to always try to face the camera. and thats what i would use for laserbeams.. or prismas.. dunno..

donBerto
02-05-2003, 08:49 AM
going off of Cyrax's "clever texture mapping"

let's use a vector for the path of the laser. starting point is at the barrel of the weapon and the terminal point is the opponent ship. [i just want to cover all grounds, bare with me]. using the magnitude/length of the vector as the 'length' of the laser's path. make a rectangle based on the length and then texture. theoretically, no matter what viewpoint you're at, you'll see a relative laser shot. well, maybe all points of view except directly where it will be just a point.

any more ideas?

baldurk
02-05-2003, 12:21 PM
I'd draw a triangular prism (a triangle at each end and a quad for each side) and then do some fancy texture mapping and particle effects to make it look better. With the right texturing and lighting you could make it look pretty smooth.

Yau
02-05-2003, 06:03 PM
The only problem with a triangular prism is that u have ta render 6 times more triangles than just using a rectangle oriengated nicely to face the camera.

With just a rectangle u could create a texture where in the middle of the beam is bright and towards the edge of the beem the color becomes slightly dull and transparent. This is very difficult to accomplish with a prism.

donBerto
02-05-2003, 06:03 PM
with baldurk's implementation, I'd make the 'prism' spin/rotate.

i like his idea but in my personal opinion, unless you make it dynamic [make it move or add some really cool things like baldurk said, lighting in itself may work against it, making it look kind of ... different :huh:

what i'm getting at is, it's like a big 'triangular piece of "metal" ' will go from the weapon to the target. cool funny effect, maybe not so 'realistic'. open to interpretation. :lol:

Noor
02-05-2003, 06:50 PM
Well if a laser is a true cylinder then if u were to only see a portion of the cyclinder flashing past ur eyes all u would see is a rectangle?? So u can use billboards to represent a laser but u don't rotate ur billboard so it always faces the viewers camera.

So if u consider a laser with 2 points, point A where is originates from (probably a gun), point B it's destination or current position. Then u draw a rectangle with a height equal to the diameter of the laser with one end at point A and the other end at point B. U can rotate the rectangle by the vector AB so that the viewer can see the maximum surface area of the rectangle.

Should the laser fire directly at the viewer then draw a circle using a billboard to create the effect.

If u use billboard u can create the 3D effects if u have really cool textures.


opps.... I said the hieght of rectangle should be the radius of the laser beam, in fact it should be the diameter.
Yau, could you please post some details of how to accomplish your idea of having one rectangle (which is exactly what i want)? Maybe some code will help. I think the most important thing here is the math behind it which is what i'm mosly looking for.

Or if you have a website, that would also help.

Thanks

Yau
02-05-2003, 11:52 PM
OKay here u go... but this code is kinda untested.


void drawLaser(float origin[3],   // position where the laser originates
                        float dest[3],     // position where the laser ends
                        float camera_pos[3], // position of the camera
                        float radius // radius of laser
                        )
{
  float vector1[3];
  float vector2[3];

  // vector1 is the vector from origin to the camera
  vector1[0] = camera_pos[0] - origin[0];
  vector1[1] = camera_pos[1] - origin[1];
  vector1[2] = camera_pos[2] - origin[2];

  // vector2 is the vector from the origin to the destination
  vector2[0] = dest[0] - origin[0];
  vector2[1] = dest[1] - origin[1];
  vector2[2] = dest[2] - origin[2];

  float normal[3];
 
  // get the normal or cross product between vector1 and vector 2
  // we will use this vector to create a rectangle which runs along vector 2
  // and as facing a direction that give maximum surface area to the camera.

  normal[0] = vector1[1] * vector2[2] - vector1[2] * vector2[1];
  normal[1] = vector1[2] * vector2[0] - vector1[0] * vector2[2];
  normal[2] = vector1[0] * vector2[1] - vector1[1] * vector2[0];

  float magnitude;

  magnitude = sqrt(SQR(normal[0]) + SQR(normal[1]) + SQR(normal[2]));

  // normalize the normal, so the magnitude of normal is 1
  normal[0] = normal[0]/magnitude;
  normal[1] = normal[1]/magnitude;
  normal[2] = normal[2]/magnitude;

  // now create the corners for the rectangle
  float corners[4][3];

  // first corner
  corners[0][0] = origin[0] - (radius * normal[0]);
  corners[0][1] = origin[1] - (radius * normal[1]);
  corners[0][2] = origin[2] - (radius * normal[2]);

  // second corner
  corners[1][0] = origin[0] + (radius * normal[0]);
  corners[1][1] = origin[1] + (radius * normal[1]);
  corners[1][2] = origin[2] + (radius * normal[2]);

  // third corner
  corners[2][0] = dest[0] + (radius * normal[0]);
  corners[2][1] = dest[1] + (radius * normal[1]);
  corners[2][2] = dest[2] + (radius * normal[2]);

  // forth corner
  corners[3][0] = dest[0] - (radius * normal[0]);
  corners[3][1] = dest[1] - (radius * normal[1]);
  corners[3][2] = dest[2] - (radius * normal[2]);

  // now draw the rectangle

  // you can add material, colours or texture commands here to make ur laser look more funky
  glBegin(GL_QUADS);
     // remember add a texture coordinate infront of each glVertex3fv for funkier looking lasers

     glVertex3fv(corners[0]);
     glVertex3fv(corners[1]);
     glVertex3fv(corners[2]);
     glVertex3fv(corners[3]);
  glEnd();

}


And finished.

B4 i mentioned that we should rotated the rectangle to allow maximum surface area to face the camera. You may notice that there r no OpenGL rotate commands here, but by using the normal between the vector from the start of the laser to the camera and the vector from the start of the laser to the end to generate the rectangle we esentially created a rectangle rotated to face the camera nicely.

I hope this code works because I kinda fudged it on the spot.

baldurk
02-06-2003, 11:29 AM
aah. So you resize the rectangle dynamically so that it's the right size? that's really good, I didn't think of that. Nice one!

banandgia
02-24-2003, 11:42 PM
see "Game Programming Gems", i can't remember exactly it's vol 1 or 2, but there must be a topic talking exactly on this approah, and it discusses texture mapping of this technique as well

Yau
02-25-2003, 03:14 AM
Well having a quick glance at vol 2, I didn't find anything on lasers so it must be on vol. 1.

banandgia
02-25-2003, 03:58 AM
or let me reborrow it from the library and post the ideas here