setzte das in c++ um: (2d physic engine) "Objektklasse": Attribute: Masse Form Material…
- User
setzte das in c++ um: (2d physic engine)
"Objektklasse": Attribute: Masse Form Material Koordinaten Bewegungsvektor Letzte Kollision Fest auf der Stelle, ja oder nein? Loop die die Physik jeden Tick macht: (hauptfunktion) Alle Objekte um ihren Bewegungsvektor bewegen. Bewegungsvektoren nach Gravität passend verändern (Grabititätsvektor addieren, bzw generieren falls Anziehung von einem Punkt kommt. Checken ob zwei Objekte kollidieren ->Kollidierungsfunktion aufrufen Ggf. Reibung abziehen. Kollisionsfunktion Assistant
#include <iostream> #include <vector> #include <cmath> // 2D-Vektor für Koordinaten und Bewegung struct Vector2D { float x; float y; Vector2D() : x(0.0f), y(0.0f) {} Vector2D(float x, float y) : x(x), y(y) {} Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); } Vector2D operator*(float scalar) const { return Vector2D(x * scalar, y * scalar); } float magnitude() const { return std::sqrt(x*x + y*y); } Vector2D normalize() const { float mag = magnitude(); if (mag > 0) { return Vector2D(x / mag, y / mag); } return *this; } }; // Materialeigenschaften enum class Material { WOOD, METAL, RUBBER, GLASS, CONCRETE }; // Formtypen enum class ShapeType { CIRCLE, RECTANGLE }; // Formrepräsentation struct Shape { ShapeType type; union { struct { float radius; } circle; struct { float width; float height; } rectangle; }; static Shape Circle(float radius) { Shape shape; shape.type = ShapeType::CIRCLE; shape.circle.radius = radius; return shape; } static Shape Rectangle(float width, float height) { Shape shape; shape.type = ShapeType::RECTANGLE; shape.rectangle.width = width; shape.rectangle.height = height; return shape; } }; // Kollisionsinformation struct Collision { bool occurred; Vector2D point; Vector2D normal; Collision() : occurred(false) {} }; // Hauptklasse für physikalische Objekte class PhysicsObject { private: float mass; Shape shape; Material material; Vector2D position; Vector2D velocity; Collision lastCollision; bool isStatic; public: PhysicsObject(float mass, Shape shape, Material material, Vector2D position, Vector2D velocity, bool isStatic) : mass(mass), shape(shape), material(material), position(position), velocity(velocity), isStatic(isStatic) {} float getMass() const { return mass; } const Shape& getShape() const { return shape; } Material getMaterial() const { return material; } Vector2D getPosition() const { return position; } Vector2D getVelocity() const { return velocity; } bool getIsStatic() const { return isStatic; } const Collision& getLastCollision() const { return lastCollision; } void setVelocity(const Vector2D& vel) { velocity = vel; } void setPosition(const Vector2D& pos) { position = pos; } void setLastCollision(const Collision& collision) { lastCollision = collision; } // Bewegung des Objekts void move(float deltaTime) { if (!isStatic) { position = position + velocity * deltaTime; } } // Anwendung der Gravitation void applyGravity(const Vector2D& gravity, float deltaTime) { if (!isStatic) { velocity = velocity + gravity * deltaTime; } } // Anwendung der Reibung void applyFriction(float friction, float deltaTime) { if (!isStatic && velocity.magnitude() > 0) { Vector2D frictionForce = velocity.normalize() * (-friction); velocity = velocity + frictionForce * deltaTime; // Stoppen bei sehr kleiner Geschwindigkeit if (velocity.magnitude() < 0.01f) { velocity = Vector2D(0, 0); } } } }; class PhysicsEngine { private: std::vector<PhysicsObject> objects; Vector2D gravity; float deltaTime; // Kollisionserkennung zwischen zwei Objekten bool checkCollision(PhysicsObject& a, PhysicsObject& b, Collision& collision) { // Hier würde die komplexe Kollisionserkennung implementiert // Dies ist eine vereinfachte Version für Kreise if (a.getShape().type == ShapeType::CIRCLE && b.getShape().type == ShapeType::CIRCLE) { Vector2D posA = a.getPosition(); Vector2D posB = b.getPosition(); float radiusA = a.getShape().circle.radius; float radiusB = b.getShape().circle.radius; Vector2D delta = Vector2D(posB.x - posA.x, posB.y - posA.y); float distance = delta.magnitude(); if (distance < radiusA + radiusB) { collision.occurred = true; collision.normal = delta.normalize(); collision.point = posA + collision.normal * radiusA; return true; } } return false; } // Kollisionsauflösung void resolveCollision(PhysicsObject& a, PhysicsObject& b, const Collision& collision) { if (a.getIsStatic() && b.getIsStatic()) return; // Impulsbasierte Kollisionsauflösung Vector2D relativeVelocity = b.getVelocity() - a.getVelocity(); float velAlongNormal = relativeVelocity.x * collision.normal.x + relativeVelocity.y * collision.normal.y; if (velAlongNormal > 0) return; float restitution = 0.5f; // Elastizität float j = -(1.0f + restitution) * velAlongNormal; if (!a.getIsStatic() && !b.getIsStatic()) { j /= (1/a.getMass() + 1/b.getMass()); Vector2D impulse = collision.normal * j; a.setVelocity(a.getVelocity() - impulse * (1/a.getMass())); b.setVelocity(b.getVelocity() + impulse * (1/b.getMass())); } else if (a.getIsStatic()) { Vector2D impulse = collision.normal * j * b.getMass(); b.setVelocity(b.getVelocity() + impulse * (1/b.getMass())); } else if (b.getIsStatic()) { Vector2D impulse = collision.normal * j * a.getMass(); a.setVelocity(a.getVelocity() - impulse * (1/a.getMass())); } // Setzen der letzten Kollision a.setLastCollision(collision); Collision reverseCollision = collision; reverseCollision.normal = Vector2D(-collision.normal.x, -collision.normal.y); b.setLastCollision(reverseCollision); } public: PhysicsEngine(const Vector2D& gravity = Vector2D(0, 9.81f), float deltaTime = 1/60.0f) : gravity(gravity), deltaTime(deltaTime) {} void addObject(const PhysicsObject& object) { objects.push_back(object); } // Hauptfunktion für die Physik-Simulation void update() { // 1. Alle Objekte bewegen for (auto& obj : objects) { obj.move(deltaTime); } // 2. Gravitation anwenden for (auto& obj : objects) { obj.applyGravity(gravity, deltaTime); } // 3. Kollisionen prüfen und auflösen for (size_t i = 0; i < objects.size(); i++) { for (size_t j = i + 1; j < objects.size(); j++) { Collision collision; if (checkCollision(objects[i], objects[j], collision)) { resolveCollision(objects[i], objects[j], collision); } } } // 4. Reibung anwenden for (auto& obj : objects) { // Reibungskoeffizient je nach Material float friction = 0.2f; switch (obj.getMaterial()) { case Material::WOOD: friction = 0.3f; break; case Material::METAL: friction = 0.15f; break; case Material::RUBBER: friction = 0.8f; break; case Material::GLASS: friction = 0.1f; break; case Material::CONCRETE: friction = 0.7f; break; } obj.applyFriction(friction, deltaTime); } } const std::vector<PhysicsObject>& getObjects() const { return objects; } }; // Beispiel für die Verwendung int main() { PhysicsEngine engine(Vector2D(0, 9.81f)); // Ball erstellen PhysicsObject ball(1.0f, Shape::Circle(0.5f), Material::RUBBER, Vector2D(0, 0), Vector2D(2, 0), false); // Boden erstellen PhysicsObject floor(0.0f, Shape::Rectangle(10.0f, 1.0f), Material::CONCRETE, Vector2D(0, 5), Vector2D(0, 0), true); engine.addObject(ball); engine.addObject(floor); // Simulation für einige Schritte for (int i = 0; i < 300; i++) { engine.update(); // Hier könnte Rendering oder Ausgabe erfolgen if (i % 60 == 0) { std::cout << "Simulation Schritt " << i << std::endl; } } return 0; }Möchtest du, dass ich den Code erkläre oder einzelne Teile davon erläutere?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.