This thing is a laser-targeted machine gun turret. It will follow your laser pointer around shooting as it goes.
Download
Download: Laser Targeted Turret
Version: 1.0
Filesize: 319.15 KB
Date Added: 05-05-2010
Downloads: 267
Category: GMod
Tags: adv dup, advanced duplicator, turret, targeted, laser, e2, expression 2, garrys mod, gmod
Description: A laser targeted, Expression 2 controlled turret for Garry's Mod. Download includes a detailed picture of the components, the E2 Code, and an Advanced Duplicator file of the turret.
Construction
The construction for this one was fairly straightforward and simple, which makes the programming and operation all that much easier. I recommend using the Easy Precision tool to make precise welds and axis constraints.
Parts List:
- 1x – PHX3 Robotics Base
- 1x – PHX3 3x3x1 Block
- 1x – PHX3 Circular Plate
- 1x – PHX3 2x2x4 Block
- 2x – PHX3 2×2 Block
- 4x – Wire Turret
- 3x – Wire Entity Marker
- 1x – Wire Laser Pointer Receiver
- 1x – Wire Expression 2 Chip
Total Parts: 15
The circular plate is axis-constrained onto the 3x3x1 block, which is in turn welded onto the robotics base. A 2x2x4 block is welded to the circular plate, providing a good mount for the 2x2x2 blocks, which provide the elevation mounts for the guns themselves. These are axis-constrained onto the middle block. Three entity markers identify the three moving parts, which allows the code to actually move the parts. I went ahead and colored everything a dark gray, with the left gun being green and the right one being red; this helps identify the front of the turret.
Programming and Control
This little guy runs off of one Expression 2 chip, which does some vector to angle conversions, and then uses ApplyAngForce() to aim the turret.
Probably the most difficult part is using the ApplyAngForce correctly. Here’s a brief walk through on how it works for this turret.
Find the Target Location
This is pretty easy. Just use owner():AimPos(). This is wherever the owner of the turret is currently aiming.
Rotate the Turret
Next we want to rotate the turret so it is facing the target. We do that with these two lines:
LateralTargetAng = -ang(0, SpinPlate:bearing(AimPos), 0) * 500
SpinPlate:applyAngForce(LateralTargetAng + $LateralTargetAng * Error)
The first line does several things. First, it calculates the bearing (how far left or right) from the target. It then converts that bearing (which is just a number) into an angle (Pitch, Yaw, Roll). Pitch and Roll are zero because we only want to change the Yaw of the rotating plate. We then multiply this whole angle by 500 to give it some force when it actually moves. Lastly we negate the angle so that the turret moves towards the bearing, not away from it.
The second line is actually what moves the plate. We use ApplyAngForce on the plate, and give it the angle we just made. We also add the current angle to what the target angle was the last cycle. This is very important, as it increases the stability of the gun. What it’s doing is actually using a PID loop to give us a alternating series that converges on the target bearing, but you don’t need to know that. Just know that it makes the movements more precise and controlled.
Elevate the Guns
This part works very similarly to the rotation code, except now we’re changing the props Pitch instead of Yaw.
Here is the code that elevates the left gun mount.
LP = LeftMount:elevation(AimPos)
LeftVTA = -ang(LP, 0, 0) * 500
LeftMount:applyAngForce(LeftVTA + $LeftVTA * Error)
The first line calculates LP, which is the Left Pitch, or how far up or down the guns need to aim. We look at the elevation (up or down) from the owner’s aiming position to calculate this.
The second line then creates an angle out of the pitch, and sets yaw and roll to zero, since we only want these blocks to rotate up and down, not sideways or horizontally. We negate it so it moves towards the target pitch, and multiply it by 500 to give it some oomph.
The third line, again, does the actual moving. Again we us a Delta calculation to make our movements more precise and smooth. Error is just a number, which specifies our desired precision of error.
Note that the code for the right elevation mount is identical to the left one.
Hopefully I’ve given some insight on how to use ApplyAngForce in Wiremod and how some of those awesome automatic, kill-everyone-and-everything turrets work.


