Adding generic Polygon fixture support
[qml-box2d:dakerfps-qml-box2d.git] / box2dfixture.cpp
1 /*
2  * Box2D QML plugin
3  * Copyright (C) 2010 Nokia Corporation
4  *
5  * This file is part of the Box2D QML plugin.
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  * License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library;  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "box2dfixture.h"
22
23 #include "box2dworld.h"
24
25 #include <Box2D.h>
26
27 Box2DFixture::Box2DFixture(QDeclarativeItem *parent) :
28     QDeclarativeItem(parent),
29     mFixture(0),
30     mDensity(0.0f),
31     mFriction(0.2f),
32     mRestitution(0.0f),
33     mSensor(false)
34 {
35 }
36
37 void Box2DFixture::setDensity(float density)
38 {
39     if (mDensity == density)
40         return;
41
42     mDensity = density;
43     if (mFixture)
44         mFixture->SetDensity(density);
45     emit densityChanged();
46 }
47
48 void Box2DFixture::setFriction(float friction)
49 {
50     if (mFriction == friction)
51         return;
52
53     mFriction = friction;
54     if (mFixture)
55         mFixture->SetFriction(friction);
56     emit frictionChanged();
57 }
58
59 void Box2DFixture::setRestitution(float restitution)
60 {
61     if (mRestitution == restitution)
62         return;
63
64     mRestitution = restitution;
65     if (mFixture)
66         mFixture->SetRestitution(restitution);
67     emit restitutionChanged();
68 }
69
70 void Box2DFixture::setSensor(bool sensor)
71 {
72     if (mSensor == sensor)
73         return;
74
75     mSensor = sensor;
76     if (mFixture)
77         mFixture->SetSensor(sensor);
78     emit sensorChanged();
79 }
80
81 void Box2DFixture::createFixture(b2Body *body)
82 {
83     b2Shape *shape = createShape();
84
85     b2FixtureDef fixtureDef;
86     fixtureDef.shape = shape;
87     fixtureDef.density = mDensity;
88     fixtureDef.friction = mFriction;
89     fixtureDef.restitution = mRestitution;
90     fixtureDef.isSensor = mSensor;
91
92     mFixture = body->CreateFixture(&fixtureDef);
93     mFixture->SetUserData(this);
94
95     delete shape;
96 }
97
98 void Box2DFixture::emitBeginContact(Box2DFixture *other)
99 {
100     emit beginContact(other);
101 }
102
103 void Box2DFixture::emitContactChanged(Box2DFixture *other)
104 {
105     emit contactChanged(other);
106 }
107
108 void Box2DFixture::emitEndContact(Box2DFixture *other)
109 {
110     emit endContact(other);
111 }
112
113
114 b2Shape *Box2DBox::createShape()
115 {
116     const float32 _x = x() / scaleRatio;
117     const float32 _y = -y() / scaleRatio;
118
119     b2Vec2 vertices[4];
120     vertices[0].Set(_x, _y);
121     vertices[1].Set(_x, _y - height() / scaleRatio);
122     vertices[2].Set(_x + width() / scaleRatio, _y - height() / scaleRatio);
123     vertices[3].Set(_x + width() / scaleRatio, _y);
124     int32 count = 4;
125
126     b2PolygonShape *shape = new b2PolygonShape;
127     shape->Set(vertices, count);
128     return shape;
129 }
130
131
132 b2Shape *Box2DCircle::createShape()
133 {
134     b2CircleShape *shape = new b2CircleShape;
135     shape->m_radius = mRadius / scaleRatio;
136     shape->m_p.Set(x() / scaleRatio, -y() / scaleRatio);
137     return shape;
138 }
139
140 b2Shape *Box2DPolygon::createShape()
141 {
142     const QVariantList &vertices_list = m_vertices.toList();
143     int count = vertices_list.length();
144     b2Vec2 vertices[count];
145     int i = 0;
146     foreach (const QVariant &variant_point, vertices_list) {
147         const QPointF &point = variant_point.toPointF();
148         vertices[i].Set(point.x() / scaleRatio, point.y() / scaleRatio);
149         i++;
150     }
151
152     b2PolygonShape *shape = new b2PolygonShape;
153     shape->Set(vertices, count);
154     return shape;
155 }