123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /*
- * 2D Physic Engine
- * app.cpp: basic application handling.
- * Based on pikuma.com Learn Game Physics Engine Programming course.
- * Copyright (c) 2022 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 07/06/2022.
- */
- #include <app.h>
- #include <graphics.h>
- #include <imgui.h>
- #include <imgui_sdl.h>
- #include <imgui_impl_sdl.h>
- #include <physics/constants.h>
- #include <physics/body.h>
- #include <physics/vec2.h>
- #include <physics/force.h>
- #include <physics/shape.h>
- #include <physics/shapes/circle.h>
- #include <SDL.h>
- void application::parseParameters(int argc, char *argv[])
- {
- // Nothing to do for now
- }
- void application::setup()
- {
- this->running = graphics::openWindow();
- this->millisecsPreviousFrame = SDL_GetTicks();
- this->anchor = vec2(graphics::width() / 2.0,30);
- this->bodies.push_back(new body(circleShape(50), graphics::width() / 2.0, graphics::height() / 2.0,
- 2.0));
- }
- void application::input()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- //ImGui_ImplSDL2_ProcessEvent(&event);
- //ImGuiIO &io = ImGui::GetIO();
- switch(event.type)
- {
- case SDL_QUIT:
- this->running = false;
- break;
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_1)
- {
- this->showVelocity ^= true;
- }
- if (event.key.keysym.sym == SDLK_2)
- {
- this->showForceSum ^= true;
- }
- if (event.key.keysym.sym == SDLK_3)
- {
- this->showAllForces ^= true;
- }
- if (event.key.keysym.sym == SDLK_ESCAPE)
- {
- this->running = false;
- }
- if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
- if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
- if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
- if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
- break;
- case SDL_KEYUP:
- if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
- if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
- if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
- if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
- break;
- case SDL_MOUSEBUTTONDOWN:
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- this->leftButtonPressed = true;
- }
- break;
- case SDL_MOUSEBUTTONUP:
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- if (this->leftButtonPressed)
- {
- vec2 impulseDirection = (bodies[0]->position - this->mouseCursor).unitVector();
- double impulseMagnitude = (bodies[0]->position - this->mouseCursor).magnitude() * 5.0;
- bodies[0]->velocity = impulseDirection * impulseMagnitude;
- }
- this->leftButtonPressed = false;
- }
- break;
- case SDL_MOUSEMOTION:
- this->mouseCursor.x = event.motion.x;
- this->mouseCursor.y = event.motion.y;
- break;
- }
- }
- }
- void application::syncAndDeltatime()
- {
- waitTime.start();
- /* Let's make sure we are running at the expected framerate */
- uint32_t now = SDL_GetTicks();
- int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
- if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
- {
- SDL_Delay(timeToWait);
- }
- this->deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
- /* clamp maximum deltatime to avoid weird behaviours */
- if (this->deltaTime > 0.016)
- {
- this->deltaTime = 0.016;
- }
- this->millisecsPreviousFrame = SDL_GetTicks();
- waitTime.stop();
- }
- void application::update()
- {
- this->syncAndDeltatime();
- updateTime.start();
- for (auto part:this->bodies)
- {
- part->addForce(pushForce);
- vec2 friction = force::generateFrictionForce(*part, 20);
- part->addForce(friction);
- vec2 drag = force::generateDragForce(*part, 0.001);
- part->addForce(drag);
- vec2 weight = vec2(0, part->mass * 9.81 * PIXELS_PER_METER);
- part->addForce(weight);
- }
- vec2 spring = force::generateSpringForce(*this->bodies[0],
- this->anchor, this->restLenght, this->k);
- this->bodies[0]->addForce(spring);
- for (auto bod:this->bodies)
- {
- bod->integrate(deltaTime);
- }
- for (auto bod:this->bodies)
- {
- if (bod->shp->getType() == SHAPE_CIRCLE) {
- // UGLY HACK
- circleShape *shp = (circleShape *) bod->shp;
- if (((bod->position.y - shp->radius) <= 0) ||
- ((bod->position.y + shp->radius) >= graphics::height()))
- {
- if ((bod->position.y - shp->radius) <= 0)
- {
- bod->position.y = shp->radius;
- }
- else
- {
- bod->position.y = graphics::height() - shp->radius;
- }
- bod->velocity.y = -bod->velocity.y;
- }
- if (((bod->position.x - shp->radius) <= 0) ||
- ((bod->position.x + shp->radius) >= graphics::width()))
- {
- if ((bod->position.x - shp->radius) <= 0)
- {
- bod->position.x = shp->radius;
- }
- else
- {
- bod->position.x = graphics::width() - shp->radius;
- }
- bod->velocity.x = -bod->velocity.x;
- }
- }
- }
- updateTime.stop();
- }
- void application::render()
- {
- renderTime.start();
- graphics::clearScreen(0xFF0F0721);
- if (this->leftButtonPressed)
- {
- graphics::draw::line(this->bodies[0]->position.x, this->bodies[0]->position.y,
- this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
- }
- graphics::draw::line(this->anchor.x, this->anchor.y,
- this->bodies[0]->position.x, this->bodies[0]->position.y,
- 0xFF313131);
- graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
- for(auto bod: this->bodies)
- {
- bod->rotation += 0.01;
- bod->shp->draw(*bod);
- }
- for(auto bod: this->bodies)
- {
- bod->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
- }
- graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
- graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
- graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
- graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f",
- 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
- graphics::draw::text(5, 53, 0x11FF22, "DBG: V:%c S:%c A:%c", this->showVelocity ? 'Y':'N',
- this->showForceSum ? 'Y':'N', this->showAllForces ? 'Y':'N');
- graphics::renderFrame();
- renderTime.stop();
- }
- void application::destroy()
- {
- // Nothing for now.
- for (auto part:this->bodies)
- {
- delete part;
- }
- graphics::closeWindow();
- }
|