4
\$\begingroup\$

I'm fiddling about with gMod trying to make a gamemode where you have to throw a piece of cake into a cylinder that spawns on the map.

This is the code I'm using to spawn the cylinder:

local ent = ents.Create( "prop_physics" )
ent:SetModel("models/props_phx/construct/windows/window_curve360x1.mdl")
ent:SetPos( Vector(math.random(100,2500), math.random(1500,-1000), 150) )
--ent:SetPos( math.random(-1500,-2500), math.random(-1500,-2500), 100 )
ent:Spawn()

cleanup.Add( self.Owner, "props", ent )

undo.Create( "Spawned_Cylinder" )
undo.AddEntity( ent )
undo.SetPlayer ( self.Owner )
undo.Finish()

The cake throwing part is basically just a SWEP that shoots the 'cakehat' prop. How would I test whether there is a piece of a cake inside the cylinder?

\$\endgroup\$
1
\$\begingroup\$

You take the center of the cake and determine if it's within distance of the radius and also within the distance of the height. It's been a while since I've done gmod lua so bear with me.

local cylinderCenterPos = Vector(0,0,0)
local cylinderHeight = 2
local cylinderRadius = 2
local cakePos = cake.GetPos()

local radiusDist = math.sqrt(math.pow(cylinderCenterPos.x - cakePos.x,2) + math.pow(cylinderCenterPos.y - cakePos.y,2)) --Horizontal axes
local heightDist = math.abs(cylinderCenterPos.z - cakePos.z) --Up axis

if radiusDist <= cylinderRadius and heightDist <= heightDist then
    --The cake is in the cylinder
end

Note that this will test if the center of the cake is within the cylinder and assumes a cylinder that's height is running along the up vector.

Checking on a rotated cylinder is a bit harder as you have to check using the transformed axis of the cylinder (however it's rotated).

\$\endgroup\$
  • \$\begingroup\$ I've never done gmod Lua, but I'd assume it has support for collision-detection already? It would be absurd for every mod to reimplement collision-detection on its own, in Lua, instead of using the Source engine's built-in capabilities... \$\endgroup\$ – BlueRaja - Danny Pflughoeft Nov 27 '16 at 7:34
  • 1
    \$\begingroup\$ @BlueRaja-DannyPflughoeft The only collision detection that Gmod exposes too my knowledge is StartTouch and friends b/c it only partially exposes the full Source engine capabilities. This exact thing is what brush triggers do but I don't think you can place those in Gmod (after the map is compiled in Hammer) and they also must be static. He/she is trying to make a prop_physics and also spawn it at runtime. Reimplementing it (it's only like 5 lines of code) is probably the fastest and only possible option. \$\endgroup\$ – Coburn Nov 28 '16 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.