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 all 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
18 changes: 0 additions & 18 deletions include/server/game/boxcollider.hpp

This file was deleted.

67 changes: 43 additions & 24 deletions include/server/game/collider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@

#include "server/game/constants.hpp"

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:

};
struct Physics;

/**
* @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
};

/**
* @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);

/**
* @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);

/**
* @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);
28 changes: 26 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,34 @@ class Object {
*/
ObjectType type;

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

/**
* @brief Object's render model type (specifies this Object's render model
* to the client)
*/
ModelType modelType;

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 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
5 changes: 0 additions & 5 deletions include/server/game/servergamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#include <unordered_map>
#include <queue>

// From sharedgamestate.hpp
//struct SharedGameState;

/// Represents a list of events from a certain client with a specified ID
using EventList = std::vector<std::pair<EntityID, Event>>;

Expand Down Expand Up @@ -186,6 +183,4 @@ class ServerGameState {
* @brief 2-D Grid of GridCells (filled after loadMaze() is called).
*/
Grid grid;

// TODO: Add reference to passed-in Event queue.
};
19 changes: 0 additions & 19 deletions include/server/game/spherecollider.hpp

This file was deleted.

10 changes: 10 additions & 0 deletions include/shared/game/sharedmodel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

/**
* @brief Enumeration of every render model in the game.
*/
enum class ModelType {
Cube,
Player,
WarrenBear
};
26 changes: 11 additions & 15 deletions include/shared/game/sharedobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <optional>
#include <glm/glm.hpp>

//#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 @@ -56,29 +56,18 @@ enum class SurfaceType {
};

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
* for rendering)
*/
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 @@ -98,8 +87,14 @@ struct SharedPhysics {
*/
glm::vec3 dimensions;

/**
* @brief Calculates and returns the center position of this object.
* @return glm::vec3 that denotes the center position of this object.
*/
glm::vec3 getCenterPosition();

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

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

boost::optional<SharedStats> stats;
boost::optional<SharedItemInfo> iteminfo;
Expand All @@ -121,7 +117,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:
};
16 changes: 10 additions & 6 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ void Client::draw() {
case ObjectType::Player: {
// don't render yourself
if (this->session->getInfo().client_eid.has_value() && sharedObject->globalID == this->session->getInfo().client_eid.value()) {
glm::vec3 pos = sharedObject->physics.position;
// TODO: Update the player eye level to an acceptable level
glm::vec3 pos = sharedObject->physics.getCenterPosition();
pos.y += PLAYER_EYE_LEVEL;
cam->updatePos(pos);
break;
}
auto lightPos = glm::vec3(0.0f, 10.0f, 0.0f);
auto player_pos = glm::vec3(sharedObject->physics.position.x, sharedObject->physics.position.y + 0.1, sharedObject->physics.position.z);

auto player_pos = sharedObject->physics.getCenterPosition();

this->player_model->translateAbsolute(player_pos);
this->player_model->draw(
Expand All @@ -307,7 +309,8 @@ void Client::draw() {
// warren bear is an enemy because why not
// auto pos = glm::vec3(0.0f, 0.0f, 0.0f);
auto lightPos = glm::vec3(-5.0f, 0.0f, 0.0f);
this->bear_model->translateAbsolute(sharedObject->physics.position);

this->bear_model->translateAbsolute(sharedObject->physics.getCenterPosition());
this->bear_model->draw(
this->model_shader,
this->cam->getViewProj(),
Expand All @@ -331,8 +334,9 @@ void Client::draw() {
}
case ObjectType::SolidSurface: {
auto cube = std::make_unique<Cube>(glm::vec3(0.4f,0.5f,0.7f));
cube->scale( sharedObject->solidSurface->dimensions);
cube->translateAbsolute(sharedObject->physics.position);
cube->scale(sharedObject->physics.dimensions);

cube->translateAbsolute(sharedObject->physics.getCenterPosition());
cube->draw(this->cube_shader,
this->cam->getViewProj(),
this->cam->getPos(),
Expand All @@ -352,7 +356,7 @@ void Client::keyCallback(GLFWwindow *window, int key, int scancode, int action,
/* Store player EID for use in certain key handling */
std::optional<EntityID> eid;

if (this->session->getInfo().client_eid.has_value()) {
if (this->session != nullptr && this->session->getInfo().client_eid.has_value()) {
eid = this->session->getInfo().client_eid.value();
}

Expand Down
3 changes: 1 addition & 2 deletions src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ set(LIB_NAME game_server_lib)
set(FILES
lobbybroadcaster.cpp
server.cpp
game/boxcollider.cpp
game/spherecollider.cpp
game/collider.cpp
game/creature.cpp
game/item.cpp
game/solidsurface.cpp
Expand Down
Loading
Loading