I have 2 cubes, and i’m trying to make them attach to each other on attachment points I set on the model. There is 1 point on each side of the cube in the center. I’ve got them going to the points mostly, but I can’t seem to get the angles to square up. I don’t want to rotate the cube much seeing as the two points have been touched together, like top to right or left to front(that code works ok), but just so the edges are square with each other.
Here is the code I’m using right now, i’m going to keep working on solving this, but help at this point is very much appreciated. Thanks in advance.
[lua]
function ENT:StartTouch( ent )
local hEnt = ent -- Did this because this was originally being done in PhysicsCollide, was local hEnt = data.HitEntity
local shouldExit = false
if IsClass(hEnt, "power_gen") then
print(tostring(self) .. "'s velocity is " .. tostring(self:GetVelocity()) .. " Velocity Length: " .. tostring(self:GetVelocity():Length()) )
print(tostring(hEnt) .. "'s velocity is " .. tostring(hEnt:GetVelocity()) .. " Velocity Length: " .. tostring(hEnt:GetVelocity():Length()) )
--if self:GetPhysicsObject():IsMoveable() and
-- !hEnt:GetPhysicsObject():IsMoveable() then return end
if self:GetVelocity():Length() < hEnt:GetVelocity():Length() then
print(tostring(hEnt) .. "(hEnt) is attaching to " .. tostring(self))
else
print(tostring(self) .. "(self) is attaching to " .. tostring(hEnt))
end
for _, gen in pairs(hEnt.AttachedGens) do
if gen == self then shouldExit = true end
if table.HasValue(gen.AttachedGens, self) or
table.HasValue(gen.AttachedGens, hEnt) then
shouldExit = true
end
end
if shouldExit then return end
local winDist = 5
local winAt1 = ""
local winAt2 = ""
for ent1At, attached1 in pairs(self.Attachments) do
for ent2At, attached2 in pairs(hEnt.Attachments) do
local distCalc = self:GetAttachment(self:LookupAttachment(ent1At)).Pos:Distance( hEnt:GetAttachment(hEnt:LookupAttachment(ent2At)).Pos )
--print(tostring(self) .. "'s attachment point " .. ent1At .. " is " .. tostring(distCalc) .. " from attachment point " .. ent2At .. " on " .. tostring(hEnt))
if distCalc < winDist and
!(attached1 or attached2) then
winDist = distCalc
winAt1 = ent1At
winAt2 = ent2At
end
end
end
if winAt1 == "" or
winAt2 == "" then
print("No Suitible Attachment Point Found")
self.attaching = false
hEnt.attaching = false
return
end
print(tostring(self) .. " should attach it's " .. winAt1 .. " to " .. tostring(hEnt) .. "'s " .. winAt2 )
print(tostring(self) .. " attachment ID is " .. tostring(self:LookupAttachment(winAt1)))
print(tostring(hEnt) .. " attachment ID is " .. tostring(hEnt:LookupAttachment(winAt2)))
print("winAt1 Pos : " .. tostring(self:GetAttachment(self:LookupAttachment(winAt1)).Pos))
print(tostring(self) .. " pos : " .. tostring(self:GetPos()))
print("winAt2 Pos : " .. tostring(hEnt:GetAttachment(hEnt:LookupAttachment(winAt2)).Pos))
print(tostring(hEnt) .. " pos : " .. tostring(hEnt:GetPos()))
local atPos1 = self:GetAttachment(self:LookupAttachment(winAt1)).Pos
local atPos2 = hEnt:GetAttachment(hEnt:LookupAttachment(winAt2)).Pos
local atOffset1 = (self:GetPos() - atPos1) * 1.2
local atOffset2 = hEnt:GetPos() - atPos2
print("atOffset1 : " .. tostring(atOffset1))
print("atOffset2 : " .. tostring(atOffset2))
--self:SetAttachment(hEnt:GetAttachment(hEnt:LookupAttachment(winAt2)))
self:SetPos((atPos2 + atOffset1))
local sAngles = self:GetAngles()
local hAngles = hEnt:GetAngles()
if winAt1 == "top" then sAngles = self:GetUp():Angle() end
if winAt1 == "bottom" then sAngles = self:GetUp():Angle() * -1 end
if winAt1 == "right" then sAngles = self:GetRight():Angle() end
if winAt1 == "left" then sAngles = self:GetRight():Angle() * -1 end
if winAt1 == "front" then sAngles = self:GetForward():Angle() end
if winAt1 == "back" then sAngles = self:GetForward():Angle() *-1 end
if winAt2 == "top" then hAngles = hEnt:GetUp():Angle() * -1 end
if winAt2 == "bottom" then hAngles = hEnt:GetUp():Angle() end
if winAt2 == "right" then hAngles = hEnt:GetRight():Angle() * -1 end
if winAt2 == "left" then hAngles = hEnt:GetRight():Angle() end
if winAt2 == "front" then hAngles = hEnt:GetForward():Angle() * -1 end
if winAt2 == "back" then hAngles = hEnt:GetForward():Angle() end
self:SetAngles(self:AlignAngles(sAngles, hAngles))
constraint.Weld( self, hEnt, 0,0,0,true)
table.insert(self.AttachedGens, hEnt)
table.insert(hEnt.AttachedGens, self)
self.Attachments[winAt1] = true
hEnt.Attachments[winAt2] = true
end
end
[/lua]