FIRST WORKING HOLES!!!! WOW!

This commit is contained in:
Maximilian Giller 2023-07-05 01:06:57 +02:00
parent a5ceecd2a4
commit b457c33a72
9 changed files with 101 additions and 57 deletions

View file

@ -50,7 +50,7 @@
#define COLLECTABLES_SIM_POSITION_ITERATIONS 3 #define COLLECTABLES_SIM_POSITION_ITERATIONS 3
#define COLLECTABLES_SIM_LINEAR_DAMPING 0.5f #define COLLECTABLES_SIM_LINEAR_DAMPING 0.5f
#define COLLECTABLES_SIM_ANGULAR_DAMPING 0.5f #define COLLECTABLES_SIM_ANGULAR_DAMPING 0.5f
#define COLLECTABLES_SIM_DENSITY 1.f #define COLLECTABLES_SIM_DENSITY 3.f
#define COLLECTABLES_SIM_FRICTION 0.0f #define COLLECTABLES_SIM_FRICTION 0.0f
#define COLLECTABLES_SIM_RESTITUTION 0.5f #define COLLECTABLES_SIM_RESTITUTION 0.5f
#define COLLECTABLES_SIM_SLEEPING true #define COLLECTABLES_SIM_SLEEPING true

View file

@ -26,8 +26,8 @@ void BodyAdapter::createSquare(b2BodyType type, sf::Vector2f center, sf::Vector2
b2BodyDef bodyDef; b2BodyDef bodyDef;
bodyDef.type = type; bodyDef.type = type;
bodyDef.angularDamping = COLLECTABLES_SIM_ANGULAR_DAMPING; // bodyDef.angularDamping = COLLECTABLES_SIM_ANGULAR_DAMPING;
bodyDef.linearDamping = COLLECTABLES_SIM_LINEAR_DAMPING; // bodyDef.linearDamping = COLLECTABLES_SIM_LINEAR_DAMPING;
bodyDef.position.Set(center.x, center.y); bodyDef.position.Set(center.x, center.y);
bodyObject = world->CreateBody(&bodyDef); bodyObject = world->CreateBody(&bodyDef);
@ -42,8 +42,7 @@ b2Body *BodyAdapter::body() const
sf::Vector2f BodyAdapter::getCenter() const sf::Vector2f BodyAdapter::getCenter() const
{ {
auto position = bodyObject->GetPosition(); auto position = bodyObject->GetPosition();
auto velocity = bodyObject->GetLinearVelocity(); return {position.x, position.y};
return sf::Vector2f(position.x, position.y) + sf::Vector2f(velocity.x, velocity.y);
} }
sf::Vector2f BodyAdapter::getSize() const sf::Vector2f BodyAdapter::getSize() const
@ -53,10 +52,7 @@ sf::Vector2f BodyAdapter::getSize() const
void BodyAdapter::setCenter(sf::Vector2f center) void BodyAdapter::setCenter(sf::Vector2f center)
{ {
// Set via velocity bodyObject->SetTransform({center.x, center.y}, bodyObject->GetAngle());
auto velocity = bodyObject->GetLinearVelocity();
velocity += b2Vec2(center.x, center.y) - bodyObject->GetPosition();
bodyObject->SetLinearVelocity(velocity);
} }
void BodyAdapter::setBoxSize(sf::Vector2f size) void BodyAdapter::setBoxSize(sf::Vector2f size)
@ -74,8 +70,8 @@ void BodyAdapter::setBoxSize(sf::Vector2f size)
b2FixtureDef fixtureDef; b2FixtureDef fixtureDef;
fixtureDef.shape = &shape; fixtureDef.shape = &shape;
fixtureDef.friction = COLLECTABLES_SIM_FRICTION; // fixtureDef.friction = COLLECTABLES_SIM_FRICTION;
fixtureDef.restitution = COLLECTABLES_SIM_RESTITUTION; // fixtureDef.restitution = COLLECTABLES_SIM_RESTITUTION;
fixtureDef.density = COLLECTABLES_SIM_DENSITY; fixtureDef.density = COLLECTABLES_SIM_DENSITY;
bodyObject->CreateFixture(&fixtureDef); bodyObject->CreateFixture(&fixtureDef);

View file

@ -13,12 +13,15 @@ CollectableSimulation::CollectableSimulation(const std::shared_ptr<Collectable>
// Create ground // Create ground
simGround = std::make_shared<CollectableSimGround>(world); simGround = std::make_shared<CollectableSimGround>(world);
// Todo: Set proper ground width // Todo: Set proper ground width
// Create body // Create body
collectableBody = std::make_shared<BodyAdapter>(world); collectableBody = std::make_shared<BodyAdapter>(world);
auto coordinates = collectable->coordinates->diagonalWorld(); auto coordinates = collectable->coordinates->diagonalWorld();
collectableBody->createSquare(b2_dynamicBody, {coordinates.horizontal, coordinates.vertical}, sf::Vector2f(1, 1));
// todo: scale collectable and HOLE WIDTH SIZE based on factor
auto size = sf::Vector2f(1, 1);
collectableBody->createSquare(b2_dynamicBody, {coordinates.horizontal, coordinates.vertical}, size);
} }
void CollectableSimulation::physicsUpdate() void CollectableSimulation::physicsUpdate()
@ -38,18 +41,7 @@ std::shared_ptr<Collectable> CollectableSimulation::getCollectable() const
void CollectableSimulation::updateGroundHole() void CollectableSimulation::updateGroundHole()
{ {
auto holeLayout = HoleLayout::getInstance()->atDepth(collectable->getDepth()); auto holeLayout = HoleLayout::getInstance()->atDepth(collectable->getDepth());
simGround->createLayout(holeLayout);
if (holeLayout.holes.empty())
{
simGround->closeAllHoles();
return;
}
// TODO: Remove assumption
// Assume only one hole
auto hole = holeLayout.holes[0];
// todo: Update ground hole
} }
void CollectableSimulation::updateCollectable() void CollectableSimulation::updateCollectable()
@ -58,6 +50,6 @@ void CollectableSimulation::updateCollectable()
collectable->coordinates->setDiagonal({bodyPosition.x, bodyPosition.y, collectable->getDepth()}); collectable->coordinates->setDiagonal({bodyPosition.x, bodyPosition.y, collectable->getDepth()});
// Convert radians to degrees // Convert radians to degrees
float angle = collectableBody->body()->GetAngle() * 180 / M_PI; float angle = -collectableBody->body()->GetAngle() * 180 / M_PI;
collectable->setRotation(angle); collectable->setRotation(angle);
} }

