In front angle prediction of LUA scripted vehicle.

Hello, I am currently developing vehicles that are defined with LUA. ( In example, like SCars )

I currently have the file structure of veh_base and then creating sub entities off the base.

This is a file of a sub entities.

[lua]
AddCSLuaFile()

ENT.Type = “anim”
ENT.PrintName = “Vehicle Bus”
ENT.Base = “veh_base”
ENT.Author = “RayChamp”
ENT.Spawnable = true

ENT.wheelupdate = true

ENT.Model = “models/jessev92/stalker/bus_chassis.mdl”

ENT.WheelHeight = 13
ENT.TurnSpeed = 3

ENT.MaxSpeed = 40
ENT.MaxBackupSpeed = 20

ENT.Acceleration = .1
ENT.Life = 1500

ENT.TireDirection = “Up”

ENT.DriverOffset = Vector(-12,-2,-9.5)

ENT.Seats = {
{
Model = “models/nova/airboat_seat.mdl”,
Pos = Vector( 60, 35, -10 ),
Ang = Angle( 0, -90, 0 ),
Driver = true,
NoDraw = true,
},
{
Model = “models/nova/airboat_seat.mdl”,
Pos = Vector( -25, 30, -10 ),
Ang = Angle( 0, -90, 0 ),
NoDraw = true,
},
{
Model = “models/nova/airboat_seat.mdl”,
Pos = Vector( -25, -30, -10 ),
Ang = Angle( 0, -90, 0 ),
NoDraw = true,
},
{
Model = “models/nova/airboat_seat.mdl”,
Pos = Vector( -70, -30, -10 ),
Ang = Angle( 0, -90, 0 ),
NoDraw = true,
}
}

ENT.Wheels = {
{
Model = “models/props_phx/wheels/trucktire.mdl”,
Pos = Vector( 125, -35, -50 ),
Ang = Angle( 0, 180, -90 ),
Flip = true,
Front = true,
},
{
Model = “models/props_phx/wheels/trucktire.mdl”,
Pos = Vector( 125, 35, -50 ),
Ang = Angle( 0, 180, 90 ),
Front = true,
},
{
Model = “models/props_phx/wheels/trucktire.mdl”,
Pos = Vector( -65, 40, -55 ),
Ang = Angle( 0, 180, 90 ),
},
{
Model = “models/props_phx/wheels/trucktire.mdl”,
Pos = Vector( -65, -40, -55 ),
Ang = Angle( 0, 0, 90 ),
Flip = true
}
}
[/lua]

I’m working on aligning the vehicles angle based off the ground it’s on.

Here is the code

[lua]
function ENT:GetGroundTrace()
local pos = self:GetPos()

local tr = util.TraceHull({
	start = pos,
	endpos = pos - Vector( 0, 0, 999999999 ),
	filter = function( ent ) return !( ent == self or ent:GetParent() == self ) end,
	mins = self:OBBMins(),
	maxs = self:OBBMaxs(),
})

return tr

end

function ENT:GetVehicleAngle()
local ang = self:GetAngles()

local ground = self.tr.HitNormal:Angle() // self.tr = self:GetGroundTrace()

    // this is just for simulating the tilting effect as if there was gravity.
if ( ang.r >= 45 ) then
	ang.r = 90
elseif ( ang.r <= -45 ) then
	ang.r = -90
else
	ang.r = 0
end

local dif = self:GetBackAndFrontZDif() // this was my attempt to compare the front position of the vehicle and the bottom position Z and then try to turn that into a angle and use that offset - couldn't get it to work correctly.

ang.p = math.ceil( ground.p - 270 ) * -1

--[[if ( dif > 50 ) then
	ang.p = ang.p * -1 // it would flip the pitch, that seems to be the issue
end]]

local turnLeft = nil
local turnRight = nil

if ( IsValid( self.Driver ) ) then
	turnLeft = self.Driver:KeyDown( IN_MOVELEFT )
	turnRight = self.Driver:KeyDown( IN_MOVERIGHT )

	local calc = ( self.Speed / self.MaxSpeed )
	
	if ( calc == 0 ) then
		calc = -( self.BackupSpeed / self.MaxBackupSpeed )
	end
	
	local amt = self.TurnSpeed * calc

	if ( turnLeft ) then
		ang:RotateAroundAxis( ang:Up(), amt )
	elseif ( turnRight ) then
		ang:RotateAroundAxis( ang:Up(), -amt )
	end
end

ang.p = math.floor( ang.p )
ang.y = math.floor( ang.y )
ang.r = math.floor( ang.r )


return ang

end

// this function is used with self:GetPhysicsObject():UpdateShadow( pos, self:GetVehicleAngle, .05 )
[/lua]

Now it does the job kinda, the problem is when going down a hill, the vehicle will tilt up like it’s doing a wheelie. Basically the angle being applied is doing opposite of what it’s supposed to be doing.

http://puu.sh/cWE9U/989d773f63.jpg

Displaying on this picture, that white box is displaying the proper angle of the surface angle that’s under the vehicle, the vehicle is applied that exact angle ( They use the same traces )

I need some sort of idea to offset the angle if it’s going down a hill, or perhaps there is a much better way to go about this.

This is a pretty advanced problem, the average scripter will probably have no idea, what makes you different?

I appreciate all help on this issue, thank you.

It looks like you have an angle clientside already, judging by the existence of a white cube rotated to the vehicle’s proper angle.

Debug the angles of your white box and the angles of the vehicle itself. If the pitch of the vehicle is in fact the negative pitch of your white box, then make the necessary adjustments in your GetVehicleAngle method.

Or am I not seeing the issue correctly?

The issue is dynamic unfortunately, going up a hill, there are no issues with that, it’s only when going down a hill. I would have to figure out how to determine if the vehicle is going down and predict it correctly.

Is there a way to find the slope of the surface area, like world slope ?

Tracers return a directional vector of the surface they hit, tr.HitNormal. You can simply convert that into an angle to find the slope of any part of the maps surface.

Edit:
I am blind.

That is what he is doing. The issue is that his angle is not rotating with the vehicle, so the front end always goes up even when his vehicle is going down.

Do some debug and see what the difference between the white box’s angle and the entity’s angle are. If they are the same, then something is really out of whack. They should be different, or rather, they shouldn’t.

If they are different, you need to make them the same. If they are the same, something is really wrong.

I wouldn’t use direction to change the angle. I’d look for the contact points ( wheels ) and get the ground underneath. Using the 4 points where the ground is, you can create a plane for visual reference, but you’d also have the pitch and roll.

If you want to try by using direction, then here’s a simple way to get the direction that something is traveling in: https://bitbucket.org/Acecool/acecooldev_base/src/master/gamemode/shared/entity/entity_getmovedirection.lua

The angles are not based off direction, it does use the “ground underneath” A useful function would be to tell me if the ground is going down or up.

If you take the forward wheel and rear wheel, you can calculate the pitch. Then, take the front left, and front right to calculate roll. You may want to compare right with left and front with rear for those axis.

Hey RayChamp can you message me or add me on steam? http://steamcommunity.com/id/bobblackmon/
I have a question, and it seems like you know how to solve it.

It’s difficult to create your own physics for cars, believe me lol

You need to think with math.

To be quiet honest, I’ve gotten alot farther then I thought I could, i’m not looking to create a ultra realistic vehicle, just flexible and resource safe.

Because SCars performs horribly in multiplayer due to the amount of entities for one vehicle.

Agreed. Every entity has a network sync to each client involving position, angle, etc…