Skip to content

Physics

ural89 edited this page Jan 18, 2026 · 3 revisions

Physics

Creating physics polygon

First create an empty project as explained in [Create Your First Project] https://github.com/ural89/ConsoleCraftEngine/wiki/Quick-Start-Guide

in EntryScene.h add polygon creator ptr:

private:
  void OnInput(int input);
  PolygonCreator* m_PolygonCreator; //Add this line

in EntryScene.cpp write these lines into Init() method:

void EntryScene::Init()
{
    // AddGameObject(new ExampleGameObject(*this), Vector2(5,5)); //delete or comment out this line
    m_PolygonCreator = new PolygonCreator(*this); 
    m_PolygonCreator->SetEditingActive(true); //this will enable polygon creation mode.


    Vector2 rectPos = Vector2(5,5);
    Vector2 rectSize = Vector2(6, 6);
    float rectRotation = 0;
    int color = 2;
    m_PolygonCreator->CreateRectanglePolygon({rectPos, rectSize, rectRotation}, color); //place a polygon into scene

This code will add a rectangle into the scene. It will move with physics.

    m_PolygonCreator->SetEditingActive(true);

This line will give you ability to draw polygons into the scene. You can move cursor with WASD keys. Press E to place a point.

Press Space Bar to confirm your polygon:

Adding joints

You can create joints between polygons. Create two polygons and add a joint between them:

void EntryScene::Init()
{
    m_PolygonCreator = new PolygonCreator(*this); 
    m_PolygonCreator->SetEditingActive(true);
    Vector2 rectPos = Vector2(10,10);
    Vector2 rectSize = Vector2(6, 6);
    float rectRotation = 0;
    int color = 2;
    auto first = m_PolygonCreator->CreateRectanglePolygon({rectPos, rectSize, rectRotation}, color);
    auto second = m_PolygonCreator->CreateRectanglePolygon({Vector2(7, 0), rectSize, rectRotation}, color);

//Create joints
    JointParams jointParams;
    jointParams.firstPolygon = first;
    jointParams.secondPolygon = second;
    jointParams.length = 15;
    jointParams.stiffness = 1000;
    jointParams.maxLength = 25;
    AddJoint(jointParams); //you can call this from a scene class
}

To see joints you can use JointRenderer: in your scene header:

#pragma once
#include "Core/Physics/Polygon/PolygonCreator/PolygonCreator.h"
#include "Core/Scene.h"
#include "Physics/JointRenderer.h"

class EntryScene : public Scene {
public:
  void Init() override;
  void Update(float deltaTime) override;

private:
  void OnInput(int input);
  PolygonCreator *m_PolygonCreator;
  JointRenderer *jointRenderer;
};

and in cpp:

#include "EntryScene.h"
#include "Physics/JointRenderer.h"
#include "Scene.h"

// #include "GameObjects/ExampleGameObject.h"

void EntryScene::Init()
{
    m_PolygonCreator = new PolygonCreator(*this); 
    m_PolygonCreator->SetEditingActive(true);
    Vector2 rectPos = Vector2(10,10);
    Vector2 rectSize = Vector2(6, 6);
    float rectRotation = 0;
    int color = 2;
    auto first = m_PolygonCreator->CreateRectanglePolygon({rectPos, rectSize, rectRotation}, color);
    auto second = m_PolygonCreator->CreateRectanglePolygon({Vector2(7, 0), rectSize, rectRotation}, color);
    JointParams jointParams;
    jointParams.firstPolygon = first;
    jointParams.secondPolygon = second;
    jointParams.length = 15;
    jointParams.stiffness = 1000;
    jointParams.maxLength = 25;
    AddJoint(jointParams);
    jointRenderer = new JointRenderer(*this); //initialize joint renderer with class parameter
}

void EntryScene::Update(float deltaTime)
{
    Scene::Update(deltaTime);
    jointRenderer->Update(deltaTime); //add this to update to render realtime
}

Clone this wiki locally