Jump to content

Platform Physics


SpiderPig
 Share

Recommended Posts

Here's a small example on how physics on a moving object can be achieved.  The physics objects are different to their visual counter parts so that they can be calculated without any rotation.  Then using matrix magic the physics position and rotation can be transformed to any moving target.

PlatformPhysics.thumb.jpg.ef07fc4b058c93aaf2ee39df867b170a.jpg

#include "UltraEngine.h"

using namespace UltraEngine;

Vec3 mousepos = Vec3(0.0f);
float move_adjustment = 0.1f;
float move_speed = 1.0f;
float lookspeed = 0.1f;
float looksmoothing = 0.5f;
bool enable_camera = true;

void UpdatePhysics(shared_ptr<Entity> source, shared_ptr<Entity> target, shared_ptr<Entity> platform = nullptr) {
    auto pos = source->GetPosition();
    auto rot = source->GetRotation();

    if (platform != nullptr) {
        auto matrix = platform->GetMatrix();
        matrix = matrix.Inverse();
        matrix.t = Vec4(0, 0, 0, 1);

        pos = TransformPoint(pos, Mat4(), matrix);
        rot = TransformRotation(rot, Mat4(), matrix);
    }

    target->SetPosition(pos);
    target->SetRotation(rot);
}

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 10, -5);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 45, 0);

    auto floor = CreateBox(world, 10.0f, 0.1f, 10.0f);
    floor->SetCollider(nullptr);
    floor->SetPosition(0.0f, -3.0f, 0.0f);
    floor->SetMaterial(LoadMaterial("Materials\\Developer\\grid01.mat"));

    auto box = CreateBox(world);
    box->SetCollider(nullptr);
    box->SetPosition(2.0f, 2.0f, 0.0f);

    auto ref = CreateBox(world);
    ref->SetCollider(nullptr);
    ref->SetPosition(11, 0, 0);

    auto player = CreateCylinder(world);
    player->SetCollider(nullptr);
    player->SetPosition(0.0f, 5.0f, 0.0f);

    auto cam_pivot = CreatePivot(world);
    cam_pivot->SetParent(player);
    cam_pivot->SetPosition(0.0f, 1.8f, 0.0f);

    camera->SetParent(cam_pivot);
    camera->SetPosition(0.0f, 0.0f, -3.0f);
    camera->SetDebugPhysicsMode(true);


    //Create physics objects
    auto f2 = CreatePivot(world);
    auto ff2 = CreateBoxCollider(10.0f, 0.1f, 10.0f);
    f2->SetCollider(ff2);
    f2->SetPosition(0, -3, 0);
    f2->SetCollisionType(COLLISION_SCENE);

    auto b2 = CreatePivot(world);
    auto bb2 = CreateBoxCollider(1.0f, 1.0f, 1.0f);
    b2->SetCollider(bb2);
    b2->SetMass(1.0f);
    b2->SetPosition(2.0f, 2.0f, 0.0f);
    b2->SetCollisionType(COLLISION_PROP);

    //player physics object
    auto player_phys = CreatePivot(world);
    auto cyl = CreateCylinderCollider(0.5f, 1.0f);
    player_phys->SetCollider(cyl);
    player_phys->SetPhysicsMode(PHYSICS_PLAYER);
    player_phys->SetCollisionType(COLLISION_PLAYER);
    player_phys->SetMass(1.0f);
    player_phys->SetPosition(0, 5, 0);

    bool stage = 0;
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_F2) == true) { camera->SetWireframe(!camera->GetWireframe()); }
        if (window->KeyHit(KEY_F3) == true) { camera->SetDebugPhysicsMode(!camera->GetDebugPhysicsMode()); }
        if (window->KeyHit(KEY_F4) == true) { enable_camera = !enable_camera; }

        if (enable_camera) {
            auto _displaySize = window->GetSize();
            float cx = Round((float)_displaySize.x / 2.0f);
            float cy = Round((float)_displaySize.y / 2.0f);

            auto mpos = Vec3(window->GetMousePosition().x, window->GetMousePosition().y, window->GetMousePosition().z);
            window->SetMousePosition(cx, cy);
            mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing);
            auto dx = (mpos.x - cx) * lookspeed;
            auto dy = (mpos.y - cy) * lookspeed;


            auto camrot = cam_pivot->GetRotation();
            camrot.x += dy;
            camrot.y += dx;
            cam_pivot->SetRotation(camrot);
            mousepos = mpos;
        }

        floor->Turn(0.0f, 0.1f, 0.0f);
        
        auto y_angle = cam_pivot->GetRotation().y;
        auto move = 0.0f, strafe = 0.0f;
        if (window->KeyDown(KEY_W)) { move = 1.0f; }
        else if (window->KeyDown(KEY_S)) { move = -1.0f; }
        if (window->KeyDown(KEY_A)) { strafe = -1.0f; }
        else if (window->KeyDown(KEY_D)) { strafe = 1.0f; }
        player_phys->SetInput(y_angle, move, strafe);


        if (window->KeyHit(KEY_P)) { b2->AddForce(0,200,0); }

        UpdatePhysics(b2, box, floor);
        UpdatePhysics(player_phys, player, floor);

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...