Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
feat(physics): make density and restitution configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Aug 12, 2020
1 parent a21706e commit 6dc39ef
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
8 changes: 8 additions & 0 deletions assets/scenes/menu.json
Expand Up @@ -24,6 +24,8 @@
"name": "PhysicsBody",
"type":"static",
"sensor": false,
"density": 0,
"restitution": 0,
"data": [200, 200, 50, 50],
"category": ["boundary"],
"mask": ["player", "bullet"]
Expand All @@ -32,6 +34,8 @@
"name": "PhysicsBody",
"type": "static",
"sensor": false,
"density": 0,
"restitution": 0,
"data": [150, 250, 50, 50],
"category": ["boundary"],
"mask": ["player", "bullet"]
Expand All @@ -46,6 +50,8 @@
"name": "PhysicsBody",
"type":"static",
"sensor": true,
"density": 0,
"restitution": 0,
"data": [200, 250, 25, 25],
"category": ["collectible"],
"mask": ["player"]
Expand All @@ -70,6 +76,8 @@
"name": "PhysicsBody",
"type":"dynamic",
"sensor": false,
"density": 10000,
"restitution": 0,
"data": [50, 50, 50, 50],
"category": ["player"],
"mask": ["boundary", "collectible", "bullet"]
Expand Down
13 changes: 12 additions & 1 deletion include/components/PhysicsBody.h
Expand Up @@ -11,13 +11,16 @@ class PhysicsBody final : public Component {
b2Body* body_{};
b2BodyType type_;
bool sensor_;
float density_;
float restitution_;
Vector4<int32_t> data_;
uint16_t category_;
uint16_t mask_;

public:
PhysicsBody(std::weak_ptr<GameObject> gameObject, b2BodyType type,
bool sensor, Vector4<int32_t> vector, uint16_t category,
bool sensor, float density, float restitution,
Vector4<int32_t> vector, uint16_t category,
uint16_t mask) noexcept;
~PhysicsBody() noexcept override;

Expand All @@ -31,6 +34,14 @@ class PhysicsBody final : public Component {

[[nodiscard]] inline const bool& sensor() const noexcept { return sensor_; }

[[nodiscard]] inline const float& density() const noexcept {
return density_;
}

[[nodiscard]] inline const float& restitution() const noexcept {
return restitution_;
}

[[nodiscard]] inline const uint16_t& category() const noexcept {
return category_;
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/BulletBox.cpp
Expand Up @@ -24,8 +24,7 @@ void BulletBox::onAwake() noexcept {

auto* pBody = body_.lock()->body();
pBody->SetTransform(data_.toVector2<float>().toVec(), 0.f);
pBody->ApplyLinearImpulseToCenter(velocity_.toVec(), true);
// pBody->ApplyForceToCenter(velocity_.toVec(), true);
pBody->ApplyForceToCenter(velocity_.toVec(), true);
}

void BulletBox::onUpdate() noexcept {
Expand Down
11 changes: 8 additions & 3 deletions src/components/PhysicsBody.cpp
Expand Up @@ -7,11 +7,14 @@
#include "scenes/Scene.h"

PhysicsBody::PhysicsBody(std::weak_ptr<GameObject> parent, b2BodyType type,
bool sensor, Vector4<int32_t> data, uint16_t category,
bool sensor, float density, float restitution,
Vector4<int32_t> data, uint16_t category,
uint16_t mask) noexcept
: Component(std::move(parent)),
type_(type),
sensor_(sensor),
density_(density),
restitution_(restitution),
data_(data),
category_(category),
mask_(mask) {
Expand All @@ -21,6 +24,7 @@ PhysicsBody::PhysicsBody(std::weak_ptr<GameObject> parent, b2BodyType type,
static_cast<float>(data.y()));
bodyDef.angle = 0.f;
bodyDef.fixedRotation = true;
bodyDef.awake = true;

body_ = scene().lock()->world().CreateBody(&bodyDef);

Expand All @@ -30,11 +34,12 @@ PhysicsBody::PhysicsBody(std::weak_ptr<GameObject> parent, b2BodyType type,

b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape;
fixtureDef.density = 1.f;
fixtureDef.userData = this;
fixtureDef.density = density;
fixtureDef.isSensor = sensor_;
fixtureDef.filter.categoryBits = category_;
fixtureDef.filter.maskBits = mask_;
fixtureDef.restitution = restitution;
fixtureDef.userData = this;
body_->CreateFixture(&fixtureDef);
}

Expand Down
5 changes: 3 additions & 2 deletions src/components/PlayerController.cpp
Expand Up @@ -73,11 +73,12 @@ void PlayerController::onUpdate() noexcept {
const auto newTransform = std::make_shared<Transform>(
go->shared_from_this(), bp.toVector2<float>(), bs);
const auto newPhysics = std::make_shared<PhysicsBody>(
go->shared_from_this(), b2BodyType::b2_dynamicBody, false, bp,
go->shared_from_this(), b2BodyType::b2_dynamicBody, false, 1.f, 1.f, bp,
static_cast<uint16_t>(1 << 4), static_cast<uint16_t>(0b11111));
const auto newBullet = std::make_shared<BulletBox>(
go->shared_from_this(), bp,
Vector2<double>{-cos(angle) * 10000.0, -sin(angle) * 10000.0}, 2.f);
Vector2<double>{-cos(angle) * 5000000000.0, -sin(angle) * 5000000000.0},
2.f);
go->addComponent(newTransform);
go->addComponent(newPhysics);
go->addComponent(newBullet);
Expand Down
6 changes: 5 additions & 1 deletion src/factories/PhysicsBodyFactory.cpp
Expand Up @@ -10,6 +10,7 @@ PhysicsBodyFactory::~PhysicsBodyFactory() noexcept = default;
// "name": "PhysicsBody",
// "type": "static",
// "sensor": false,
// "restitution": 0.6,
// "data": [400, 400, 100, 100],
// "category": ["player"],
// "mask": ["boundary"]
Expand All @@ -19,7 +20,8 @@ std::shared_ptr<PhysicsBody> PhysicsBodyFactory::fromJson(
const Json::Value& json, std::weak_ptr<GameObject> parent) {
return std::make_shared<PhysicsBody>(
parent, getBodyTypeFromName(json["type"].asString()),
json["sensor"].asBool(), Vector4<int32_t>(json["data"]),
json["sensor"].asBool(), json["density"].asFloat(),
json["restitution"].asFloat(), Vector4<int32_t>(json["data"]),
getMaskFromJson(json["category"]), getMaskFromJson(json["mask"]));
}

Expand All @@ -29,6 +31,8 @@ Json::Value PhysicsBodyFactory::toJson(
json["name"] = name();
json["type"] = getNameFromBodyType(value->type());
json["sensor"] = value->sensor();
json["density"] = value->density();
json["restitution"] = value->restitution();
json["data"] = value->data().toJson();
json["category"] = getJsonFromMask(value->category());
json["mask"] = getJsonFromMask(value->mask());
Expand Down

0 comments on commit 6dc39ef

Please sign in to comment.