Game Development Community

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]
#21
10/09/2009 (11:13 pm)
Fixed my typo :d
It's easier to just create and delete the volumelight object when you mount and unmount the weapon.

Do you have a link to Daniels resource?
#22
10/09/2009 (11:22 pm)
lol, your completely right, forgot about those methods... in my haste for a fix i was trying onAdd (lmao).

here ya go:(must have!) www.garagegames.com/community/resources/view/14920

Have it working hand and hand with guiObjectView for a Weapon customization screen.

Here ya go, Alpha Projects Latest Pics:
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/CustomWeapon.jpg
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/laser.jpg
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/InventorySetup.jpg

I'll try and write up a blog sometime soon with more info on the latest additions, but atm I've got some features that still need to be worked out.

Btw: Thanks Paul and... Paul(lol) for the demo code!
:)
#23
10/10/2009 (1:45 am)
The images mounted through my resource are just like other images, but placed at a diffeent location. So if your gun is image slot 0, and your laser scope is image slot 1 mounted on image slot 0, then you'd need to mount the light to image slot 1 instead of 0. There's probably other changes you'd need to make, I haven't read the code here in detail.
#24
10/10/2009 (1:54 am)
Well I have 3 seperate mounts: LaserMount, GripMount and ScopeMount on each weapon so if I was too substitute Slot1 with LaserMount would that work?

I'm going to try it now, But I have to admit I could not follow the Resource in my head as much as I would have liked to.(too much mount to mount then mount the mount etc.. etc.., just confused the hell out of me, I'll try examining it some more. lol)

