Compare commits

..

3 Commits

Author SHA1 Message Date
Kayne Ruse 7c51f820d5 I fucking hate collisions 2014-12-27 20:27:24 +11:00
Kayne Ruse 8bf45bc8e4 Merge branch 'high-water-mark' into collisions 2014-12-27 19:30:12 +11:00
Kayne Ruse 5c1ea1988e Trying to implement smooth collisions without other issues 2014-12-27 16:43:40 +11:00
5 changed files with 132 additions and 82 deletions
+51 -7
View File
@@ -21,15 +21,59 @@
*/
#include "local_character.hpp"
#include <iostream>
bool LocalCharacter::ProcessCollisions(std::list<BoundingBox>& boxList) {
if (CheckCollisionSimple(boxList, origin + motion)) {
Vector2 velocity;
velocity.x = CorrectVelocityX(boxList, motion.x);
velocity.y = CorrectVelocityY(boxList, motion.y);
origin += velocity;
return true;
}
else {
origin += motion;
return false;
}
}
bool LocalCharacter::ProcessCollisionGrid(std::list<BoundingBox> boxList) {
for(auto& box : boxList) {
if (box.CheckOverlap(origin + bounds)) {
origin -= motion;
motion = {0, 0};
bool LocalCharacter::CheckCollisionSimple(std::list<BoundingBox>& boxList, Vector2 newPos) {
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + newPos)) {
return true;
}
}
return false;
}
}
double LocalCharacter::CorrectVelocityX(std::list<BoundingBox>& boxList, double velocityX) {
double ret = velocityX;
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + origin + Vector2(velocityX, 0) )) {
if (velocityX > 0) {
ret = std::min(ret, it.x - (origin.x + bounds.x + bounds.w -1));
ret = std::min(ret, 0.0);
}
else if (velocityX < 0) {
ret = std::max(ret, (it.x + it.w) - (origin.x + bounds.x));
ret = std::max(ret, 0.0);
}
}
}
return ret;
}
double LocalCharacter::CorrectVelocityY(std::list<BoundingBox>& boxList, double velocityY) {
double ret = velocityY;
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + origin + Vector2(0, velocityY) )) {
if (velocityY > 0) {
ret = std::min(ret, it.y - (origin.y + bounds.y + bounds.h -1));
ret = std::min(ret, 0.0);
}
else if (velocityY < 0) {
ret = std::max(ret, (it.y + it.h) - (origin.y + bounds.y));
ret = std::max(ret, 0.0);
}
}
}
return ret;
}
+5 -3
View File
@@ -33,10 +33,12 @@ public:
LocalCharacter() = default;
virtual ~LocalCharacter() = default;
bool ProcessCollisionGrid(std::list<BoundingBox>);
bool ProcessCollisions(std::list<BoundingBox>& boxList);
private:
//NOTE: NO MEMBERS
protected:
bool CheckCollisionSimple(std::list<BoundingBox>& boxList, Vector2 newPos);
double CorrectVelocityX(std::list<BoundingBox>& boxList, double velocityX);
double CorrectVelocityY(std::list<BoundingBox>& boxList, double velocityY);
};
#endif
+45 -69
View File
@@ -134,12 +134,27 @@ void InWorld::Update() {
//free the buffer
delete reinterpret_cast<char*>(packetBuffer);
//heartbeat system
CheckHeartBeat();
//check the connection (heartbeat)
if (Clock::now() - lastBeat > std::chrono::seconds(3)) {
if (attemptedBeats > 2) {
//escape to the disconnect screen
SendDisconnectRequest();
SetNextScene(SceneList::DISCONNECTEDSCREEN);
ConfigUtility::GetSingleton()["client.disconnectMessage"] = "Error: Lost connection to the server";
}
else {
ServerPacket newPacket;
newPacket.type = SerialPacketType::PING;
network.SendTo(Channels::SERVER, &newPacket);
attemptedBeats++;
lastBeat = Clock::now();
}
}
//update all entities
for (auto& it : characterMap) {
it.second.Update();
// it.second.Update();
}
for (auto& it : monsterMap) {
it.second.Update();
@@ -153,14 +168,35 @@ void InWorld::Update() {
return;
}
//get the collidable boxes
std::list<BoundingBox> boxList = GenerateCollisionGrid(localCharacter, tileSheet.GetTileW(), tileSheet.GetTileH());
//prepare for collisions
BoundingBox wallBounds = {0, 0, tileSheet.GetTileW(), tileSheet.GetTileH()};
std::list<BoundingBox> boxList;
//NOTE: for loops were too dense to work with, so I've just used while loops
//NOTE: this code is complex, and can be replaced with hard-coded relative positions, at the cost of variable-sized sprites/bounding boxes
//outer loop
wallBounds.x = snapToBase((double)wallBounds.w, localCharacter->GetOrigin().x) - wallBounds.w;
while(wallBounds.x < (localCharacter->GetOrigin() + localCharacter->GetBounds()).x + localCharacter->GetBounds().w) {
//inner loop
wallBounds.y = snapToBase((double)wallBounds.h, localCharacter->GetOrigin().y) - wallBounds.h;
while(wallBounds.y < (localCharacter->GetOrigin() + localCharacter->GetBounds()).y + localCharacter->GetBounds().h) {
//check to see if this tile is solid
if (regionPager.GetSolid(wallBounds.x / wallBounds.w, wallBounds.y / wallBounds.h)) {
//push onto the box set
boxList.push_front(wallBounds);
}
//increment
wallBounds.y += wallBounds.h;
}
//increment
wallBounds.x += wallBounds.w;
}
//process the collisions
if (localCharacter->ProcessCollisionGrid(boxList)) {
localCharacter->CorrectSprite();
SendLocalCharacterMotion();
}
localCharacter->ProcessCollisions(boxList);
//update the camera
camera.x = localCharacter->GetOrigin().x - camera.marginX;
@@ -463,26 +499,6 @@ void InWorld::HandleDisconnectForced(ClientPacket* const argPacket) {
ConfigUtility::GetSingleton()["client.disconnectMessage"] = "You have been forcibly disconnected by the server";
}
void InWorld::CheckHeartBeat() {
//check the connection (heartbeat)
if (Clock::now() - lastBeat > std::chrono::seconds(3)) {
if (attemptedBeats > 2) {
//escape to the disconnect screen
SendDisconnectRequest();
SetNextScene(SceneList::DISCONNECTEDSCREEN);
ConfigUtility::GetSingleton()["client.disconnectMessage"] = "Error: Lost connection to the server";
}
else {
ServerPacket newPacket;
newPacket.type = SerialPacketType::PING;
network.SendTo(Channels::SERVER, &newPacket);
attemptedBeats++;
lastBeat = Clock::now();
}
}
}
//-------------------------
//map management
//-------------------------
@@ -675,11 +691,6 @@ void InWorld::HandleCharacterSetRoom(CharacterPacket* const argPacket) {
}
void InWorld::HandleCharacterSetOrigin(CharacterPacket* const argPacket) {
//TODO: Authentication
if (argPacket->characterIndex == characterIndex) {
return;
}
//check that this character exists
std::map<int, BaseCharacter>::iterator characterIt = characterMap.find(argPacket->characterIndex);
if (characterIt != characterMap.end()) {
@@ -691,11 +702,6 @@ void InWorld::HandleCharacterSetOrigin(CharacterPacket* const argPacket) {
}
void InWorld::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
//TODO: Authentication
if (argPacket->characterIndex == characterIndex) {
return;
}
//check that this character exists
std::map<int, BaseCharacter>::iterator characterIt = characterMap.find(argPacket->characterIndex);
if (characterIt != characterMap.end()) {
@@ -722,34 +728,4 @@ void InWorld::SendLocalCharacterMotion() {
newPacket.motion = localCharacter->GetMotion();
network.SendTo(Channels::SERVER, &newPacket);
}
std::list<BoundingBox> InWorld::GenerateCollisionGrid(Entity* ptr, int tileWidth, int tileHeight) {
//prepare for collisions
BoundingBox wallBounds = {0, 0, tileWidth, tileHeight};
std::list<BoundingBox> boxList;
//NOTE: for loops were too dense to work with, so I've just used while loops
//outer loop
wallBounds.x = snapToBase((double)wallBounds.w, ptr->GetOrigin().x);
while(wallBounds.x < (ptr->GetOrigin() + ptr->GetBounds()).x + ptr->GetBounds().w) {
//inner loop
wallBounds.y = snapToBase((double)wallBounds.h, ptr->GetOrigin().y);
while(wallBounds.y < (ptr->GetOrigin() + ptr->GetBounds()).y + ptr->GetBounds().h) {
//check to see if this tile is solid
if (regionPager.GetSolid(wallBounds.x / wallBounds.w, wallBounds.y / wallBounds.h)) {
//push onto the box set
boxList.push_front(wallBounds);
}
//increment
wallBounds.y += wallBounds.h;
}
//increment
wallBounds.x += wallBounds.w;
}
return std::move(boxList);
}
-3
View File
@@ -85,8 +85,6 @@ protected:
void HandleDisconnectResponse(ClientPacket* const);
void HandleDisconnectForced(ClientPacket* const);
void CheckHeartBeat();
//map management
void SendRegionRequest(int roomIndex, int x, int y);
void HandleRegionContent(RegionPacket* const);
@@ -102,7 +100,6 @@ protected:
//player movement
void SendLocalCharacterMotion();
std::list<BoundingBox> GenerateCollisionGrid(Entity*, int tileWidth, int tileHeight);
//indexes
int& clientIndex;
+31
View File
@@ -0,0 +1,31 @@
TODO: Account system needs salts & hashes for security
TODO: Character system might need an API
TODO: Door system needs an API
TODO: monster system needs an API
TODO: rewrite the main body of the server
TODO: I need a better way to handle the statistics
TODO: Fix shoddy movement
TODO: Handle statistics server-side
TODO: Periodic mass server saves
TODO: join vs login
TODO: Remove the big "Shut Down" button
TODO: Make a way for the server owner to control the server directly
TODO: The TileSheet class should implement the surface itself
TODO: Passwords/Authentication
TODO: Time delay for requesting region packets
TODO: A proper logging system
-------------------------
The entities might need an API, which interfaces with all entity types (characters, monsters, doors, etc.)
The Entity base class handles position (including room) and motion, so something generic like this could be used (or aliased by other APIs)
entity:
-------------------------
Lobby:
JOIN_REQUEST -> JOIN_RESPONSE, JOIN_REJECTION
LOGIN_REQUEST -> LOGIN_RESPONSE, LOGIN_REJECTION