Jump to content

Checking a Surface's Size


reepblue
 Share

Recommended Posts

Hey all.

 

I've been looking all over the api reference and the forums about this, but is there a way to check a surface's surface area (size)? I want to place a model on the wall with a raycast only if the surface is big enough.

 

Thanks.

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Here is my code snippet. What value(s) would be best as each brush can have different dimentions. How would I go and say "If this face is bigger than 96cm x 96cm, then allow placement." ?

 

if pickinfo.surface~=nil then

 --Print out the surface AABB
 local v1 = pickinfo.surface:GetAABB()
 --local v2 = c:GetAABB(Entity.LocalAABB)
 v1:Update()
 --v2:Update()
 System:Print("Surface ")
 System:Print(v1)
 --System:Print("Cyclone ")
 --System:Print(v2)
 if v1.min.y > 1 then
  System:Print("!!!SUCESSFUL PLACEMENT!!!")
  c = tolua.cast(c,"Model")
  c:SetPosition(pickinfo.position)
  c:SetParent(pickinfo.entity)
  c:AlignToVector(pickinfo.normal)
  c:SetScript("Scripts/Objects/Cyclone/Cyclone.lua",false)
  c.script:SetPushDir(pickinfo.normal.x,pickinfo.normal.y,pickinfo.normal.z)
  c.script:SetStartOpen()
  c.script:Start()
  self.LastCyclone = c
 end
end

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

This code will do a basic job of doing what you want, but it has limitations.

 

local hitPos = pickInfo.position
local surface = pickInfo.surface
local canPlace = true

for v = 0, surface:CountVertices() - 1 do
local vertPos = surface:GetVertexPosition(v)

if hitPos:DistanceToPoint(vertPos) < (0.96 / 2) then
 canPlace = false
end
end

print(canPlace)

Link to comment
Share on other sites

I have an idea. I'll put into pseudo code, then I'll try to figure it out in code.

 

for all connecting vertices,

find the closest point along that line to the hit point.

If the distance between that point and the hit point is < 96 / 2 then don't spawn object.

 

Edit: We would also need to ignore the line that goes diagonally.

Link to comment
Share on other sites

I figured it out, but it's quite slow.

 

local hitPos = pickInfo.position
local surface = pickInfo.surface
local canPlace = true

for v1 = 0, surface:CountVertices() - 1 do
local vertPos1 = surface:GetVertexPosition(v1)
for v2 = 0, surface:CountVertices() - 1 do
if v1 ~= v2 then
 local vertPos2 = surface:GetVertexPosition(v2)
 if (vertPos1.x == vertPos2.x and vertPos1.y == vertPos2.y)
 or (vertPos1.x == vertPos2.x and vertPos1.z == vertPos2.z)
 or (vertPos1.y == vertPos2.y and vertPos1.z == vertPos2.z) then
 local closestPoint = self:ClosestPointOnLine(vertPos1, vertPos2, hitPos)

 if hitPos:DistanceToPoint(closestPoint) < (0.96 / 2) then
 canPlace = false
 end
 end
end
end
end

if canPlace == true then
local bulletDecal = Decal:Create(self.bulletDecalMaterial)
bulletDecal:SetPosition(pickInfo.position, true)
bulletDecal:AlignToVector(pickInfo.normal * -1)
bulletDecal:SetScale(Vec3(0.1, 0.1, 1))
bulletDecal:SetParent(pickInfo.entity)
end

 

function Script:ClosestPointOnLine(vA, vB, vPoint)
local vVector1 = vPoint - vA
local vVector2 = (vB - vA):Normalize()

local d = vA:DistanceToPoint(vB)
local t = vVector2:Dot(vVector1)

if t <= 0 then
return vA
end
if t >= d then
return vB
end

local vVector3 = vVector2 * t
local vClosestPoint = vA + vVector3

return vClosestPoint
end

 

Edit: This is slow because somehow the vertex count on a CSG cube is 128. WTF?

 

Edit: It seems like the engine is combining CSG meshes with the same texture to save draw calls. This is causing the above code to be slow. PickInfo.face.surface would solve this issue, but it doesn't seem to work.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...