I made a small test case. This is the code:
Code:
module edgetest;
import arc.all, blaze.all;
import derelict.sdl.sdl;
void main() {
double pxPerM = 30f;
// create world
bzAABB worldAABB;
worldAABB.lowerBound.set(-100 / pxPerM, -100 / pxPerM);
worldAABB.upperBound.set((800 + 100) / pxPerM, (600 + 100) / pxPerM);
auto gravity = bzVec2(0.0f, 5.0);
bool doSleep = true;
auto world = new bzWorld(worldAABB, gravity, doSleep);
world.continuousPhysics(true);
auto wallShapeDef = new bzEdgeDef;
wallShapeDef.friction = 0.6;
wallShapeDef.restitution = 0.1;
wallShapeDef.vertices.length = 4;
wallShapeDef.vertices[0] = bzVec2(0, 0) / pxPerM;
wallShapeDef.vertices[1] = bzVec2(50, 0) / pxPerM;
wallShapeDef.vertices[2] = bzVec2(50, 400) / pxPerM;
wallShapeDef.vertices[3] = bzVec2(0, 400) / pxPerM;
auto wallBodyDef = new bzBodyDef((bzVec2((800 - 50) / 2, (600 - 400) / 2) / pxPerM), 0);
auto wallBody = world.createBody(wallBodyDef);
wallBody.createShape(wallShapeDef);
// create bullet
auto objectShapeDef = new bzPolyDef;
objectShapeDef.setAsBox(5 / 2 / pxPerM, 5 / 2 / pxPerM);
objectShapeDef.friction = 0.1;
objectShapeDef.restitution = 0.7;
objectShapeDef.density = 0.05;
// create the body definition
auto objectBodyDef = new bzBodyDef(bzVec2.zeroVect, 0);
//objectBodyDef.linearDamping = 1;
objectBodyDef.position = bzVec2(0, 0);
objectBodyDef.angle = 0;
objectBodyDef.isBullet = true;
objectBodyDef.fixedRotation = true;
// create the body
auto objectBody = world.createBody(objectBodyDef);
// create the shape
objectBody.createShape(objectShapeDef);
objectBody.setMassFromShapes;
arc.window.open("edgetest", 800, 600, false);
arc.input.open;
Point vec2point(bzVec2 v) {
return Point(v.x, v.y);
}
bzVec2 point2vec(Point v) {
return bzVec2(v.x, v.y);
}
Point start;
arc.window.clear;
bool running = true;
while(running) {
// Process time
processTime;
world.step(1f / 30f, 10, 8);
objectBody.unFreeze;
objectBody.wakeup;
drawRectangle(vec2point(wallBody.position) * pxPerM, Size(50, 400), Color.White, false);
drawRectangle(vec2point(objectBody.position - bzVec2(5f / 2f, 5f / 2f)) * pxPerM, Size(5, 5), Color.Red, false);
arc.input.process;
if(arc.input.keyPressed(ARC_QUIT) || arc.input.keyPressed(ARC_ESCAPE)) running = false;
if(arc.input.keyPressed(ARC_PRINT)) arc.window.screenshot("screenshot");
if(arc.input.keyPressed(ARC_PAUSE)) {
do {
arc.input.process;
} while(!arc.input.keyPressed(ARC_PAUSE));
}
if(arc.input.mouseButtonPressed(LEFT)) {
start = arc.input.mousePos;
} else if(arc.input.mouseButtonReleased(LEFT)) {
objectBody.xf.position = point2vec(start) / pxPerM;
Point end = arc.input.mousePos;
objectBody.applyForce(point2vec((end - start) / pxPerM), objectBody.position);
start = Point.zeroVect;
}
if(start != Point.zeroVect) {
drawLine(start, arc.input.mousePos, Color.Green);
}
// switch to next window frame
arc.window.swap;
//arc.window.clear;
// Limit fps
limitFPS(FRAMERATE);
}
// close window when done with it
scope(exit) {
arc.font.close;
arc.input.close;
arc.window.close;
}
}
//current time
uint startOfFrameTime = 0;
//previous time
uint prevStartOfFrameTime = 0;
//Frames per second
uint fps_ = 0;
//helpers for fps calculation
const uint FRAMERATE = 30;
uint frames = 0;
uint msPassed = 0;
// Code to keep frame rate constant
/**
Calculates fps and captures start of frame time.
Call at the start of the frame loop.
**/
void processTime()
{
if (startOfFrameTime == 0) {
startOfFrameTime = SDL_GetTicks();
prevStartOfFrameTime = startOfFrameTime - 1;
}
prevStartOfFrameTime = startOfFrameTime;
startOfFrameTime = SDL_GetTicks();
frames++;
msPassed += (startOfFrameTime - prevStartOfFrameTime);
if (msPassed > 1000) {
fps_ = frames;
frames = 0;
msPassed = 0;
}
}
void limitFPS(uint maxFps)
{
int targetMsPerFrame = 1000 / maxFps;
uint cTime = SDL_GetTicks();
int sleepAmount = targetMsPerFrame - (cTime - startOfFrameTime);
if (sleepAmount <= 0)
return;
SDL_Delay(sleepAmount);
}
the screenshot is attached.