Programming a Gravity Gun
by Chad Kilgore · in Torque Game Engine · 07/18/2007 (1:38 pm) · 34 replies
I am trying to program a would-be gravity gun but am having some problems in the direction that I am taking. Using part of the Item Collision (specifically a variation on the BlockWeapon::throwBlock function), I am easily able to push objects away from me.
My idea for pulling objects towards the player is to use a path marker mounted at the muzzle point and then assign the selected object to follow the path to that marker. What do people think of this idea? Or does somebody see an easier (or already implemented) way to have a weapon similar to the Half-Life 2 gravity gun? Thanks everybody.
My idea for pulling objects towards the player is to use a path marker mounted at the muzzle point and then assign the selected object to follow the path to that marker. What do people think of this idea? Or does somebody see an easier (or already implemented) way to have a weapon similar to the Half-Life 2 gravity gun? Thanks everybody.
#2
07/18/2007 (2:32 pm)
I've looked into ODEScript before. I think that this option is not viable for reasons that the game ultimately is designed to be played on lower-end machines. Also I don't need "real" physics, just an approximation of its behavior.
#3
if you're not opposed to going into C a lil' bit,
you could just add a "gravity gun force" on the server-side player object,
expose it to script, ghost it down in packUpdate(), and factor it in after this line in player.cc:
07/18/2007 (2:36 pm)
Well,if you're not opposed to going into C a lil' bit,
you could just add a "gravity gun force" on the server-side player object,
expose it to script, ghost it down in packUpdate(), and factor it in after this line in player.cc:
acc += (mAppliedForce / mMass) * TickSec;
#4

Click image to download video
I don't know how else to achive such a cool effect without using physics.
07/18/2007 (4:08 pm)
A friend of mine got something that's pretty fun working with PhysX:
Click image to download video
I don't know how else to achive such a cool effect without using physics.
#5
07/18/2007 (4:25 pm)
Wow the video looks really cool !!
#7
What I am really needing though is an incredibly simple gravity gun. One that pulls objects towards you and then pushes them away, almost as if I have a powerful vacuum cleaner on suction and then reverse.
07/18/2007 (5:29 pm)
That is one of the coolest implementation of a physics gun I have ever seen. Kudos!What I am really needing though is an incredibly simple gravity gun. One that pulls objects towards you and then pushes them away, almost as if I have a powerful vacuum cleaner on suction and then reverse.
#8
07/18/2007 (5:42 pm)
That looks so fun
#9
Didn't turn out too bad for a 10 minute hack job - the weapon casts a ray that searches for RigidShapes and if it finds one sets the transform of the targeted RigidShape to the current player's transform.
Ultimately you'd want to interpolate between the player and target's transform and include a method for returning the object on mouse button up, voila there's your vacuum gun done entirely in script.
07/19/2007 (12:45 am)
Lol, I thought I'd have a quick go at scripting this in Torque: GravGun_Torque Video (17 MB)Didn't turn out too bad for a 10 minute hack job - the weapon casts a ray that searches for RigidShapes and if it finds one sets the transform of the targeted RigidShape to the current player's transform.
Ultimately you'd want to interpolate between the player and target's transform and include a method for returning the object on mouse button up, voila there's your vacuum gun done entirely in script.
#10

