Jump to content

Physics fixed update function?


Andy Gilbert
 Share

Recommended Posts

Hi, Im wanting to put all my physics calculations into a physics fixed update function based on the callback that the document present im a but unsure how i do this as it has a parameter for an entity?

 

Can i not have a single function that does all the physics routines every physics update?

 

Thanks

Andy

The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do.

 

Leadwerks Game-Ready 3D Models:https://sellfy.com/gib3d

Link to comment
Share on other sites

I don't think there's a global physics update callback. the Entity callback is your best bet. This is called during UpdateWorld(). Assign it to every entity then branch off for specific types in a single function. But you probably did this already.

 

' INITIALISING ENTITYS IN A LOOP, SET THE PHYSICS CALLBACK '
...
SetEntityCallback(newEntity, UpdatePhysicsCallback, ENTITYCALLBACK_UPDATEPHYSICS) ;
...
...
' CALLED 60 FPS REGARDLESS OF FRAMERATE '
Function UpdatePhysicsCallback(entity:TEntity)
  if (entity <> Null)
     if (game.LocalPlayer.entity = entity)
        UpdateLocalPlayerPhysics(entity) ;
     Else
        UpdateRemoteEntityPhysics(entity);
     End If
  End If
End Function

 

If you have to do this for lots of entities, but have many of a specific type, it would make sense to use caching to your advantage by having specific callback functions for them.

6600 2.4G / GTX 460 280.26 / 4GB Windows 7

Author: GROME Terrain Modeling for Unity, UDK, Ogre3D from PackT

Tricubic Studios Ltd. ~ Combat Helo

Link to comment
Share on other sites

To build off of what Flexman has shown, you could fake 1 update call by having an EntityManager class that stores an entity and a list of entities. Whenever you make an entity you add it to this list. Then you only register the callback to the 1 entity in this class. When that entity update gets called, you cycle through the list of all other entities.

 

class EntityManager
{
private:
TEntity _entity;
list<TEntity> _entities;
public:
EntityManager()
{
 SetEntityCallback(_entity, UpdatePhysicsCallback, ENTITYCALLBACK_UPDATEPHYSICS);
 SetEntityUserData(_entity, (byte*)this);
}
list<TEntity>& GetEntities() { return _entities; }
void AddEntity(TEntity e) { _entities.push_back(e); }
};

void _stdcall UpdatePhysicsCallback(TEntity ent)
{
EntityManager* mgr = (EntityManager*)GetEntityUserData(ent);
// now you can call mgr->GetEntities() and loop over them all to process or maybe just call a function in the EntityManager class that will loop so that yo don't have to expose the list of entities to anything else.
}

  • Upvote 1
Link to comment
Share on other sites

No clue, but in LE Lua a model with a script attached to it that is structured in the template that is in the Script folder has a physics update function callback in it. If you just have the 1 model then attached this script to it, put the script in the same dir and name it the same as the model, and then those functions will be automatically called.

Link to comment
Share on other sites

You can use the UpdatePhysics hook:


require("Scripts/constants/keycodes") 
require("Scripts/console") 

local bonks=0
local frames=0

function MyPhysicsHook()
bonks=bonks+1
AddConsoleText("PhysicsUpdates: "..bonks.."    Frame: "..frames)
end

--Register abstract path 
RegisterAbstractPath("") 

--Set graphics mode 
if Graphics(1024,768)==0 then 
Notify("Failed to set graphics mode.",1) 
return 
end 

world=CreateWorld() 
if world==nil then 
Notify("Failed to initialize engine.",1)   return 
end 

fw=CreateFramework()

camera=fw.main.camera
camera:SetPosition(Vec3(0,0,-2)) 

light=CreateSpotLight(10) 
light:SetRotation(Vec3(45,55,0)) 
light:SetPosition(Vec3(5,5,-5)) 

model=LoadModel("abstract::oildrum.gmf") 

ground=CreateBodyBox(10,1,10)
groundmesh=CreateCube(ground)
groundmesh:SetScale(Vec3(10.0,1.0,10.0))
ground:SetPosition(Vec3(0.0,-2.0,0.0)) 
groundmesh:Paint(LoadMaterial("abstract::cobblestones.mat"))
ground:SetCollisionType(1)

Collisions(1,1,1)
--DebugPhysics(1)
SetStats(0)

light=CreateDirectionalLight() 
light:SetRotation(Vec3(45,45,45)) 

local timer=AppTime()+1000
consolemode=1
AddHook("UpdatePhysics",MyPhysicsHook)
while AppTerminate()==0 do 

if KeyHit(KEY_ESCAPE)==1 then
break
end 

if timer<AppTime() then
timer=AppTime()+1000;
model=CopyEntity(model)
model:SetPosition(Vec3(0,10,0))
end

fw:Update()
fw:Render()

frames=frames+1

DrawText("FPS: "..UPS(),300,0)
Flip(0)

end

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...