From kde-commits Sun Oct 31 23:23:46 2010 From: Stefan Majewsky Date: Sun, 31 Oct 2010 23:23:46 +0000 To: kde-commits Subject: KDE/kdegames/kolf Message-Id: <20101031232346.04D26AC8A8 () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=128856759212998 SVN commit 1191720 by majewsky: Milestone! The intro level works with Box2D-powered ball propagation! M +0 -1 ball.cpp M +8 -8 canvasitem.cpp M +1 -1 external/Box2D/Common/b2Settings.h M +7 -1 game.cpp --- trunk/KDE/kdegames/kolf/ball.cpp #1191719:1191720 @@ -109,7 +109,6 @@ void Ball::setVelocity(const Vector& velocity) { CanvasItem::setPhysicalVelocity(velocity); - kDebug() << velocity; } #endif --- trunk/KDE/kdegames/kolf/canvasitem.cpp #1191719:1191720 @@ -29,11 +29,12 @@ : game(0) , m_body(0) , m_overlay(0) - , m_simulationType(CanvasItem::CollisionSimulation) + , m_simulationType((CanvasItem::SimulationType) -1) { b2BodyDef bodyDef; bodyDef.userData = this; m_body = world->CreateBody(&bodyDef); + setSimulationType(CanvasItem::CollisionSimulation); } CanvasItem::~CanvasItem() @@ -133,7 +134,7 @@ QPointF CanvasItem::physicalVelocity() const { b2Vec2 v = m_body->GetLinearVelocity(); - return QPointF(v.x, v.y) / Kolf::Box2DScaleFactor; + return QPointF(v.x, v.y); } void CanvasItem::setPhysicalVelocity(const QPointF& newVelocity) @@ -142,16 +143,15 @@ if (newVelocity != currentVelocity) { const qreal mass = m_body->GetMass(); - if (mass == 0) + //WARNING: Velocities are NOT scaled. The timestep is scaled, instead. + //See where b2World::Step() gets called for more info. + if (mass == 0 || m_simulationType != CanvasItem::DynamicSimulation) { - m_body->SetLinearVelocity(b2Vec2( - newVelocity.x() * Kolf::Box2DScaleFactor, - newVelocity.y() * Kolf::Box2DScaleFactor - )); + m_body->SetLinearVelocity(b2Vec2(newVelocity.x(), newVelocity.y())); } else { - const QPointF impulse = (newVelocity - currentVelocity) * mass * Kolf::Box2DScaleFactor; + const QPointF impulse = (newVelocity - currentVelocity) * mass; m_body->ApplyLinearImpulse(b2Vec2(impulse.x(), impulse.y()), m_body->GetPosition()); } } --- trunk/KDE/kdegames/kolf/external/Box2D/Common/b2Settings.h #1191719:1191720 @@ -99,7 +99,7 @@ /// A velocity threshold for elastic collisions. Any collision with a relative linear /// velocity below this threshold will be treated as inelastic. -#define b2_velocityThreshold 1.0f +#define b2_velocityThreshold 0.0f /// The maximum linear position correction used when solving constraints. This helps to /// prevent overshoot. --- trunk/KDE/kdegames/kolf/game.cpp #1191719:1191720 @@ -1639,6 +1639,7 @@ void Wall::setVisible(bool yes) { QGraphicsLineItem::setVisible(yes); + setSimulationType(yes ? CanvasItem::CollisionSimulation : CanvasItem::NoSimulation); startItem->setVisible(yes); endItem->setVisible(yes); @@ -2740,7 +2741,12 @@ } } //step world - const double timeStep = 1.0; //so that CanvasItem::physicalVelocity() corresponds to the position change per step + //NOTE: I previously set timeStep to 1.0 so that CItem's physicalVelocity() + //corresponds to the position change per step. In this case, the physical + //velocity would be scaled by Kolf::Box2DScaleFactor, which would result in + //very small velocities (below Box2D's internal cutoff thresholds!) for + //usual movements. Therefore, we apply the scaling to the timestep instead. + const double timeStep = 1.0 * Kolf::Box2DScaleFactor; g_world->Step(timeStep, 10, 10); //parameters 2/3 = iteration counts (TODO: optimize) //conclude simulation for (b2Body* body = g_world->GetBodyList(); body; body = body->GetNext())