forked from qml-box2d/qml-box2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made simple Box2D rectangular bodies functional
Defined the Body item, which can only be used as a child of a World item. The standard x, y, width and height properties can be used to define its position and size, and it will automatically determine its mass. The World item has been extended with properties to set the time step, the number of iterations for each time step and the time between each frame.
- Loading branch information
Showing
7 changed files
with
248 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Box2D QML plugin | ||
* Copyright (C) 2010 Nokia Corporation | ||
* | ||
* This file is part of the QmlArcade. | ||
* | ||
* QmlArcade is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the Free | ||
* Software Foundation; either version 2 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* QmlArcade is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with QmlArcade. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "box2dbody.h" | ||
|
||
#include "box2dworld.h" | ||
|
||
#include <Box2D.h> | ||
#include <cmath> | ||
|
||
static const float scaleRatio = 32.0f; // 32 pixels in one meter | ||
|
||
Box2DBody::Box2DBody(QDeclarativeItem *parent) : | ||
QDeclarativeItem(parent), | ||
mBody(0), | ||
mFixed(false) | ||
{ | ||
} | ||
|
||
Box2DBody::~Box2DBody() | ||
{ | ||
if (Box2DWorld *world = qobject_cast<Box2DWorld*>(parent())) | ||
world->unregisterBody(this); | ||
} | ||
|
||
void Box2DBody::componentComplete() | ||
{ | ||
if (Box2DWorld *world = qobject_cast<Box2DWorld*>(parent())) | ||
world->registerBody(this); | ||
else | ||
qWarning() << "Warning: Body should have World as parent"; | ||
} | ||
|
||
void Box2DBody::initialize(b2World *world) | ||
{ | ||
b2BodyDef bodyDef; | ||
bodyDef.position.Set((x() + width() / 2) / scaleRatio, | ||
-(y() + height() / 2) / scaleRatio); | ||
bodyDef.angle = -(rotation() * (2 * M_PI)) / 360.0; | ||
|
||
mBody = world->CreateBody(&bodyDef); | ||
|
||
// TODO: Create a shape for each child shape | ||
b2PolygonDef shapeDef; | ||
shapeDef.SetAsBox(width() / 2.0f / scaleRatio, | ||
height() / 2.0f / scaleRatio); | ||
shapeDef.density = 1.0f; // TODO: Make a property for it in Box2DShape | ||
shapeDef.friction = 0.3f; // TODO: Make a property for it in Box2DShape | ||
shapeDef.restitution = 0.5f; | ||
mBody->CreateShape(&shapeDef); | ||
|
||
if (!mFixed) | ||
mBody->SetMassFromShapes(); // TODO: Make a property for it in Box2DBody | ||
} | ||
|
||
/** | ||
* Synchronizes the state of this body with the internal Box2D state. | ||
*/ | ||
void Box2DBody::synchronize() | ||
{ | ||
Q_ASSERT(mBody); | ||
const b2Vec2 position = mBody->GetPosition(); | ||
const float32 angle = mBody->GetAngle(); | ||
setPos(position.x * scaleRatio - width() / 2, | ||
-position.y * scaleRatio - height() / 2); | ||
setRotation(-(angle * 360.0) / (2 * M_PI)); | ||
} | ||
|
||
void Box2DBody::cleanup(b2World *world) | ||
{ | ||
world->DestroyBody(mBody); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Box2D QML plugin | ||
* Copyright (C) 2010 Nokia Corporation | ||
* | ||
* This file is part of the QmlArcade. | ||
* | ||
* QmlArcade is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by the Free | ||
* Software Foundation; either version 2 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* QmlArcade is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with QmlArcade. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef BOX2DBODY_H | ||
#define BOX2DBODY_H | ||
|
||
#include <QDeclarativeItem> | ||
|
||
class Box2DWorld; | ||
|
||
class b2Body; | ||
class b2World; | ||
|
||
/** | ||
* The Box2D body, build up from a list of shapes. | ||
*/ | ||
class Box2DBody : public QDeclarativeItem | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(bool fixed READ fixed WRITE setFixed) | ||
|
||
public: | ||
explicit Box2DBody(QDeclarativeItem *parent = 0); | ||
~Box2DBody(); | ||
|
||
bool fixed() const | ||
{ return mFixed; } | ||
|
||
void setFixed(bool fixed) | ||
{ mFixed = fixed; } | ||
|
||
void componentComplete(); | ||
|
||
void initialize(b2World *world); | ||
void synchronize(); | ||
void cleanup(b2World *world); | ||
|
||
private: | ||
bool mFixed; | ||
b2Body *mBody; | ||
}; | ||
|
||
#endif // BOX2DBODY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters