shadmar Posted November 11, 2014 Share Posted November 11, 2014 I know it's not documented/unsupported atm, but just tryin out from the Vehicle class: Found these, but crashed on my first try (if anyone figures it out let me know) : //Tires virtual int AddTire(float x, float y, float z, float mass, float radius, float width, bool steering, float suspensionDamper = 200.0, float suspensionSpring = 2000.0, float suspensionLength = 1.2, float lateralStiffness = 20.0, float longitudinalStiffness = 100000.0, float aligningMOmentTrail = 1.5) = 0;//lua virtual void AddAxle(int lefttire, int righttire) = 0;//lua //Optional settings virtual void SetGearRatio(std::vector<float> forwardGearRatio, float reverseGearRatio) = 0;//lua virtual void SetTorqueCurve(float topSpeedKPH, float idleTorquePoundPerFoot, float idleRPM, float peakTorquePoundPerFoot, float peakTorqueRPM, float peakHorsePower, float peakHorsePowerRPM, float redLineTorquePoundPerFoot, float redLineRPM) = 0;//lua //Build virtual bool Build() = 0;//lua //Control virtual void SetAcceleration(float engineGasPedal) = 0;//lua virtual void SetSteering(float steering) = 0;//lua virtual void SetBrakes(float brakes) = 0;//lua virtual void SetHandBrakes(float brakes) = 0;//lua virtual void SetGear(int gear) = 0;//lua virtual void SetTransmissionMode(bool automatic) = 0;//lua virtual void SetEngineRunning(const bool running) = 0;//lua //Information virtual Mat4 GetTireMatrix(int tireindex)=0;//lua virtual int GetGear() = 0;//lua virtual float GetRPM() = 0;//lua virtual float GetSpeed() = 0;//lua virtual int CountTires()=0;//lua virtual float GetTireLateralForce(const int index)=0;//lua virtual bool GetEngineRunning()=0;//lua virtual float GetTireSlip(const int index) = 0;//lua static Vehicle* Create(Entity* entity);//lua Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Josh Posted November 11, 2014 Share Posted November 11, 2014 import "Scripts/Functions/ReleaseTableObjects.lua" Script.acceleration=10 Script.tiremass=100 Script.followdistance=4 Script.cameraangle=35 Script.camerastyle=0 Script.cameramovespeed=0.5 Script.smoothedcamerarotation=Vec3(0) Script.cameralooksmoothing = 5 Script.cameraradius = 0.25 Script.mincameraangle = -15 Script.maxcameraangle = 55 Script.enableESP=true function Script:Start() local n,box local child={} local tirepos local tirewidth = 0.25 local tireradius = 0.4 self.sound={} self.sound.engineloop=Sound:Load("Sound/Vehicles/revMiddle.wav") self.sound.enginestart=Sound:Load("Sound/Vehicles/starter.wav") self.sound.tireskid=Sound:Load("Sound/Vehicles/tire_skid.wav") self.source={} self.tires={} --Build the vehicle self.vehicle = Vehicle:Create(self.entity) for n=0,7 do child[0] = self.entity:FindChild("tire"..tostring(n*2+0)) child[1] = self.entity:FindChild("tire"..tostring(n*2+1)) if child[0]~=nil and child[1]~=nil then for i=0,1 do box = self:CalculateTireSize(child) if box~=nil then tireradius = math.max(box.size.y,box.size.z)*0.5 tirewidth = box.size.x tirepos = box.center + child:GetPosition(false) self.vehicle:AddTire(tirepos.x,tirepos.y,tirepos.z,self.tiremass,tireradius,tirewidth,n<1) table.insert(self.tires,child) end end if n>0 then self.vehicle:AddAxle(self.vehicle:CountTires()-2,self.vehicle:CountTires()-1) end end end self.vehicle:Build() if self.camerastyle==0 then local context = Context:GetCurrent() local cx=math.floor(context:GetWidth()/2+0.5) local cy=math.floor(context:GetHeight()/2+0.5) Window:GetCurrent():SetMousePosition(cx,cy) end --Create follow camera self.camera = Camera:Create() self:UpdateCamera(0) end function Script:CalculateTireSize(entity) local n,model,surf,v,p,box if entity:GetClass()==Object.ModelClass then model = tolua.cast(entity,"Model") for n=0,model:CountSurfaces()-1 do surf = model:GetSurface(n) for v=0,surf:CountVertices()-1 do p = surf:GetVertexPosition(v) * entity.scale if box==nil then box=AABB(p,p) else box.min.x = math.min(box.min.x,p.x) box.min.y = math.min(box.min.y,p.y) box.min.z = math.min(box.min.z,p.z) box.max.x = math.max(box.max.x,p.x) box.max.y = math.max(box.max.y,p.y) box.max.z = math.max(box.max.z,p.z) end end end end if box~=nil then box:Update() end return box end function Script:UpdateCamera(smoothing) local speed = Time:GetSpeed() local n --Camera position local aabb = self.entity:GetAABB(Entity.GlobalAABB) local vehiclepos = aabb.center self.cameraposition = vehiclepos self.camera:SetPosition(self.cameraposition) --Camera rotation if self.camerastyle==0 then local context = Context:GetCurrent() local cx=math.floor(context:GetWidth()/2+0.5) local cy=math.floor(context:GetHeight()/2+0.5) local window = Window:GetCurrent() local mousepos = window:GetMousePosition() local dx = mousepos.x-cx local dy = mousepos.y-cy if self.camerarotation==nil then self.camerarotation = Vec3(self.entity:GetRotation(true).y,35,0) end if self.smoothedcamerarotation==nil then self.smoothedcamerarotation = self.camerarotation end self.camerarotation.x = self.camerarotation.x + dx * self.cameramovespeed * speed self.camerarotation.y = self.camerarotation.y + dy * self.cameramovespeed * speed self.camerarotation.y = Math:Clamp(self.camerarotation.y,self.mincameraangle,self.maxcameraangle) self.smoothedcamerarotation.x = Math:CurveAngle(self.camerarotation.x, self.smoothedcamerarotation.x, self.cameralooksmoothing / speed) self.smoothedcamerarotation.y = Math:CurveAngle(self.camerarotation.y, self.smoothedcamerarotation.y, self.cameralooksmoothing / speed) self.camera:SetRotation(0,self.smoothedcamerarotation.x,0) self.camera:Turn(self.smoothedcamerarotation.y,0,0) window:SetMousePosition(cx,cy) else local targetrot = self.entity:GetRotation(true) if self.camerarotation==nil then self.camerarotation = targetrot end self.smoothedcamerarotation.x = 0 self.smoothedcamerarotation.z = 0 self.smoothedcamerarotation.y = Math:CurveAngle(targetrot.y,self.camera.rotation.y,self.cameralooksmoothing/speed) self.camera:SetRotation(self.smoothedcamerarotation) self.camera:Turn(self.cameraangle,0,0) end self.camera:Move(0,0,-self.followdistance) local pickinfo = PickInfo() self.entity:Hide() if self.camera.world:Pick(vehiclepos,self.camera:GetPosition(true),pickinfo,self.cameraradius,true) then self.camera:SetPosition(pickinfo.position,true) end self.entity:Show() --Tire skidding local slip=0 if self.tireslipvolume==nil then self.tireslipvolume=0 end for n=0,self.vehicle:CountTires()-1 do slip = math.max(slip,self.vehicle:GetTireSlip(n)) end if slip>0.1 and self.entity:GetVelocity(false):xz():Length()>2.23694*3 then self.tireslipvolume = self.tireslipvolume + 0.1 self.tireslipvolume = math.min(self.tireslipvolume,1) else self.tireslipvolume = self.tireslipvolume - 0.1 self.tireslipvolume = math.max(self.tireslipvolume,0) end if self.tireslipvolume>0 then if self.sound.tireskid~=nil then if self.source.tireskid==nil then self.source.tireskid = Source:Create() self.source.tireskid:SetSound(self.sound.tireskid) self.source.tireskid:SetLoopMode(true) self.source.tireskid:Play() else self.source.tireskid:Resume() end self.source.tireskid:SetVolume(self.tireslipvolume) end else if self.source.tireskid~=nil then if self.source.tireskid:GetState()==Source.Playing then self.source.tireskid:Pause() end end end end function Script:UpdatePhysics() local window = Window:GetCurrent() local steerangle=0 if window:KeyDown(Key.D) then steerangle = steerangle + 45 end if window:KeyDown(Key.A) then steerangle = steerangle - 45 end local brakes=0 if window:KeyDown(Key.Space) then brakes = 5000 end self.vehicle:SetBrakes(brakes) if self.enableESP then local force=0 local n local threshold=50 for n=0,self.vehicle:CountTires()-1 do force = math.max(force, self.vehicle:GetTireLateralForce(n)) end force = force / self.tiremass self.steerangle = steerangle * Math:Clamp((threshold-(force-threshold))/threshold,0,1) --local roll = math.abs(self.entity:GetRotation().z) --steerangle = steerangle * Math:Clamp((5-roll)/5,0,1) end if self.smoothedsteerangle==nil then self.smoothedsteerangle = self.steerangle end self.smoothedsteerangle = Math:Inc(self.steerangle,self.smoothedsteerangle,3) self.vehicle:SetSteering(self.smoothedsteerangle) self.steerangle=steerangle ---------------------------------------------- --Handle acceleration ---------------------------------------------- local gas = 0 if window:KeyDown(Key.W) then gas = gas + self.acceleration --Start engine if self.vehicle:GetEngineRunning()==false then self.enginestarttime=Time:GetCurrent() self.vehicle:SetEngineRunning(true) self.vehicle:SetHandBrakes(0) if self.sound.enginestart~=nil then if self.source.enginestart==nil then self.source.enginestart=Source:Create() self.source.enginestart:SetSound(self.sound.enginestart) end self.source.enginestart:Play() end if self.source.engineloop==nil then if self.sound.engineloop~=nil then self.source.engineloop=Source:Create() self.source.engineloop:SetSound(self.sound.engineloop) self.source.engineloop:SetLoopMode(true) end end end end if window:KeyDown(Key.S) then gas = gas - self.acceleration end if self.vehicle:GetEngineRunning() then if Time:GetCurrent()-self.enginestarttime>2000 then local gear if gas<0 then gas = -gas gear = -1 else if window:KeyDown(Key.Shift) then gas = gas * 100 end gear = 1 end if self.smoothedgas==nil then self.smoothedgas = 0 end if gas>self.smoothedgas then self.smoothedgas = Math:Inc(gas,self.smoothedgas,0.01) else self.smoothedgas=gas end self.vehicle:SetAcceleration(self.smoothedgas) self.vehicle:SetGear(gear) end end ---------------------------------------------- ---------------------------------------------- if self.vehicle:GetEngineRunning() then if self.source.engineloop~=nil then if self.source.engineloop:GetState()==Source.Stopped then if self.source.enginestart~=nil then if self.source.enginestart:GetTime()>1 then self.source.engineloop:Play() end else self.source.engineloop:Play() end end local pitch = self.vehicle:GetRPM()/2500.0+0.5 if self.smoothedpitch==nil then self.smoothedpitch=pitch end self.smoothedpitch = Math:Curve(pitch,self.smoothedpitch,10) self.source.engineloop:SetPitch(self.smoothedpitch) end end end function Script:UpdateWorld() self:UpdateCamera(10) end function Script:PostRender(context) context:SetBlendMode(Blend.Alpha) --context:DrawText(tostring(self.vehicle:GetGear()),0,140) context:DrawText(tostring(self.entity:GetVelocity():Length() * 2.23694),0,160) --context:DrawText(tostring(self.steerangle),0,180) context:DrawText(tostring(self.latforce),0,200) end function Script:Draw() local index local child local scale for index,child in ipairs(self.tires) do scale = child:GetScale() child:SetMatrix(self.vehicle:GetTireMatrix(index-1),true) child:SetScale(scale) end end function Script:Detach() ReleaseTableObjects(self.source) ReleaseTableObjects(self.sound) camera:Release() end 6 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
nick.ace Posted November 13, 2014 Share Posted November 13, 2014 I started playing with some of the vehicle physics, but the vehicle seems to glitch out a lot and I think I'm doing something wrong maybe. Is there a way to see where the tires are located (like a command similar to SetDebugPhysicsMode)? Quote Link to comment Share on other sites More sharing options...
Jazz Posted November 17, 2014 Share Posted November 17, 2014 Finally got it to work for me. Script never could find all the tires so I modified it very slightly. Changed code is in the section below. Sorry about the formatting, I just can't get it to display properly. --Build the vehicle self.vehicle = Vehicle:Create(self.entity) for n=1,8, 2 do child[0] = self.entity:FindChild("tire"..tostring(n)) child[1] = self.entity:FindChild("tire"..tostring(n+1)) if child[0]~=nil and child[1]~=nil then for i=0,1 do box = self:CalculateTireSize(child[i]) if box~=nil then tireradius = math.max(box.size.y,box.size.z)*0.5 tirewidth = box.size.x tirepos = box.center + child[i]:GetPosition(false) self.vehicle:AddTire(tirepos.x,tirepos.y,tirepos.z,self.tiremass,tireradius,tirewidth,n<2) table.insert(self.tires,child[i]) end end if n>0 then self.vehicle:AddAxle(self.vehicle:CountTires()-2,self.vehicle:CountTires()-1) end end end I used the pickup truck body prefab and added the tires manually, setting the truck's tire children like the attached image... Hope this helps someone Edit: forgot to mention the tires must be named tire1, tire2, etc. 3 Quote --- Scott Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060 Link to comment Share on other sites More sharing options...
shadmar Posted November 21, 2014 Author Share Posted November 21, 2014 oh you did nice, I get a crash every time (exe stopped working). Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Jazz Posted November 22, 2014 Share Posted November 22, 2014 Truck and Tires are rigid body/prop. Certain settings will crash it. Still trying to figure it out. Last vehicle I made the same way but forward/reverse work opposite of what they should. 1 Quote --- Scott Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060 Link to comment Share on other sites More sharing options...
shadmar Posted November 22, 2014 Author Share Posted November 22, 2014 Ok not crashing anymore, but kind of "takes off" into oblivion Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
shadmar Posted November 22, 2014 Author Share Posted November 22, 2014 Got it, dont give wheels a collision shape. Only the car. The wheels must named tire0,1,2,3 using the original script. 0,1 being the steering wheels. Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
shadmar Posted November 22, 2014 Author Share Posted November 22, 2014 Working car prefab in the WS : http://steamcommunity.com/sharedfiles/filedetails/?id=345315752 if someone wants to test/play 2 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Olby Posted November 22, 2014 Share Posted November 22, 2014 Working car prefab in the WS : http://steamcommunity.com/sharedfiles/filedetails/?id=345315752 if someone wants to test/play This is awesome... Now watch teens flocking in saying they're building the next GTA but cant get their models loaded in. 2 Quote Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64) Link to comment Share on other sites More sharing options...
beo6 Posted November 24, 2014 Share Posted November 24, 2014 It works nicely in the prefab. But when I use another model it just crashes when I start just like all the time when I tried to get car physics running. I am now thinking it is the model that makes the problem but not sure why. Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted November 24, 2014 Share Posted November 24, 2014 I got the script to work with the Monster truck and tyres model. I just copied all the settings from Shadmars model onto the monster truck. Works a treat. Quote Link to comment Share on other sites More sharing options...
shadmar Posted November 24, 2014 Author Share Posted November 24, 2014 Only the car (wihout wheels) needs a collsion box (prop), wheels must not have any collision, and you should be ok Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
gamecreator Posted November 24, 2014 Share Posted November 24, 2014 Any chance someone can make a simple C version available? Quote Link to comment Share on other sites More sharing options...
shadmar Posted November 24, 2014 Author Share Posted November 24, 2014 Yeah no problem car_and_camera = Prefab::Load("AddOns/CarDemo prefab (requires addon Vehicles installed)/cardemo.pfb"); 1 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
gamecreator Posted November 24, 2014 Share Posted November 24, 2014 Thanks. Not familiar with prefabs but I'll try it. How would you change something like tire friction? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.