Click image to download video
^ Poor man's gravity gun - I'll stop now.
07/19/2007 (5:03 am)
Half Life 2 eat your heart out (not really):
Click image to download video
^ Poor man's gravity gun - I'll stop now.
#11
07/19/2007 (5:05 am)
Cool. :) Code snippets?
#12
07/19/2007 (5:09 am)
Sure.function gravityGunImage::onFire(%this, %obj, %slot)
{
// cast a ray to determine if we're targeting a RigidShape object
//
// muzVec is the vector coming from the gravity gun's "muzzle"
%muzVec = %obj.getMuzzleVector(%slot);
// muzNVec = normalized muzVec
%muzNVec = VectorNormalize(%muzVec);
// range of ray
%range = 100;
// distance to player - how far the object hovers from the player
%distance = 10;
// scale muzNVec to the range the gravity beam can reach
%muzScaled = VectorScale(%muzNVec, %range);
// muzPoint = the actual point of the gun's "muzzle"
%muzPoint = %obj.getMuzzlePoint(%slot);
// rangeEnd = muzzle point + length of beam
%rangeEnd = VectorAdd(%muzPoint, %muzScaled);
// search for ShapeBase objects
%searchMasks = $TypeMasks::ShapeBaseObjectType;
// search for objects within the beam's range that fit the mask above
%scanTarg = ContainerRayCast(%muzPoint, %rangeEnd, %searchMasks, %obj);
//
// We found something, let's rock
if(%scanTarg)
{
// object's server ID
%targetID = getWord(%scanTarg, 0);
// calculate where crosshair is aiming and create a barrier
%muzScale = VectorScale(%muzNVec, %distance);
%distance = VectorAdd(%muzPoint, %muzScale);
//
// Only pick up RigidShapes
if(%targetID.getClassname() $= "RigidShape")
%targetID.setTransform(%distance);
}
}Only works on RigidShapes and it needs some tweaking.
#13
07/19/2007 (5:44 am)
Nice work :) looks great
#14
You could also use the same effect for a gravity grenade that grabs everything within a circle of the blast and pulls them towards the grenade. Fun!
07/19/2007 (5:57 am)
Thanks! I could see adding a variable distance to this so that after a falloff, only a portion of the vector gets added... to kinda slowly pull it towards you if far away, then snap and grab it. Maybe also tie that into the power/energy available level.You could also use the same effect for a gravity grenade that grabs everything within a circle of the blast and pulls them towards the grenade. Fun!
#15
07/19/2007 (7:38 am)
How is it that your video shows that the barrel coming to your position over a period of time and when I use your code snippet (thank you very much, by the way) my object immediately transforms to the distance position?
#16
P.S. my code is very hacky and is not the right way of doing it - but hey it was fun :P
07/19/2007 (7:42 am)
Are you using a RigidShape?P.S. my code is very hacky and is not the right way of doing it - but hey it was fun :P
#17
At the console, set the timescale valeue to 0.3 and then try Tim's weapon mod. you should see a slow and steady movement toward your viewport.
@Tim
Excellent script snippet! Thanks for posting it.
Since dropping it in, I've couped it with a force gun I built a few months ago. By mounting my force gun on node 1 (right click to fire) and your grav gun mounted on node 0, I can achieve something like HL2's Zero Point Gravity gun.
Also, since my cars inflict velocity damage on collision, I added wheeled vehicles to the "pick up" mask so I could grab and throw cars for defense and offense.
Using your grav gun effect in conjunction with my force gun is loads of fun. :)
I'll try to post a vid.
07/19/2007 (7:54 am)
@ChadAt the console, set the timescale valeue to 0.3 and then try Tim's weapon mod. you should see a slow and steady movement toward your viewport.
$timescale=0.3
@Tim
Excellent script snippet! Thanks for posting it.
Since dropping it in, I've couped it with a force gun I built a few months ago. By mounting my force gun on node 1 (right click to fire) and your grav gun mounted on node 0, I can achieve something like HL2's Zero Point Gravity gun.
Also, since my cars inflict velocity damage on collision, I added wheeled vehicles to the "pick up" mask so I could grab and throw cars for defense and offense.
Using your grav gun effect in conjunction with my force gun is loads of fun. :)
I'll try to post a vid.
#19
Read the first part of my lat post. I'm betting that if you slow down the simulation time of your Torque game, you'll see that it does move properly, just really fast. :)
07/19/2007 (8:11 am)
@ChadRead the first part of my lat post. I'm betting that if you slow down the simulation time of your Torque game, you'll see that it does move properly, just really fast. :)
#20
07/19/2007 (8:12 am)
Try setting the mass of your rigid shape quite high, I use around 500.
Torque Owner Anton Bursch