Having a problem with ENT:PhysicsSimulate().

Alright so to start off I am trying to code a skull pet with special abilities and so on. And I’m currently working on a way to make it Follow the User. I already got it flying using the PhysicsSimulate() Function and it works pretty well. But now I need it to face the user and stop turning. I would like to use the angular velocity output that comes with the Physics Simulate function. Now my problem is that I can’t really think of a way to make it work. It’d be great if someone could help or explain it to me.

Thanks in advance



a go. It’ll be a lot easier to make work than doing it yourself, unless your skull has more than 1 prop.

No it is just one prop. But the pet will have different abilities and those I would have to handle with PhysicsSimulate. I would really like a way to make it face a specific points with PhysicsSimulate.

Best example for PhysicsSimulate is

Hm… I understood the principe of the function already. Only thing I don’t understand how to set the angular velocity of my ent to face exactly one point.

Then look at the example…

Well there is no example. at least none of what I am trying to do.

Of-course it isn’t going to do exactly what you want but it shows you a practical example of how to use the angular_force callback

Yeah. That it does. But what shall I do if I have no clue how to start? My problem that I can’t think of a way to do it…

That’s why you look at the example and if you can’t work it out after that then you clearly don’t understand how it works

Then I have to admit that I clearly don’t understand it. That’s why I’m asking for help. I need someone to explain me how I would calculate an angular force that makes my entity face a specific coordinate constantly.

newAngVel = (targetAngle-curAngle)*speed-oldAngVel*damping

Pretty straight forward way, there’s most likely more advanced solutions but this will do for starters.

Calculate the difference between the angle you want and the angle you are at currently. then shift the values left one and apply that force. kinda like this
example code

local targetAngle = Angle(0,90,0)
local currentAngle = Angle(60,45,92)
local tempAngle = Angle(0,0,0)
local angleVelocity = Angle(0,0,0)

tempAngle.p = math.AngleDifference(targetAngle.p, currentAngle.p)
tempAngle.y = math.AngleDifference(targetAngle.y, currentAngle.y)
tempAngle.r = math.AngleDifference(targetAngle.r, currentAngle.r)

angleVelocity.p = tempAngle.y
angleVelocity.y = tempAngle.r
angleVelocity.r = tempAngle.p
– now you should be able to use angleVelocity to turn your entity to where you want.

I do this in my Float Props tool, there may be an easier way but this worked for me. And it has nothing to do with understanding the PhysicsSimulate function, it’s about angle math, which i suck at and got this through a lot of trial and error. Also this page Physics on the wiki has a lot of the equations for things like this.

So I just have to put the angleVelocity as return value for the function. Or am I mistaken?

well, it’s an example of how to calculate the need value, I use AddAngleVelocity on the physics object I haven’t played with PhysicsSimulate much, but if it wants an angle velocity as a return then try it.

My prop is spinning like hell but not stopping at the right angle.

sorry, forgot to mention you’ll need to scale the angeVelocity by deltatime in the funciton, otherwise you are apply all the force needed to get to the right position every time it’s called, you only need to move it a little each time physics simulate is called, that’s what the deltatime argument is for

How about this?


function ENT:PhysicsSimulate( phys, deltatime )

// Object Position
local Pos			=	phys:GetPos()

// Owner Vars
local Owner			=	self:GetMaster()
local OwnerPos		=	Owner:GetPos()

// Return Values
local Linear 		=	Vector(0,0,0)
local Angular 		=	Vector(0,0,0)

// Height Tracer (traces down to _ground_) 
local tracedata = {}
tracedata.start		=	Pos
tracedata.endpos	=	Vector(Pos.x, Pos.y, Pos.z - 100)
tracedata.filter	=	{ self.Entity }
tracedata.mask		=	MASK_SOLID | MASK_WATER
local tr 			=	util.TraceLine( tracedata )

// Calculation Values
local Vel			=	phys:GetVelocity()
local AirResistance =	1

// Distance
// self:GetIdleHeight() + tr.HitPos.z - Pos.z
local Distance		=	0

if ( self:GetActionState() == 0) then
	Distance	=	self:GetIdleHeight() + tr.HitPos.z - Pos.z
elseif ( self:GetActionState() == 1 ) then
	//Distance	=	

if ( Distance == 0 ) then return end

// Calculate Z-Force
local zExponent = Distance^2

if ( Distance < 0 ) then
	zExponent = zExponent * -1

zExponent = zExponent * deltatime * 300

local zVel = Vel.z

zExponent = zExponent - (zVel * deltatime * 600 * ( AirResistance + 1 ) )
zExponent = math.Clamp( zExponent, -5000, 5000 )

Linear.z = zExponent

// Add Air Resitance to Object
if ( AirResistance > 0 ) then
	Linear.y = Vel.y * -1 * AirResistance
	Linear.x = Vel.x * -1 * AirResistance

return Angular, Linear, SIM_GLOBAL_ACCELERATION


Alright so this is what I’m using right now. I got some of it from the Hoverball Code and some of it from my own. This so far makes the entity hover exactly 75 units above the ground. But it seemed nothing you guys showed me works with the angular return value.

Have you tried SIM_LOCAL_ACCELERATION or the other SIM_ 's ?