Laser Sight
by Paul Clarke · in Torque Game Engine · 10/04/2009 (10:28 am) · 59 replies
I'm trying to implement a laser sight for the weapons in my game instead of using a crosshair. So far I've been using a volume light and it works quite well, but as you'll see in the picture below it clips through walls, enemies and eveything else.
I guess I'm just wondering if anyone knows how to stop this from happening, or if there's a better way to do laser sight. I've looked at the resources available but they all seem to be for laser projectiles which isn't what I need.
Any help is appreciated.
[IMG]http://img225.imageshack.us/img225/7172/25492337.th.jpg[/IMG]
I guess I'm just wondering if anyone knows how to stop this from happening, or if there's a better way to do laser sight. I've looked at the resources available but they all seem to be for laser projectiles which isn't what I need.
Any help is appreciated.
[IMG]http://img225.imageshack.us/img225/7172/25492337.th.jpg[/IMG]
About the author
#42
10/11/2009 (2:42 am)
Do yourself a favour and run a debug build ;). Trial and error will work eventually, but it's much nicer to be able to see exactly what's causing the problem (or even what file it's in), and it makes it easier for others to help. Even if you don't use the debug features, building and running in debug mode will usually show you the approximate location of a crash, unless it's an infinite loop.
#43
So indeed I will try the Debug build whenever I run into a crash from now on.(for testing though I'll leave Release on for the performance gain)
Thanks for the advice. :)
10/11/2009 (2:57 am)
Well, for now, I've managed to stop all the crashes, however You've raised a good point and I'm pretty sure I can understand the Debugger by now... (hopefully, lol)So indeed I will try the Debug build whenever I run into a crash from now on.(for testing though I'll leave Release on for the performance gain)
Thanks for the advice. :)
#44
10/11/2009 (4:22 am)
Yes, Release builds are faster - what I do is compile in debug first, run it and just test all the stuff I've added to make sure nothing's broken, then once I've fixed any problems, build a release EXE of the flawless code.
#45
10/13/2009 (3:59 am)
Does anyone know how to attach the laser sight to just one player, I'm making a single player game and don't want my bots using the laser. is there an if statement or something I put in Armor::onAdd to check if the player is user controlled? I'm sure this should be simple, I just can't figure it out.
#46
just copy the If function, I modded some of the other values.
10/13/2009 (4:13 am)
function Armor::onAdd(%this, %obj)
{
...
...
if (isObject( %client )) //this makes sure its not an AI
{
// sgLaserObject
%light = new volumeLight() {
dataBlock = "sgLaserLight";
rotation = "-0.357694 0.933839 9.9834e-009 180";
scale = "1 1 1";
//dataBlock = "sgMountLight";
Enable = "1";
IconSize = "1";
ParticleColorAttenuation = "1";
Texture = "common/lighting/red-lightFalloffMono.png";
lpDistance = "38.00";
ShootDistance = "60";
ShootLOS = "true";
Xextent = "0.025";
Yextent = "0.025";
SubdivideU = "4";
SubdivideV = "4";
FootColour = "1.000000 1.000000 1.000000 0.182000";
TailColour = "0.400000 0.400000 0.400000 0.400000";
};
%light.attachtoobject(%obj);
%obj.light = %light;
}
}just copy the If function, I modded some of the other values.
#47
Edit: There are also no errors in the console.
10/13/2009 (4:47 am)
Thanks for the help CSMP, unfortunately it doesn't seem to work, with the if the laser isn't rendered. I must have checked it about 20 times thinking I'd made a mistake, but for some reason it won't work.Edit: There are also no errors in the console.
#48
10/13/2009 (5:17 am)
Try %obj.client instead of %client
#49
10/13/2009 (5:22 am)
Thanks Paul, it works now.
#50
Just realised that I copied that code piece from the player.cs and forgot to change the variable over.
(shoulda tested that first, lol)
10/13/2009 (10:04 am)
oops, sorry about that. :)Just realised that I copied that code piece from the player.cs and forgot to change the variable over.
(shoulda tested that first, lol)
#51
Such a clever resource--if only I could get it to track with the crosshair! It moves in the same general direction, but at different rates, like it's mounted at the player's elbow or something. And there don't seem to be any slot parameters for attachtoObject().
01/12/2010 (5:24 pm)
Is there any particular technique to mounting the light on the player so that it doesn't move around? Such a clever resource--if only I could get it to track with the crosshair! It moves in the same general direction, but at different rates, like it's mounted at the player's elbow or something. And there don't seem to be any slot parameters for attachtoObject().
#52
05/22/2010 (1:19 pm)
so where do I put this....shape->getRenderMuzzlePoint(mountPoint,&startPosition); shape->getRenderMuzzleVector(mountPoint,&endPosition);
#53
05/22/2010 (1:58 pm)
void volumeLight::renderGL(SceneState *state, SceneRenderImage *image)
{
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBindTexture(GL_TEXTURE_2D, mLightHandle.getGLName());
Point3F lightpoint;
// This is where the hypothetical point source of the spot will be
// The volume slices are projected along the lines from
lightpoint.x = lightpoint.y = 0.0f;
lightpoint.z = -mlpDistance;
F32 ax = mXextent / 2;
F32 ay = mYextent / 2;
//Laser
F32 collisionDistance = mShootDistance;
GameBase *obj = getAttachedObject();
ShapeBase *shape = dynamic_cast<ShapeBase*>(obj);
if(shape && mShootLOS)
{
Point3F startPosition;
Point3F endPosition;
RayInfo rInfo;
MatrixF muzzleMat;
/*
shape->getMuzzlePoint(0,&startPosition); //Stock Code
shape->getMuzzleVector(0,&endPosition);
*/
shape->getRenderMuzzlePoint(mountPoint,&startPosition); //Laser
shape->getRenderMuzzleVector(mountPoint,&endPosition); // Use differant mount node for laser (can leave alone)
endPosition.normalize();
endPosition *= mShootDistance;
endPosition += startPosition;
shape->disableCollision();
const U32 ObjectMask = TerrainObjectType |
InteriorObjectType |
StaticObjectType |
PlayerObjectType |
VehicleObjectType |
DamagableItemObjectType;
if(getContainer()->castRay(startPosition, endPosition, ObjectMask, &rInfo) == true)
collisionDistance = (rInfo.point - startPosition).len();
shape->enableCollision();
}
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Draw the bottom foot... this is basically the glowing region.
glBegin(GL_QUADS);
glColor4f(mfootColour.red, mfootColour.green, mfootColour.blue, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-ax, -ay, 0.0f);
glColor4f(mfootColour.red, mfootColour.green, mfootColour.blue, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(ax, -ay, 0.0f);
glColor4f(mfootColour.red, mfootColour.green, mfootColour.blue, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(ax, ay, 0.0f);
glColor4f(mfootColour.red, mfootColour.green, mfootColour.blue, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-ax, ay, 0.0f);
glEnd();
S32 i;
glDepthMask(GL_FALSE);
// Slices in X/U space
for(i = mSubdivideU; i >= 0; i--)
{
F32 k = ((F32)i) / mSubdivideU; // use for the texture coord
F32 bx = i * mXextent / mSubdivideU - ax; // use for point positions
// These are the two endpoints for a slice at the foot
Point3F end1(bx, -ay, 0.0f);
Point3F end2(bx, ay, 0.0f);
//Laser
end1 -= lightpoint; // get a vector from point to lightsource
end1.normalize(); // normalize vector
end1 *= collisionDistance; // multiply it out by shootlength
end1.x += bx; // Add the original point location to the vector
end1.y -= ay;
// Do it again for the other point.
end2 -= lightpoint;
end2.normalize();
end2 *= collisionDistance;
end2.x += bx;
end2.y += ay;
sgRenderY(Point3F(bx, ay, 0.0f), end1, end2, mfootColour, mtailColour, k);
}
// Slices in Y/V space
for(i = 0; i <= mSubdivideV; i++)
{
F32 k = ((F32)i) / mSubdivideV; // use for the texture coord
F32 by = i * mXextent / mSubdivideV - ay; // use for point positions
// These are the two endpoints for a slice at the foot
Point3F end1(-ax, by, 0.0f);
Point3F end2(ax, by, 0.0f);
//Laser
end1 -= lightpoint; // get a vector from point to lightsource
end1.normalize(); // normalize vector
end1 *= collisionDistance; // extend it out by shootlength
end1.x -= ax; // Add the original point location to the vector
end1.y += by;
// Do it again for the other point.
end2 -= lightpoint;
end2.normalize();
end2 *= collisionDistance;
end2.x += ax;
end2.y += by;
sgRenderX(Point3F(ax, by, 0.0f), end1, end2, mfootColour, mtailColour, k);
}
glDepthMask(GL_TRUE);
}
#54
05/22/2010 (2:00 pm)
@Donnie: Above is my entire renderGL function, it has those changes and from the looks it may have all the other needed changes.(don't take my word on that)
#55
05/22/2010 (3:25 pm)
Gotcha...why didnt I see that!! Thanks man.....
#56
05/22/2010 (3:35 pm)
Hey C, is there something I'm not doing to get this to show in game. I did everything. I made laserlight.cs file in common/lighting/lights, then I put the code in my player.cs file in Armor::onAdd. Is there anything else I need to do and thanks for your help.....
#57
Heres an example of my weapon's onMount code with condition checks:
Also try and make sure that your sgLaser is referring to a valid mountpoint or it may just use the right-foot node(in my case).
Edit: if you have used Paul's demo code in post#18 it seems that the bottom of his code(script) has been cut off... try adding the missing section from the above code and see if that helps?
05/22/2010 (4:08 pm)
I'm pretty sure that all of the scripts in the lighting folders are exec'd by default, but that would be worth checking out.Heres an example of my weapon's onMount code with condition checks:
function MagalImage::onMount(%this, %player, %slot)
{
Parent::onMount(%this, %player, %slot);
echo("c4M4A1Image::onMount Called!");
if (%player.client.primary $= "Magal" && %player.client.Laser $= "Basic")
{
// sgLaserObject
%light = new volumeLight()
{
dataBlock = "MagalsgLaser";
rotation = "-0.357694 0.933839 9.9834e-009 180";
scale = "1 1 1";
//dataBlock = "sgMountLight";
Enable = "1";
IconSize = "1";
ParticleColorAttenuation = "1";
Texture = "common/lighting/red-lightFalloffMono.png";
lpDistance = "38.00";
ShootDistance = "60";
ShootLOS = "true";
Xextent = "0.025";
Yextent = "0.025";
SubdivideU = "4";
SubdivideV = "4";
FootColour = "1.000000 1.000000 1.000000 0.182000";
TailColour = "0.400000 0.400000 0.400000 0.400000";
};
%light.attachtoobject(%player);
%player.light = %light;
}
}Also try and make sure that your sgLaser is referring to a valid mountpoint or it may just use the right-foot node(in my case).
Edit: if you have used Paul's demo code in post#18 it seems that the bottom of his code(script) has been cut off... try adding the missing section from the above code and see if that helps?
#58
I have; mountPoint
ejectPoint
muzzlePoint
Any help would be great and thanks C for tyring to help me....
05/23/2010 (8:34 am)
Still a no-go..... Hey C, what should my weapon model contain?? I do have a muzzlepoint, is there some other point I should export with it??I have; mountPoint
ejectPoint
muzzlePoint
Any help would be great and thanks C for tyring to help me....
#59
05/26/2010 (1:45 pm)
The laser should be coming from the muzzlepoint.(with the sgLaser as a mointpoint of 0)
Torque Owner CSMP
MP Studios
Problem with that method is if I remember what I was doing when the crash happened, lol.(because most of the time the "crashes" dont log the error into the console)
The crashes I was talking about were instant "Windows Program Stopped Responding" type errors.