View file

@ -2,22 +2,47 @@
#include <utility> #include <utility>
#include "../../../../config.h" #include "../../../../config.h"
#include "../layouts/depth_hole_layout.hpp"
void CollectableSimGround::closeAllHoles() void CollectableSimGround::closeAllHoles()
{ {
// Create one segment for the ground
createSegment(-groundWidth / 2.f, groundWidth / 2.f);
}
void CollectableSimGround::createLayout(DepthHoleLayout &layout)
{
// TODO: Check if layout changed at all before clearing segments
segments.clear(); segments.clear();
// Create one segment for the ground if (layout.holes.empty())
auto groundSegment = std::make_shared<CollectableSimGroundSegment>(world); {
groundSegment->body->createSquare(b2_kinematicBody, closeAllHoles();
{0, -COLLECTABLES_SIM_GROUND_THICKNESS / 2.f}, return;
{groundWidth, COLLECTABLES_SIM_GROUND_THICKNESS}); }
segments.push_back(groundSegment); // Sort holes from left to right
std::sort(layout.holes.begin(), layout.holes.end(),
[](DepthHoleDescription a, DepthHoleDescription b)
{
return a.x < b.x;
});
// Create segments for holes
float leftCorner = -groundWidth / 2.f;
for (auto &hole: layout.holes)
{
auto rightCorner = hole.x - hole.width / 2.f;
createSegment(leftCorner, rightCorner);
leftCorner = hole.x + hole.width / 2.f;
}
// Create segment for the right side
createSegment(leftCorner, groundWidth / 2.f);
} }
CollectableSimGround::CollectableSimGround(std::shared_ptr<b2World> world, float groundWidth) CollectableSimGround::CollectableSimGround(std::shared_ptr<b2World> world, float groundWidth)
: world(std::move(world)) : world(std::move(world)), groundWidth(0)
{ {
setGroundWidth(groundWidth); setGroundWidth(groundWidth);
closeAllHoles(); closeAllHoles();
@ -50,14 +75,9 @@ void CollectableSimGround::updateOuterSegmentsToWidth()
auto leftSegment = outerSegments.left->body; auto leftSegment = outerSegments.left->body;
auto leftCenter = leftSegment->getCenter(); auto leftCenter = leftSegment->getCenter();
auto leftSize = leftSegment->getSize(); auto leftSize = leftSegment->getSize();
auto deltaToBorder = borderPoints - abs(leftCenter.x - leftSize.x / 2.f); auto rightPoint = leftCenter.x + leftSize.x / 2.f;
// Increase width if positive, decrease if negative createSegment(-borderPoints, rightPoint, outerSegments.left);
leftSize.x += deltaToBorder;
leftCenter.x -= deltaToBorder / 2.f;
leftSegment->setBoxSize(leftSize);
leftSegment->setCenter(leftCenter);
} }
// Right segment // Right segment
@ -66,17 +86,32 @@ void CollectableSimGround::updateOuterSegmentsToWidth()
auto rightSegment = outerSegments.right->body; auto rightSegment = outerSegments.right->body;
auto rightCenter = rightSegment->getCenter(); auto rightCenter = rightSegment->getCenter();
auto rightSize = rightSegment->getSize(); auto rightSize = rightSegment->getSize();
auto deltaToBorder = borderPoints - abs(rightCenter.x + rightSize.x / 2.f); auto leftPoint = rightCenter.x - rightSize.x / 2.f;
// Increase width if positive, decrease if negative createSegment(leftPoint, borderPoints, outerSegments.right);
rightSize.x += deltaToBorder;
rightCenter.x += deltaToBorder / 2.f;
rightSegment->setBoxSize(rightSize);
rightSegment->setCenter(rightCenter);
} }
} }
void CollectableSimGround::createSegment(float leftCorner, float rightCorner,
std::shared_ptr<CollectableSimGroundSegment> segment)
{
if (leftCorner > rightCorner)
{
throw std::runtime_error("Left corner must be less than right corner");
}
if (segment == nullptr)
{
segment = std::make_shared<CollectableSimGroundSegment>(world);
segments.push_back(segment);
}
auto center = sf::Vector2f((leftCorner + rightCorner) / 2.f, -COLLECTABLES_SIM_GROUND_THICKNESS / 2.f);
auto size = sf::Vector2f(rightCorner - leftCorner, COLLECTABLES_SIM_GROUND_THICKNESS);
segment->body->createSquare(b2_kinematicBody, center, size);
}
CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments(int holeId) CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments(int holeId)
{ {
SideSegments sideSegments; SideSegments sideSegments;

View file

@ -5,16 +5,18 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "collectable_sim_ground_segment.hpp" #include "collectable_sim_ground_segment.hpp"
#include "../layouts/depth_hole_layout.hpp"
class CollectableSimGround class CollectableSimGround
{ {
public: public:
CollectableSimGround(std::shared_ptr<b2World> world, float groundWidth = 1000); explicit CollectableSimGround(std::shared_ptr<b2World> world, float groundWidth = 1000);
void closeAllHoles();
void setGroundWidth(float width); void setGroundWidth(float width);
void createLayout(DepthHoleLayout &layout);
private: private:
std::vector<std::shared_ptr<CollectableSimGroundSegment>> segments; std::vector<std::shared_ptr<CollectableSimGroundSegment>> segments;
std::shared_ptr<b2World> world; std::shared_ptr<b2World> world;
@ -28,6 +30,10 @@ private:
SideSegments getOuterSegments(int holeId = -1); SideSegments getOuterSegments(int holeId = -1);
void updateOuterSegmentsToWidth(); void updateOuterSegmentsToWidth();
void createSegment(float leftCorner, float rightCorner, std::shared_ptr<CollectableSimGroundSegment> segment = nullptr);
void closeAllHoles();
}; };

View file

@ -33,8 +33,11 @@ struct HoleDescription
} }
float chordLength = 2.f * std::sqrt(radius * radius - distance * distance); float chordLength = 2.f * std::sqrt(radius * radius - distance * distance);
float chordLengthInDiagonal = chordLength * WORLD_TO_ISO_SCALE;
return {playerId, closestPointAtDepth.x, chordLength}; auto diagonalWorldCoords = CoordinateTransformer::worldToDiagonal(WorldCoordinates{worldPosition});
return {playerId, diagonalWorldCoords.horizontal, chordLengthInDiagonal};
} }
}; };

