• Getting the Wheel SEnt to move(Spawned by my own SEnt)
    4 replies, posted
Hi, i'm making an entity which needs wheels so it can move. But for some reason the wheels won't move at all. Here's my code: [lua] function ENT:SpawnWheels() local model = "models/madjawa/malp/malpwheel.mdl" local pos = {} pos[1] = self:GetPos()+self:GetRight()*-25+self:GetUp()*-5+self:GetForward()*5 pos[2] = self:GetPos()+self:GetRight()*25+self:GetUp()*-5+self:GetForward()*5 pos[3] = self:GetPos()+self:GetRight()*25+self:GetForward()*40+self:GetUp()*-5 pos[4] = self:GetPos()+self:GetRight()*-25+self:GetForward()*40+self:GetUp()*-5 pos[5] = self:GetPos()+self:GetRight()*25+self:GetForward()*-30+self:GetUp()*-5 pos[6] = self:GetPos()+self:GetRight()*-25+self:GetForward()*-30+self:GetUp()*-5 local ang = {} ang[1] = self:GetAngles()+Angle(90,0,0) ang[2] = self:GetAngles()+Angle(-90,180,0) ang[3] = self:GetAngles()+Angle(-90,180,0) ang[4] = self:GetAngles()+Angle(90,0,0) ang[5] = self:GetAngles()+Angle(-90,180,0) ang[6] = self:GetAngles()+Angle(90,0,0) for i=1,6 do local e = ents.Create("gmod_wheel") e:SetModel(model) e:SetPos(pos[i]) e:SetAngles(ang[i]) e:Spawn() e:Activate() e:SetOwner(self) e.Motor=self e.Motor.forcescale=1 --e:SetParent(self) constraint.Weld(e,self,0,0,0,true) self.MalpWheels[i]=e end end function ENT:OnRemove() for i=1,6 do self.MalpWheels[i]:Remove() end end function ENT:Think() if(self.Control) then if(self.Controler:KeyDown(IN_FORWARD)) then print("See i told you so") for i=1,6 do self.MalpWheels[i].Motor.direction=1 self.MalpWheels[i]:SetTorque(10000) self.MalpWheels[i].Toggle=true self.MalpWheels[i].ToggleState=true end self:MoveForward() print("bah humbug") end if(self.Controler:KeyDown(IN_MOVELEFT)) then self:TurnLeft() elseif(self.Controler:KeyReleased(IN_MOVELEFT)) then self.Rotated=false end if(self.Controler:KeyDown(IN_MOVERIGHT)) then self:TurnRight(plus) end end end function ENT:MoveForward() if(ValidEntity(self)) then for i=1,6 do self.MalpWheels[i]:Forward(true,1) end end end [/lua] Any ideas?
You could just spawn the wheel as a prop_physics and then use AddAngleVelocity. [lua] local phys = wheel:GetPhysicsObject() local dir = wheel:GetForward() --Angle or vector phys:AddAngleVelocity( dir * Torque ) [/lua]
[QUOTE=Sakarias88;23617808]You could just spawn the wheel as a prop_physics and then use AddAngleVelocity. [lua] local phys = wheel:GetPhysicsObject() local dir = wheel:GetForward() --Angle or vector phys:AddAngleVelocity( dir * Torque ) [/lua][/QUOTE] I'll try that thanks [editline]07:19PM[/editline] It doesn't move my entity for some reason. I see dust from the wheel prop as if it's trying to move and the whole thing wobbles about but nothing else happens. Here's my code again: Wheel SEnt: [lua] include("shared.lua") AddCSLuaFile("shared.lua") AddCSLuaFile("cl_init.lua") function ENT:SpawnFunction(pl,tr) if (!tr.HitWorld) then return end; local e = ents.Create("malp_wheel"); e:Spawn(); e:Activate(); return e; end function ENT:Initialize() self:SetModel("models/madjawa/malp/malpwheel.mdl") self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) self.Phys = self:GetPhysicsObject() end function ENT:MoveForward(b) if(ValidEntity(self)) then if(b) then local phys = self:GetPhysicsObject() local dir = self:GetForward() phys:AddAngleVelocity(dir*10000) end end end [/lua] My Entity: [lua] function ENT:SpawnWheels() local model = "models/madjawa/malp/malpwheel.mdl" local pos = {} pos[1] = self:GetPos()+self:GetRight()*-25+self:GetUp()*-5+self:GetForward()*5 pos[2] = self:GetPos()+self:GetRight()*25+self:GetUp()*-5+self:GetForward()*5 pos[3] = self:GetPos()+self:GetRight()*25+self:GetForward()*40+self:GetUp()*-5 pos[4] = self:GetPos()+self:GetRight()*-25+self:GetForward()*40+self:GetUp()*-5 pos[5] = self:GetPos()+self:GetRight()*25+self:GetForward()*-30+self:GetUp()*-5 pos[6] = self:GetPos()+self:GetRight()*-25+self:GetForward()*-30+self:GetUp()*-5 local ang = {} ang[1] = self:GetAngles()+Angle(90,0,0) ang[2] = self:GetAngles()+Angle(-90,180,0) ang[3] = self:GetAngles()+Angle(-90,180,0) ang[4] = self:GetAngles()+Angle(90,0,0) ang[5] = self:GetAngles()+Angle(-90,180,0) ang[6] = self:GetAngles()+Angle(90,0,0) for i=1,6 do local e = ents.Create("malp_wheel") e:SetPos(pos[i]) e:SetAngles(ang[i]) e:Spawn() e:Activate() -- e:SetOwner(self) -- e:SetParent(self) constraint.Weld(e,self,0,0,0,true) self.MalpWheels[i]=e end end function ENT:OnRemove() for i=1,6 do self.MalpWheels[i]:Remove() end end function ENT:Think() if(self.Control) then if(ValidEntity(self.Controler)) then for i=1,6 do if(self.Controler:KeyDown(IN_FORWARD)) then self.MalpWheels[i]:MoveForward(true) else self.MalpWheels[i]:MoveForward(false) end end if(self.Controler:KeyDown(IN_MOVELEFT)) then self:TurnLeft() elseif(self.Controler:KeyReleased(IN_MOVELEFT)) then self.Rotated=false end if(self.Controler:KeyDown(IN_MOVERIGHT)) then self:TurnRight() end end end end [/lua] This is getting quite annoying now [editline]08:37PM[/editline] I have figured out that the reason it's not moving is because the wheels are welded(Duh im soo stupid) to the entity. Problem is if i use [lua]e:SetParent(self)[/lua] the wheel's lose collision. what else can i do?
[QUOTE=Ronon Dex;23617849]I'll try that thanks [editline]07:19PM[/editline] It doesn't move my entity for some reason. I see dust from the wheel prop as if it's trying to move and the whole thing wobbles about but nothing else happens. Here's my code again: Wheel SEnt: [lua] include("shared.lua") AddCSLuaFile("shared.lua") AddCSLuaFile("cl_init.lua") function ENT:SpawnFunction(pl,tr) if (!tr.HitWorld) then return end; local e = ents.Create("malp_wheel"); e:Spawn(); e:Activate(); return e; end function ENT:Initialize() self:SetModel("models/madjawa/malp/malpwheel.mdl") self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) self.Phys = self:GetPhysicsObject() end function ENT:MoveForward(b) if(ValidEntity(self)) then if(b) then local phys = self:GetPhysicsObject() local dir = self:GetForward() phys:AddAngleVelocity(dir*10000) end end end [/lua] My Entity: [lua] function ENT:SpawnWheels() local model = "models/madjawa/malp/malpwheel.mdl" local pos = {} pos[1] = self:GetPos()+self:GetRight()*-25+self:GetUp()*-5+self:GetForward()*5 pos[2] = self:GetPos()+self:GetRight()*25+self:GetUp()*-5+self:GetForward()*5 pos[3] = self:GetPos()+self:GetRight()*25+self:GetForward()*40+self:GetUp()*-5 pos[4] = self:GetPos()+self:GetRight()*-25+self:GetForward()*40+self:GetUp()*-5 pos[5] = self:GetPos()+self:GetRight()*25+self:GetForward()*-30+self:GetUp()*-5 pos[6] = self:GetPos()+self:GetRight()*-25+self:GetForward()*-30+self:GetUp()*-5 local ang = {} ang[1] = self:GetAngles()+Angle(90,0,0) ang[2] = self:GetAngles()+Angle(-90,180,0) ang[3] = self:GetAngles()+Angle(-90,180,0) ang[4] = self:GetAngles()+Angle(90,0,0) ang[5] = self:GetAngles()+Angle(-90,180,0) ang[6] = self:GetAngles()+Angle(90,0,0) for i=1,6 do local e = ents.Create("malp_wheel") e:SetPos(pos[i]) e:SetAngles(ang[i]) e:Spawn() e:Activate() -- e:SetOwner(self) -- e:SetParent(self) constraint.Weld(e,self,0,0,0,true) self.MalpWheels[i]=e end end function ENT:OnRemove() for i=1,6 do self.MalpWheels[i]:Remove() end end function ENT:Think() if(self.Control) then if(ValidEntity(self.Controler)) then for i=1,6 do if(self.Controler:KeyDown(IN_FORWARD)) then self.MalpWheels[i]:MoveForward(true) else self.MalpWheels[i]:MoveForward(false) end end if(self.Controler:KeyDown(IN_MOVELEFT)) then self:TurnLeft() elseif(self.Controler:KeyReleased(IN_MOVELEFT)) then self.Rotated=false end if(self.Controler:KeyDown(IN_MOVERIGHT)) then self:TurnRight() end end end end [/lua] This is getting quite annoying now[/QUOTE] It seems like you are just not using enough force to make it move. Applying physical forces in the think function can make the movement a bit choppy since it only runs about 5 times per second (at least on my computer it's a 0.22 sec delay). You can solve this by changing how often the think function should run or you can do it in another function that runs more often. You can also help the vehicle move by applying force on it or you could just use the physics simulate func. [lua] --0 is neutral, -1 is backwards and 1 is forward ENT.LeftWheelRow = 0 ENT.RightWheelRow = 0 function ENT:PhysicsUpdate( physics ) if self.LeftWheelRow != 0 then local dir = some kind of direction for i = 1, NrOfLeftWheels do local phys = self.LefWheels[i]:GetPhysicsObject() local dir = self:GetForward() phys:AddAngleVelocity(dir * Torq * self.LeftWheelRow ) end end if self.RightWheelRow != 0 then local dir = some kind of direction for i = 1, NrOfRightWheels do local phys = self.LefWheels[i]:GetPhysicsObject() local dir = self:GetForward() phys:AddAngleVelocity(dir * Torq * self.RightWheelRow ) end end end function ENT:Think() if(self.Control) then if(ValidEntity(self.Controler)) then if(self.Controler:KeyDown(IN_FORWARD)) then self.LeftWheelRow = 1 self.RightWheelRow = 1 elseif(self.Controler:KeyDown(IN_BACK)) then self.LeftWheelRow = -1 self.RightWheelRow = -1 else self.LeftWheelRow = 0 self.RightWheelRow = 0 end if(self.Controler:KeyDown(IN_MOVELEFT)) then self.LeftWheelRow = -1 self.RightWheelRow = 1 elseif(self.Controler:KeyReleased(IN_MOVERIGHT)) then self.LeftWheelRow = 1 self.RightWheelRow = -1 end end end end [/lua]
It just sort of wobbles on the spot if i do that, and the torque is at 100000000000000000000 and still doesnt go forward
Sorry, you need to Log In to post a reply to this thread.