basic physics

This commit is contained in:
Nishi 2026-02-01 00:38:33 +09:00
commit 7193bcbc41
Signed by: nishi
GPG key ID: 27EF69B208EB9343
2 changed files with 38 additions and 4 deletions

View file

@ -318,6 +318,7 @@ struct _GSPhysics {
dWorldID world; dWorldID world;
dSpaceID space; dSpaceID space;
dJointGroupID contact_group;
}; };
struct _GSClient { struct _GSClient {

View file

@ -17,6 +17,9 @@ GSPhysics GSPhysicsCreate(GSServer server) {
physics->world = dWorldCreate(); physics->world = dWorldCreate();
physics->space = dSimpleSpaceCreate(0); physics->space = dSimpleSpaceCreate(0);
physics->contact_group = dJointGroupCreate(0);
dWorldSetGravity(physics->world, 0, 0, -9.81);
GSLog(server->engine, GSLogInfo, "Created physics engine"); GSLog(server->engine, GSLogInfo, "Created physics engine");
@ -24,6 +27,7 @@ GSPhysics GSPhysicsCreate(GSServer server) {
} }
void GSPhysicsDestroy(GSPhysics physics) { void GSPhysicsDestroy(GSPhysics physics) {
dJointGroupDestroy(physics->contact_group);
dSpaceDestroy(physics->space); dSpaceDestroy(physics->space);
dWorldDestroy(physics->world); dWorldDestroy(physics->world);
@ -32,5 +36,34 @@ void GSPhysicsDestroy(GSPhysics physics) {
free(physics); free(physics);
} }
void GSPhysicsStep(GSPhysics physics) { static void near_callback(void* data, dGeomID o1, dGeomID o2) {
const int N = 256;
dContact contact[256];
GSPhysics physics = data;
if(dGeomIsSpace(o1) || dGeomIsSpace(o2)) {
dSpaceCollide2(o1, o2, data, near_callback);
if(dGeomIsSpace(o1)) dSpaceCollide((dSpaceID)o1, data, near_callback);
if(dGeomIsSpace(o2)) dSpaceCollide((dSpaceID)o2, data, near_callback);
} else {
int how_many = dCollide(o1, o2, N, &contact[0].geom, sizeof(dContact));
int i;
for(i = 0; i < how_many; i++) {
dJointID joint;
contact[i].surface.mode = dContactApprox1;
contact[i].surface.mu = 2;
joint = dJointCreateContact(physics->world, physics->contact_group, &contact[i]);
dJointAttach(joint, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
}
}
}
void GSPhysicsStep(GSPhysics physics) {
dSpaceCollide(physics->space, physics, near_callback);
dWorldStep(physics->world, 1.0 / physics->engine->tps);
dJointGroupEmpty(physics->contact_group);
} }