View file

@ -12,14 +12,11 @@ std::shared_ptr<HoleLayout> HoleLayout::getInstance()
void HoleLayout::clear() void HoleLayout::clear()
{ {
previousHoles.clear();
currentHoles.clear(); currentHoles.clear();
} }
void HoleLayout::lateUpdate() void HoleLayout::lateUpdate()
{ {
// Move history forward
previousHoles = currentHoles;
currentHoles.clear(); currentHoles.clear();
// Collect hole descriptions of active players // Collect hole descriptions of active players
@ -33,7 +30,7 @@ void HoleLayout::lateUpdate()
} }
HoleLayout::HoleLayout() HoleLayout::HoleLayout()
: currentHoles(), previousHoles() : currentHoles()
{} {}
DepthHoleLayout HoleLayout::atDepth(float depth) const DepthHoleLayout HoleLayout::atDepth(float depth) const
@ -41,7 +38,13 @@ DepthHoleLayout HoleLayout::atDepth(float depth) const
std::vector<DepthHoleDescription> depthHoles{}; std::vector<DepthHoleDescription> depthHoles{};
for (const auto &hole: currentHoles) for (const auto &hole: currentHoles)
{ {
depthHoles.push_back(hole.atDepth(depth)); auto holeAtDepth = hole.atDepth(depth);
if (holeAtDepth.width <= 0)
{
continue;
}
depthHoles.push_back(holeAtDepth);
} }
return {depth, depthHoles}; return {depth, depthHoles};
} }

View file

@ -24,7 +24,6 @@ private:
static inline std::shared_ptr<HoleLayout> singletonInstance = nullptr; static inline std::shared_ptr<HoleLayout> singletonInstance = nullptr;
std::vector<HoleDescription> currentHoles; std::vector<HoleDescription> currentHoles;
std::vector<HoleDescription> previousHoles;
}; };

View file

@ -12,7 +12,17 @@ std::map<std::string, LevelConfig> const all_levels = {
{18, 18}, {18, 18},
{0, 18}, {0, 18},
{18, 0}}, { {18, 0}}, {
CollectableInLevel("box", {3, 5}) CollectableInLevel("box", {3, 5}),
CollectableInLevel("box", {4, 5}),
CollectableInLevel("box", {10, 6}),
CollectableInLevel("box", {2, 8}),
CollectableInLevel("box", {1, 2}),
CollectableInLevel("box", {4, 3}),
CollectableInLevel("box", {8, 3}),
CollectableInLevel("box", {6, 7}),
CollectableInLevel("box", {5, 5}),
CollectableInLevel("box", {9, 5}),
CollectableInLevel("box", {7, 4})
}, },
{ {
// Blues // Blues