Btw: right now the Laser is being shown from the MuzzlePoint and I'm trying to get the Laser to mount the LaserPoint thats attached too the LaserMount Weapon Attachment, hence the mount to mount etc. etc. thats throwing me off extraordinarily well.
#25
10/10/2009 (6:53 am)
Quote:(too much mount to mount then mount the mount etc.. etc..
Yeah, I realised that when writing, sorry ;P. I just couldn't think of a more appropriate/simple name for what I was doing.

Basically, you have 8 slots for images. These slots don't correspond to anything in the shape; they're not named or anything, they're just slots 0-7. So if you had a gun and a laser sight, you'd have mounted the gun in slot 0, and the sight in slot 1, then done something like mountImageImage(1,0,"LaserMount") to attach the laser to the gun, right?

Usually an image is rendered at the location of a named node in a shape - the node named mountX where X corresponds to the mount point set in the image datablock. What that call to mountImageImage does is tell the engine 'DON'T render the image in slot 1 at the mount node; instead, render it at the same position as image 0, at the offset given by the "LaserSight" node in that image'. So the laser image is still mounted to the Player in slot 1, but it appears to be 'mounted' to the other image in slot 0).

So I *think* what you need to do, from a brief reading of the code here, is change the mountPoint in your light datablock to the slot your laser image is mounted in. (In the case of my example above, that'd be '1' instead of '0'.)


Hey, those screens aren't looking bad! I like the upgrade stuff :D. Something I want to include in my game.
#26
10/10/2009 (6:53 am)
I've been trying to figure out how to create a dot where the laser collides with something, like this, but I haven't had much luck so far, anyone have any ideas?
#27
10/10/2009 (6:55 am)
Paul - a random guess, place a decal at the ray hit location?

Those sgVolumeLight effects are really nice! I never knew that the engine actually had a fairly good way to implement lasers :P.
#28
10/10/2009 (12:06 pm)
Using the decal manager is not of much use. It just creates a new decal each time the light renders.
That being said, it does make an interesting scorch mark.

The best bet is to make a GL circle in world-space. A small GL triangle fan with eight points should do the trick in approximating a circle. Javier seems to like the simple GL rendering methods, so he might be able to whip up some code for you ;)
#29
10/10/2009 (3:00 pm)
The Problem with mounting it to Slot4(which is the LaserMount) is that it mounts the sgLaser to the LaserMount...

However since theres an offset available in the sgLaser datablock and the LaserMount will always be the same position offset from the LaserPoint it'll work. :)

Thanks guys!
#30
10/10/2009 (6:52 pm)
CSMP - does your laser sight image have a 'muzzle' node? The sgLight code tries to place the light as getMuzzleTransform, which will return the image transform if there's no 'muzzle' node.

I really don't understand why there isn't just a generic 'getNodeTransform' that you can pass a node name into. That would make everything so much simpler.
#31
10/10/2009 (6:58 pm)
Actually, Its mounted exactly where I need it at this point(though I'm actually going to try that right now), however I'm getting a crash when I load a map with other Lights.(I should have tested this earlier)

I tried adding "ShootLOS = "false";" into the maps lights and it seems I get a crash as soon as the mission loads into view.(which I do see for a split second, I imagine before the sgLights are loaded)

Which with the MG Starter Kit you spawn as a spectator, So the sgLaser itself isnt even created yet leaving me to believe that somehow the code changes have corrupted the other lights.

Can anyone else verify?

Edit: Testing right now, so far I've removed the "Rename SceneObject to GameBase step" and it seems to work... (Its working with the other sgLights now)still testing, I'll post results asap.

Edit2: I'm getting a crash on AI or Player Death, which is understandable because its similiar to the Mount vehicle crash, however I can't find a suitable spot to remove the sgLaser from the players.
#32
10/10/2009 (8:21 pm)
Are you attaching any other lights to anything else? Does the MGSK have any extra code that does anything along those lines?

EDIT: I'm guessing Armor::onDisabled() would be a good place to put the light removal on death.
#33
10/10/2009 (8:32 pm)
Nope, sgLaser is the only mounted light and the other sgLights were placed by hand in the Mission Editor.(I've only recently started looking into the actual sgLights so most of the code is completely stock)

Can't seem to find a good spot to remove the lights from the players to stop the "Death Crash" though.

P.S. It looks freakin awesome with the Lasers in a night map though!!
#34
10/10/2009 (8:36 pm)
It's odd, I have a few missions that use the sgUniversalStaticLight class, but none of them are crashing. shootLOS also defaults to false, it's only required for lights that are mounted to another object.
#35
10/10/2009 (8:39 pm)
Well maybe its because my sgLights are "sgLightObjectData" class?(Not sure what the differance is but they seem to work for me)

Btw: Heres some screens. :)
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/LightShow3.jpg
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/LightShow2.jpg
i566.photobucket.com/albums/ss101/CS_MP/MPGE/Screens/LightShow.jpg

Edit: OK, Looks like it doesnt like my MountPoint = "4" code, and that most likely was the source of the "Lighting Crash" which could actually be an AI not liking the sgLaser at "Mount #4 Crash".(I'm going to reapply the GameBase changes and test again.)

Edit 2: Thats was it, its wasnt the Lights that crashed at startup its Mount #4... testing some more...

Edit 3: Looks like I've gotten the crashes fixed, thanks for the help again guys!

Though I've only got the player to die without the crash and with the laser, but when I try with the AI I may run into problems again... Testing more...
(sometimes another angle just helps me get out of my own head, which obviously doesnt work too well on its own... lol)
:)
#36
10/10/2009 (9:09 pm)
Just to clarify something; sgLightObject and sgUniversalStaticLight are the same thing, the later is just a copy made for the TLK.
Same goes for the datablocks, sgLightObjectData and sgUniversalStaticLightData. Just make sure to use the same type of datablock for the type of object.

Checking the TLK example, it looks like the mount object check code is screwing up mounting to static shapes. May need to have additional checks to see what type of object it is trying to mount to.
#37
10/10/2009 (9:18 pm)
@ilys: Edited above post, GameBase did work and I reimplemented it into the source, it was Mount #4 being passed to the AI that was crashing the game on Mission Load, And I think the "Death Crash" was because of the GameBase change I edited out thinking it was the reason for the crash.

Still doing testing on AI and sgLaser, though it "seems" like it should work.
#38
10/10/2009 (9:36 pm)
I've hack-fixed the "attach to static shape" bug.

In shapeImage.cc replace the following function;
void ShapeBase::getRenderMuzzleTransform(U32 imageSlot,MatrixF* mat)
{
   // Muzzle transform in world space
   MountedImage& image = mMountedImageList[imageSlot];
   S32 ni = mDataBlock->mountPointNode[imageSlot];
   if (image.dataBlock)
      getRenderImageTransform(imageSlot,image.dataBlock->muzzleNode,mat);
   else if (ni != -1)
      getRenderMountTransform(imageSlot,mat);
   else
      *mat = getRenderTransform();
}

It will now try to get the mount nod if an image slot does not exist. If the mount node does not exist, it falls back to the normal render transform.
#39
10/10/2009 (9:47 pm)
Awesome, it would be a good idea to print an error to the console to notify the coder of the mismounted object as well.
#40
10/10/2009 (10:12 pm)
How are you going about fixing these crashes? You're using an IDE like VC++2008, right? You should be able to track the source of the crashes to see exactly what is going wrong. Do you get an error message, or does everything just freeze?

I'm away from my dev computer right now, otherwise I'd try implementing this code myself.