Files
GDevelop/Extensions/PhysicsBehavior/RuntimeScenePhysicsDatas.cpp
T
Florian Rival a8559bfbbc Add clang-format to format (C++) source files automatically (#491)
* Update all CMakeLists of extensions to use clang-format
* Run clang-format on all Extensions
* Update GDCore CMakeLists.txt to add clang-format
* Run clang-format on GDCore files
* Update GDJS and GDCpp CMakeLists.txt to add clang-format
* Run clang-format on GDCpp and GDJS files
2018-05-09 15:57:38 -07:00

60 lines
1.6 KiB
C++

/**
GDevelop - Physics Behavior Extension
Copyright (c) 2010-2016 Florian Rival (Florian.Rival@gmail.com)
This project is released under the MIT License.
*/
#include "RuntimeScenePhysicsDatas.h"
#include <iostream>
#include "Box2D/Box2D.h"
#include "ContactListener.h"
#include "ScenePhysicsDatas.h"
RuntimeScenePhysicsDatas::RuntimeScenePhysicsDatas(
const ScenePhysicsDatas& behaviorSharedDatas)
: world(new b2World(
b2Vec2(behaviorSharedDatas.gravityX, -behaviorSharedDatas.gravityY),
true)),
contactListener(new ContactListener),
staticBody(NULL),
stepped(false),
scaleX(behaviorSharedDatas.scaleX),
scaleY(behaviorSharedDatas.scaleY),
invScaleX(1 / scaleX),
invScaleY(1 / scaleY),
fixedTimeStep(1.f / 60.f),
maxSteps(5),
totalTime(0) {
world->SetContactListener(contactListener);
world->SetAutoClearForces(false);
b2BodyDef bodyWithoutFixture;
staticBody = world->CreateBody(&bodyWithoutFixture);
}
void RuntimeScenePhysicsDatas::StepWorld(float dt, int v, int p) {
totalTime += dt;
if (totalTime > fixedTimeStep) {
std::size_t numberOfSteps(std::floor(totalTime / fixedTimeStep));
totalTime -= numberOfSteps * fixedTimeStep;
std::size_t numberOfStepToProcess = std::min(numberOfSteps, maxSteps);
for (std::size_t a = 0; a < numberOfStepToProcess; a++) {
world->Step(fixedTimeStep, v, p);
world->ClearForces();
}
}
// Old method
/*world->Step(dt, v, p);
world->ClearForces();*/
}
RuntimeScenePhysicsDatas::~RuntimeScenePhysicsDatas() {
delete world;
delete contactListener;
}