Skip to content

Commit

Permalink
Merge pull request #9 from Hjaltesorgenfrei/hjda/add-angular-velocity
Browse files Browse the repository at this point in the history
add angular velocity
  • Loading branch information
Hjaltesorgenfrei authored Jan 26, 2024
2 parents 3d762bf + f3193e2 commit 9a47e78
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/client/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ entt::entity App::addCubePlayer(T input) {
registry.emplace<Transform>(entity);
registry.emplace<PlayerCube>(entity);
auto body = physicsWorld->addBox(registry, entity, spawnPoint.position, glm::vec3(0.5f, 0.5f, 0.5f));
// physicsWorld->setBodyScale(body, glm::vec3(5.f, 5.f, 5.f));
// physicsWorld->setScale(body, glm::vec3(5.f, 5.f, 5.f));
registry.emplace<std::shared_ptr<RenderObject>>(entity, std::make_shared<RenderObject>(meshes["cube"], noMaterial));
return entity;
}
Expand Down Expand Up @@ -565,12 +565,12 @@ void App::drawDebugForSelectedEntity(entt::entity selectedEntity, FrameInfo& fra
glm::mat4 delta(1.0f);

if (currentGizmoOperation == ImGuizmo::TRANSLATE && drawImGuizmo(&bodyTransform, &delta)) {
physicsWorld->setBodyPosition(body->bodyID, delta * glm::vec4(body->position, 1.f));
physicsWorld->setPosition(body->bodyID, delta * glm::vec4(body->position, 1.f));
} else if (currentGizmoOperation == ImGuizmo::ROTATE && drawImGuizmo(&bodyTransform, &delta)) {
physicsWorld->setBodyRotation(body->bodyID, glm::toQuat(delta) * body->rotation);
physicsWorld->setRotation(body->bodyID, glm::toQuat(delta) * body->rotation);
} else if (currentGizmoOperation == ImGuizmo::SCALE &&
drawImGuizmo(&bodyTransform, &delta)) { // TODO: Broken and crashes.
physicsWorld->setBodyScale(body->bodyID, delta * glm::vec4(body->scale, 1.f));
physicsWorld->setScale(body->bodyID, delta * glm::vec4(body->scale, 1.f));
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/engine/NetworkServerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ void NetworkServerSystem::update(entt::registry &registry, PhysicsWorld *world,
message->rotation.z = body.rotation.z;
message->rotation.w = body.rotation.w;

message->velocity.x = body.velocity.x;
message->velocity.y = body.velocity.y;
message->velocity.z = body.velocity.z;
message->linearVelocity.x = body.linearVelocity.x;
message->linearVelocity.y = body.linearVelocity.y;
message->linearVelocity.z = body.linearVelocity.z;

message->angularVelocity.x = body.angularVelocity.x;
message->angularVelocity.y = body.angularVelocity.y;
message->angularVelocity.z = body.angularVelocity.z;
server->SendMessage(clientId, 0, message);
}

Expand Down
70 changes: 39 additions & 31 deletions src/engine/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ void PhysicsWorld::addMesh(entt::registry &registry, entt::entity entity, std::v

setUserData(mesh_id, entity);
bodies.push_back(mesh_id);
setBodyScale(mesh_id, scale);
setScale(mesh_id, scale);
addBody(registry, entity, mesh_id);
}

Expand Down Expand Up @@ -649,28 +649,26 @@ void PhysicsWorld::removeBody(IDType bodyID) {

PhysicsBody PhysicsWorld::getBody(IDType bodyID) {
return {
bodyID,
getMotionType(bodyID),
getBodyPosition(bodyID),
getBodyRotation(bodyID),
getBodyScale(bodyID),
getBodyVelocity(bodyID),
bodyID, getMotionType(bodyID), getPosition(bodyID), getRotation(bodyID),
getScale(bodyID), getLinearVelocity(bodyID), getAngularVelocity(bodyID),
};
}

void PhysicsWorld::getBody(IDType bodyID, PhysicsBody &body) {
body.bodyID = bodyID;
body.position = getBodyPosition(bodyID);
body.rotation = getBodyRotation(bodyID);
body.scale = getBodyScale(bodyID);
body.velocity = getBodyVelocity(bodyID);
body.position = getPosition(bodyID);
body.rotation = getRotation(bodyID);
body.scale = getScale(bodyID);
body.linearVelocity = getLinearVelocity(bodyID);
body.angularVelocity = getAngularVelocity(bodyID);
}

void PhysicsWorld::updateBody(IDType bodyID, PhysicsBody body) {
setBodyPosition(bodyID, body.position);
setBodyRotation(bodyID, body.rotation);
setBodyScale(bodyID, body.scale);
setBodyVelocity(bodyID, body.velocity);
setPosition(bodyID, body.position);
setRotation(bodyID, body.rotation);
setScale(bodyID, body.scale);
setLinearVelocity(bodyID, body.linearVelocity);
setAngularVelocity(bodyID, body.linearVelocity);
}

MotionType PhysicsWorld::getMotionType(IDType bodyID) {
Expand All @@ -687,17 +685,17 @@ MotionType PhysicsWorld::getMotionType(IDType bodyID) {
}
}

glm::vec3 PhysicsWorld::getBodyPosition(IDType bodyID) {
glm::vec3 PhysicsWorld::getPosition(IDType bodyID) {
RVec3 position = bodyInterface->GetCenterOfMassPosition(bodyID);
return glm::vec3(position.GetX(), position.GetY(), position.GetZ());
}

glm::quat PhysicsWorld::getBodyRotation(IDType bodyID) {
glm::quat PhysicsWorld::getRotation(IDType bodyID) {
Quat quat = bodyInterface->GetRotation(bodyID);
return glm::quat(quat.GetW(), quat.GetX(), quat.GetY(), quat.GetZ());
}

glm::vec3 PhysicsWorld::getBodyScale(IDType bodyID) {
glm::vec3 PhysicsWorld::getScale(IDType bodyID) {
const Shape *shape = bodyInterface->GetShape(bodyID);
if (shape->GetSubType() == EShapeSubType::Scaled) {
// Might not always be valid, as the more decorations can be on the shape
Expand All @@ -709,21 +707,26 @@ glm::vec3 PhysicsWorld::getBodyScale(IDType bodyID) {
return glm::vec3(1, 1, 1);
}

glm::vec3 PhysicsWorld::getBodyVelocity(IDType bodyID) {
glm::vec3 PhysicsWorld::getLinearVelocity(IDType bodyID) {
Vec3 velocity = bodyInterface->GetLinearVelocity(bodyID);
return glm::vec3(velocity.GetX(), velocity.GetY(), velocity.GetZ());
}

void PhysicsWorld::setBodyPosition(IDType bodyID, glm::vec3 position) {
glm::vec3 PhysicsWorld::getAngularVelocity(IDType bodyID) {
Vec3 velocity = bodyInterface->GetAngularVelocity(bodyID);
return glm::vec3(velocity.GetX(), velocity.GetY(), velocity.GetZ());
}

void PhysicsWorld::setPosition(IDType bodyID, glm::vec3 position) {
// TODO: Maybe bool for if it should activate?
bodyInterface->SetPosition(bodyID, RVec3(position.x, position.y, position.z), EActivation::Activate);
}

void PhysicsWorld::setBodyRotation(IDType bodyID, glm::quat rotation) {
bodyInterface->SetRotation(bodyID, Quat(rotation.x, rotation.y, rotation.z, rotation.w), EActivation::Activate);
void PhysicsWorld::setRotation(IDType bodyID, glm::quat rotation) {
bodyInterface->SetRotation(bodyID, Quat(rotation.w, rotation.x, rotation.y, rotation.z), EActivation::Activate);
}

void PhysicsWorld::setBodyScale(IDType bodyID, glm::vec3 scale) {
void PhysicsWorld::setScale(IDType bodyID, glm::vec3 scale) {
auto &body_interface = physicsSystem->GetBodyLockInterfaceNoLock();
BodyLockWrite lock(body_interface, bodyID);
auto result = lock.GetBody().GetShape()->ScaleShape(Vec3Arg(scale.x, scale.y, scale.z));
Expand All @@ -733,10 +736,14 @@ void PhysicsWorld::setBodyScale(IDType bodyID, glm::vec3 scale) {
bodyInterface->SetShape(bodyID, result.Get(), false, EActivation::Activate);
}

void PhysicsWorld::setBodyVelocity(IDType bodyID, glm::vec3 velocity) {
void PhysicsWorld::setLinearVelocity(IDType bodyID, glm::vec3 velocity) {
bodyInterface->SetLinearVelocity(bodyID, Vec3(velocity.x, velocity.y, velocity.z));
}

void PhysicsWorld::setAngularVelocity(IDType bodyID, glm::vec3 velocity) {
bodyInterface->SetAngularVelocity(bodyID, Vec3(velocity.x, velocity.y, velocity.z));
}

void PhysicsWorld::addForce(IDType bodyID, glm::vec3 force) {
bodyInterface->AddForce(bodyID, Vec3(force.x, force.y, force.z));
}
Expand Down Expand Up @@ -789,11 +796,12 @@ void PhysicsWorld::update(float dt, entt::registry &registry) {

float ratio = accumulator / cDeltaTime;

registry.view<PhysicsBody, InterpolatingBody>().each(
[this, ratio](entt::entity entity, PhysicsBody &body, InterpolatingBody &interpolation) {
body.position = glm::mix(interpolation.current.position, interpolation.next.position, ratio);
body.rotation = glm::slerp(interpolation.current.rotation, interpolation.next.rotation, ratio);
body.velocity = glm::mix(interpolation.current.velocity, interpolation.next.velocity, ratio);
body.scale = glm::mix(interpolation.current.scale, interpolation.next.scale, ratio);
});
registry.view<PhysicsBody, InterpolatingBody>().each([this, ratio](entt::entity entity, PhysicsBody &body,
InterpolatingBody &interpolation) {
body.position = glm::mix(interpolation.current.position, interpolation.next.position, ratio);
body.rotation = glm::slerp(interpolation.current.rotation, interpolation.next.rotation, ratio);
body.linearVelocity = glm::mix(interpolation.current.linearVelocity, interpolation.next.linearVelocity, ratio);
body.angularVelocity = glm::mix(interpolation.current.angularVelocity, interpolation.next.angularVelocity, ratio);
body.scale = glm::mix(interpolation.current.scale, interpolation.next.scale, ratio);
});
}
18 changes: 10 additions & 8 deletions src/engine/Physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ class PhysicsWorld {
std::vector<std::pair<glm::vec3, glm::vec3>> debugDraw();

// TODO: Add that these modify the PhysicsBody.
void setBodyPosition(IDType bodyID, glm::vec3 position);
void setBodyRotation(IDType bodyID, glm::quat rotation);
void setBodyScale(IDType bodyID, glm::vec3 scale);
void setBodyVelocity(IDType bodyID, glm::vec3 velocity);
void setPosition(IDType bodyID, glm::vec3 position);
void setRotation(IDType bodyID, glm::quat rotation);
void setScale(IDType bodyID, glm::vec3 scale);
void setLinearVelocity(IDType bodyID, glm::vec3 velocity);
void setAngularVelocity(IDType bodyID, glm::vec3 velocity);
void addForce(IDType bodyID, glm::vec3 force);
void addTorque(IDType bodyID, glm::vec3 torque);

Expand Down Expand Up @@ -88,10 +89,11 @@ class PhysicsWorld {
void onCarPhysicsDestroyed(entt::registry& registry, entt::entity entity);

MotionType getMotionType(IDType bodyID);
glm::vec3 getBodyPosition(IDType bodyID);
glm::quat getBodyRotation(IDType bodyID);
glm::vec3 getBodyScale(IDType bodyID);
glm::vec3 getBodyVelocity(IDType bodyID);
glm::vec3 getPosition(IDType bodyID);
glm::quat getRotation(IDType bodyID);
glm::vec3 getScale(IDType bodyID);
glm::vec3 getLinearVelocity(IDType bodyID);
glm::vec3 getAngularVelocity(IDType bodyID);

void setUserData(IDType bodyID, entt::entity entity);
entt::entity getUserData(IDType bodyID);
Expand Down
3 changes: 2 additions & 1 deletion src/engine/PhysicsBody.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct PhysicsBody {
glm::vec3 position;
glm::quat rotation;
glm::vec3 scale;
glm::vec3 velocity;
glm::vec3 linearVelocity;
glm::vec3 angularVelocity;

glm::mat4 getTransform() const;
};
Expand Down
7 changes: 4 additions & 3 deletions src/engine/networking_handlers/PhysicsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const void PhysicsHandler::internalHandle(entt::registry& registry, PhysicsWorld
printf("Set body %i!\n", state->networkID);

if (body) {
world->setBodyPosition(body->bodyID, fromFloat3(state->position));
world->setBodyVelocity(body->bodyID, fromFloat3(state->velocity));
world->setBodyRotation(body->bodyID, fromFloat4(state->rotation));
world->setPosition(body->bodyID, fromFloat3(state->position));
world->setLinearVelocity(body->bodyID, fromFloat3(state->linearVelocity));
world->setAngularVelocity(body->bodyID, fromFloat3(state->angularVelocity));
world->setRotation(body->bodyID, fromFloat4(state->rotation));
}
}
2 changes: 1 addition & 1 deletion src/engine/systems/TransformSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void PhysicsCamera::update(entt::registry &registry, float delta, entt::entity e
Camera &camera) const {
auto carPosition = body.position;
auto carRotation = body.rotation;
auto carVelocity = body.velocity;
auto carVelocity = body.linearVelocity;

carRotation.y *= -1;
auto forward = glm::vec3(0.0f, 0.0f, 1.0f) * carRotation;
Expand Down
13 changes: 9 additions & 4 deletions src/shared/SharedServerSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct PhysicsState : public Message {
uint32_t networkID;
float3 position;
float4 rotation;
float3 velocity;
float3 linearVelocity;
float3 angularVelocity;

PhysicsState() { tick = 0; }

Expand All @@ -44,9 +45,13 @@ struct PhysicsState : public Message {
serialize_float(stream, rotation.z);
serialize_float(stream, rotation.w);

serialize_float(stream, velocity.x);
serialize_float(stream, velocity.y);
serialize_float(stream, velocity.z);
serialize_float(stream, linearVelocity.x);
serialize_float(stream, linearVelocity.y);
serialize_float(stream, linearVelocity.z);

serialize_float(stream, angularVelocity.x);
serialize_float(stream, angularVelocity.y);
serialize_float(stream, angularVelocity.z);

return true;
}
Expand Down

0 comments on commit 9a47e78

Please sign in to comment.