Skip to content

Refactor/object position dimensions #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"timestep_length_ms": 30,
"maze": {
"directory": "maps",
"maze_file": "room.maze"
"maze_file": "maze5.maze"
}
},
"network": {
Expand Down
15 changes: 0 additions & 15 deletions include/debugger/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ class PrintCommand : public Command {
else if (property.compare("physics.movable") == 0) {
std::cout << (object->physics.movable ? "true" : "false") << std::endl;
}
else if (property.compare("physics.shared.position") == 0) {
std::cout << glm::to_string(object->physics.shared.position) << std::endl;
}
else if (property.compare("physics.velocity") == 0) {
std::cout << glm::to_string(object->physics.velocity) << std::endl;
}
Expand Down Expand Up @@ -346,18 +343,6 @@ class SetCommand : public Command {
}

// Set property
if (property.compare("physics.shared.position.x") == 0) {
obj->physics.shared.position.x = value;
std::cout << "Set object (global id " << id << ") position.x to " << value << ".\n";
}
else if (property.compare("physics.shared.position.y") == 0) {
obj->physics.shared.position.y = value;
std::cout << "Set object (global id " << id << ") position.y to " << value << ".\n";
}
else if (property.compare("physics.shared.position.z") == 0) {
obj->physics.shared.position.z = value;
std::cout << "Set object (global id " << id << ") position.z to " << value << ".\n";
}
else if (property.compare("physics.velocity.x") == 0) {
obj->physics.velocity.x = value;
std::cout << "Set object (global id " << id << ") velocity.x to " << value << ".\n";
Expand Down
36 changes: 18 additions & 18 deletions include/server/game/boxcollider.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#pragma once

#include "server/game/collider.hpp"

class BoxCollider : public Collider {
public:

BoxCollider();
BoxCollider(glm::vec3 corner, glm::vec3 dimension);
~BoxCollider();

bool detectCollision(Collider* otherBoundary) override;
bool resolveCollision(Collider* otherBoundary) override;
Shape getShape() override;

private:

};
//#pragma once
//
//#include "server/game/collider.hpp"
//
//class BoxCollider : public Collider {
//public:
//
// BoxCollider();
// BoxCollider(glm::vec3 corner, glm::vec3 dimension);
// ~BoxCollider();
//
// bool detectCollision(Collider* otherBoundary) override;
// bool resolveCollision(Collider* otherBoundary) override;
// Shape getShape() override;
//
//private:
//
//};
82 changes: 63 additions & 19 deletions include/server/game/collider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,71 @@

#include "server/game/constants.hpp"

class Collider {
public:
// Vector for bottom left corner
glm::vec3 corner;
struct Physics;

// Vector for dimensions
glm::vec3 dimensions;
//class Collider {
//public:
// // Vector for bottom left corner
// glm::vec3 corner;
//
// // Vector for dimensions
// glm::vec3 dimensions;
//
// enum Shape {
// Box,
// Sphere
// };
//
// virtual bool detectCollision(Collider* otherCollider) = 0;
//
// // TODO: Remove this method if collision resolution always happens in
// // ServerGameState::updateMovement()
// virtual bool resolveCollision(Collider* otherCollider) = 0;
//
// virtual Shape getShape() = 0;
//
//private:
//
//};

enum Shape {
Box,
Sphere
};
/**
* @brief Enumeration to identify collider shape
*/
enum class Collider {
// A collider type of None indicates that an object does not have a
// collider - all collision checks are ignored
None,
Box,
Sphere
};

virtual bool detectCollision(Collider* otherCollider) = 0;

// TODO: Remove this method if collision resolution always happens in
// ServerGameState::updateMovement()
virtual bool resolveCollision(Collider* otherCollider) = 0;
/**
* @brief Detects whether a collision has occurred between two objects given
* their Physics structs; this is done by checking for overlap between the two
* object's surfaces (defined by their positions + dimensions + collider shape)
* @param obj1 Reference to the first object's Physics struct.
* @param obj2 Reference to the second object's Physics struct.
* @return true if a collision (overlap) is detected between the two objects'
* surfaces, and false otherwise.
*/
bool detectCollision(Physics& obj1, Physics& obj2);

virtual Shape getShape() = 0;
/**
* @brief Detects whether a collision has occurred between two objects given
* their Physics structs and that the first object is a sphere.
* @param sphere Reference to the sphere object's Physics struct.
* @param obj Reference to the second object's Physics struct.
* @return true if a collision is detected between the sphere and the second
* object, and false otherwise.
*/
bool detectCollisionSphere(Physics& sphere, Physics& obj);

private:

};
/**
* @brief Detects whether a collision has occurred between two objects given
* their Physics structs and that the first object is a box.
* @param box Reference to the box object's Physics struct.
* @param obj Reference to the second object's Physics struct.
* @return true if a collision is detected between the box and the second
* object, and false otherwise.
*/
bool detectCollisionBox(Physics& box, Physics& obj);
37 changes: 35 additions & 2 deletions include/server/game/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "server/game/collider.hpp"
#include "shared/game/sharedobject.hpp"
#include "shared/utilities/typedefs.hpp"
#include "shared/game/sharedmodel.hpp"

// From sharedobject.hpp
class SharedObject;
Expand Down Expand Up @@ -41,9 +42,9 @@ struct Physics {
glm::vec3 velocityMultiplier;

/**
* @brief Pointer to this object's collider.
* @brief This object's collider type.
*/
Collider* boundary;
Collider collider;

/* Debugger Methods */
std::string to_string(unsigned int tab_offset);
Expand All @@ -69,11 +70,43 @@ class Object {
*/
ObjectType type;

/**
* @brief Object's Physics-related properties
*/
Physics physics;

/**
* @brief Object's render model information
*/
ModelType modelType;
//SharedModel model;

explicit Object(ObjectType type);
virtual ~Object();

/**
* @brief Sets this Object's model and initializes its dimensions to the
* given model's default dimensions.
* @param type ModelType of the render model that this Object should be
* rendered as.
*/
void setModel(ModelType type);

///**
// * @brief Sets object's dimensions to the given dimensions.
// * @note Always use this method to set object dimensions! This encapsulates
// * updating the object's SharedPhysics.dimensions and SharedModel.scale
// * fields, which must always be in sync!
// * @param dimensions New dimensions for this object.
// */
//void setDimensions(glm::vec3 dimensions);

/**
* @brief Maps from ModelType to a model's dimensions as read from the model
* files. (At present, these values are hard-coded in object.cpp)
*/
static std::unordered_map<ModelType, glm::vec3> models;

/**
* @brief Generates a SharedObject representation of this object.
* @return A SharedObject representation of this object.
Expand Down
38 changes: 19 additions & 19 deletions include/server/game/spherecollider.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#pragma once

#include "server/game/collider.hpp"

class SphereCollider : public Collider {
public:

SphereCollider();
SphereCollider(glm::vec3 corner, glm::vec3 dimension);
~SphereCollider();

bool detectCollision(Collider* otherCollider) override;

bool resolveCollision(Collider* otherCollider) override;
Shape getShape() override;

private:

};
//#pragma once
//
//#include "server/game/collider.hpp"
//
//class SphereCollider : public Collider {
//public:
//
// SphereCollider();
// SphereCollider(glm::vec3 corner, glm::vec3 dimension);
// ~SphereCollider();
//
// bool detectCollision(Collider* otherCollider) override;
//
// bool resolveCollision(Collider* otherCollider) override;
// Shape getShape() override;
//
//private:
//
//};
41 changes: 41 additions & 0 deletions include/shared/game/sharedmodel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

/**
* @brief Enumeration of every render model in the game.
*/
enum class ModelType {
Cube,
Player,
WarrenBear
};

/**
* @brief Struct containing an object's render information for the client.
*/
//struct SharedModel {
// /**
// * @brief Type of render model that the object containing this SharedModel
// * should be rendered as
// */
// ModelType type;
//
// /**
// * @brief Dimensions of a rectangular prism that circumscribes the model.
// * These dimensions are derived from the model's dimensions in its render
// * model object file.
// */
// glm::vec3 dimensions;
//
// /**
// * @brief 3-D vector whose components are multipliers for this model's
// * dimensions.
// * @note The effective dimensions of the render model (and of the object
// * that contains this SharedModel struct) are given by
// * SharedModel::dimensions * SharedModel::scale
// */
// glm::vec3 scale;
//
// DEF_SERIALIZE(Archive& ar, const unsigned int version) {
// ar& type& dimensions& scale;
// }
//};
15 changes: 6 additions & 9 deletions include/shared/game/sharedobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//#include "server/game/object.hpp"
#include "shared/utilities/serialize_macro.hpp"
#include "shared/utilities/typedefs.hpp"
#include "shared/game/sharedmodel.hpp"

/**
* @brief An enum for the type of an object; the fields here should match all
Expand Down Expand Up @@ -60,7 +61,6 @@ struct SharedSolidSurface {
* @brief Dimensions of the solid surface in 3 dimensions. The position of
* the SolidSurface object is at the center of the object.
*/
glm::vec3 dimensions;

/**
* @brief Type of solid surface, e.g. wall, floor, ceiling, etc.(relevant
Expand All @@ -69,16 +69,11 @@ struct SharedSolidSurface {
SurfaceType surfaceType;

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar& dimensions& surfaceType;
ar& surfaceType;
}
};

struct SharedPhysics {
/**
* @brief 3-D vector that denotes this object's current position.
*/
glm::vec3 position;

/**
* @brief 3-D vector that denotes this object's bottom left corner
* (min x and z coordinates).
Expand All @@ -99,7 +94,7 @@ struct SharedPhysics {
glm::vec3 dimensions;

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar& position& corner& facing & dimensions;
ar& corner& facing & dimensions;
}
};

Expand All @@ -112,6 +107,8 @@ class SharedObject {
EntityID globalID;
ObjectType type;
SharedPhysics physics;
//SharedModel model;
ModelType modelType;

boost::optional<SharedStats> stats;
boost::optional<SharedItemInfo> iteminfo;
Expand All @@ -121,7 +118,7 @@ class SharedObject {
~SharedObject() {}

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar& globalID & type& physics & stats & iteminfo & solidSurface;
ar& globalID & type& physics & modelType & stats & iteminfo & solidSurface;
}
private:
};
Loading
Loading