• Continuous upwards force on an entity
    22 replies, posted
Im trying to make a prop that will pull upwards with a certain amount of force. It will eventually be for making airships. My problem right now is that it will shoot up for about a second when it spawns, and then fall back and just sit there. The wiki says that phys.SetVelocity is supposed to be infinite. Here's my init.lua: [code] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function ENT:Initialize() self.Entity:SetModel( "models/props_wasteland/cargo_container01.mdl" ) self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end phys:SetMass( 1000 ) phys:SetVelocity(Vector(0,0,5000)) end [/code]
[lua]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function ENT:Initialize() self.Entity:SetModel( "models/props_wasteland/cargo_container01.mdl" ) self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end phys:SetMass( 1000 ) hook.Add("Tick" , self:EntIndex().."PushUp" , function() phys:ApplyForceOffset(Vector(0, 0, 30), Vector(0, 0, 0) ) end ) end [/lua] Try that. The problem is you were only setting the velocity once - which will mean that it will fall back down to earth. If you constantly apply a force (Could use the limit formula to find out the perfect force to keep it hovering) then it will stay in the air. The tick hook is called on each server tick - which is normally 66 or 100 times per second.
When I copy-pasted that, it made it roll forever. So, partial success. I figured you just put the force in the wrong place, but when I tried to fix it, it didn't work at all.
You just need to apply more upward force for such a big thing.
You could use phys:SetVelocity rather than phys:ApplyForceOffset. I just used it because it gives a more realistic result :3
[QUOTE=Entoros;17994985]You just need to apply more upward force for such a big thing.[/QUOTE] More force than 5000? I guess I'll try it, but I doubt that's my problem.
Why the unnecessary hooks, sir? [lua]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function ENT:Initialize() self.Entity:SetModel( "models/props_wasteland/cargo_container01.mdl" ) self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end phys:SetMass( 1000 ) end local function ForceUp() for k, v in pairs (ents.FindByClass(ENT_CLASS_NAME_HERE)) do local phys = v:GetPhysicsObject() if phys:IsValid() then phys:ApplyForceCenter(Vector(0, 0, 1000) ) end end end hook.Add("Tick", "ENT_CLASS_NAME_HERE_PushUp", ForceUp) [/lua]
Have you tried [url=http://wiki.garrysmod.com/?title=PhysObj.ComputeShadowControl]PhysObj.ComputeShadowControl[/url]? It's really good at controlling movement and make things hover smoothly. Don't let the function scare you it's actually really easy to use. :smile:
Problem with the OP's code snippet is that you're putting the velocity in initialize. It sets the velocity upwards once, and leaves it free to accelerate how it likes after that. It was effectively just launched up in the air like from a giant spring. You need to put the code for setting velocity or applying force in think, which runs 10 times a second by default, if you want the force to be constant.
[QUOTE=MegaJohnny;18028309]or applying force in think, which runs 10 times a second by default, if you want the force to be constant.[/QUOTE] Think runs every frame.
[url]http://wiki.garrysmod.com/?title=ENT.PhysicsUpdate[/url] Is also useful. Funny that I got rated late even though no one has posted this.
[QUOTE=Morcam;18035653][url]http://wiki.garrysmod.com/?title=ENT.PhysicsUpdate[/url] Is also useful.[/QUOTE] This actually looks like the best option. If the OP is still checking in on this thread, I'd use this. Put a line in it like phys:SetVelocity(0,0,1000) or something.
[QUOTE=Skyhawk;18050823]This actually looks like the best option. If the OP is still checking in on this thread, I'd use this. Put a line in it like phys:SetVelocity(0,0,1000) or something.[/QUOTE] That's exactly what I suggested. :frown: But yeah definitely the best place in which to mess with an entity's physics.
physics update works. I'm tweaking the settings now, but I have proof of concept. Thanks all.
-snip- Number is wrong, didn't include deltatime.
[QUOTE=Morcam;19113250]And if you're curious, the magic number for weightlessness seems to be about 9.015 units of force to each unit of mass. In the vertical direction, of course.[/QUOTE] What seriously? Is that some sort of gravitational constant equivalent in the Source Engine?
[QUOTE=andrewmcwatters;19115127]What seriously? Is that some sort of gravitational constant equivalent in the Source Engine?[/QUOTE] For a 600 in/sec gravity, yeah. Don't ask me why that's what it is, I haven't the slightest clue. it isn't F=ma, for some odd reason.
Objects in source don't fall at a uniform rate. I'm not sure if it's highly exacerbated air friction or just force based on mass.
Constant is ( sv_gravity:GetInt( ) / 600 ) * ( sv_gravity:GetInt( ) / -16 ) Or -37.5. That's the gravity constant method I use to calculate the needed angle for object x to travel y distance that weighs z. Works like a charm, too, so I think that number mentioned is just a lucky magic number.
[QUOTE=Kogitsune;19115257]Constant is ( sv_gravity:GetInt( ) / 600 ) * ( sv_gravity:GetInt( ) / -16 ) Or -37.5. That's the gravity constant method I use to calculate the needed angle for object x to travel y distance that weighs z. Works like a charm, too, so I think that number mentioned is just a lucky magic number.[/QUOTE] Whoa, sorry, I completely forgot about my deltatime. My number is off, sorry. Although where did you get your -16 from? I was just applying that number (about 9) times weight in an object's PhysicsUpdate to get weightlessness. Drag is off, and model doesn't matter.
I might be explaining this wrong so bear with me. Source units are in sixteenths of a foot ( 16 units = 12 inches ). So I divide the gravity by 16 to find out the feet per second of downwards force, and negate the number for calculations sake, given that negative z coordinates are lower in the map than positive ones.
[QUOTE=Kogitsune;19115879]I might be explaining this wrong so bear with me. Source units are in sixteenths of a foot ( 16 units = 12 inches ). So I divide the gravity by 16 to find out the feet per second of downwards force, and negate the number for calculations sake, given that negative z coordinates are lower in the map than positive ones.[/QUOTE] Ah, dammit. I've been wrong all along. Well, right in my specific case, but wrong in general. I forgot that it was 16units = 12inches ingame. So it is Force = Mass * Acceleration, just not in the units I was using. Good to know, thanks.
It really should be gravity / 600 * -37.5, though, since changing the gravity will hose the number up pretty bad ( at 300, the constant would be a quarter of it's value rather than the half it should be ). Didn't notice that at the time and haven't gone back to fix it yet.
Sorry, you need to Log In to post a reply to this thread.