From kde-commits Sun Oct 31 23:23:35 2010 From: Stefan Majewsky Date: Sun, 31 Oct 2010 23:23:35 +0000 To: kde-commits Subject: KDE/kdegames/kolf Message-Id: <20101031232335.D3564AC8AA () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=128856759012994 SVN commit 1191715 by majewsky: Kill Ball::vector property. M +11 -31 ball.cpp M +3 -9 ball.h M +27 -35 game.cpp --- trunk/KDE/kdegames/kolf/ball.cpp #1191714:1191715 @@ -76,35 +76,24 @@ { if (state == Stopped || state == Holed || !isVisible()) { - setVelocity(Vector()); + setVelocity(QPointF()); return; } const double subtractAmount = .027 * frictionMultiplier; - if (m_vector.magnitude() <= subtractAmount) + Vector velocity = this->velocity(); + if (velocity.magnitude() <= subtractAmount) { state = Stopped; - setVelocity(Vector()); + setVelocity(QPointF()); game->timeout(); return; } - m_vector.setMagnitude(m_vector.magnitude() - subtractAmount); - setVector(m_vector); + velocity.setMagnitude(velocity.magnitude() - subtractAmount); + setVelocity(velocity); frictionMultiplier = 1.0; } -void Ball::setVelocity(const Vector& velocity) -{ - CanvasItem::setVelocity(velocity); - m_vector = QPointF(velocity.x(), -velocity.y()); -} - -void Ball::setVector(const Vector& newVector) -{ - m_vector = newVector; - CanvasItem::setVelocity(Vector(newVector.x(), -newVector.y())); -} - void Ball::moveBy(double dx, double dy) { EllipticalCanvasItem::moveBy(dx, dy); @@ -132,9 +121,8 @@ if (m_collisionId == 1 && !velocity().isNull()) friction(); - double initialVector = m_vector.magnitude(); + const double initialVelocity = velocity().magnitude(); const double minSpeed = .06; - bool justCollidedWithWall = false; QList items = collidingItems(); @@ -178,24 +166,16 @@ } } - double vectorChange = initialVector - m_vector.magnitude(); - if(vectorChange < 0 ) - vectorChange *= -1; + const double currentVelocity = velocity().magnitude(); + const double velocityChange = qAbs(initialVelocity - currentVelocity); - if(m_vector.magnitude() < minSpeed && vectorChange < minSpeed && m_vector.magnitude()) + if(currentVelocity < minSpeed && velocityChange < minSpeed && currentVelocity) { - if( justCollidedWithWall ) - { //don't want to stop if just hit a wall as may be in the wall - //problem: could this cause endless ball bouncing between 2 walls? - m_vector.setMagnitude(minSpeed); - } - else - { + //cutoff low velocities setVelocity(Vector()); setState(Stopped); } } -} BallState Ball::currentState() { --- trunk/KDE/kdegames/kolf/ball.h #1191714:1191715 @@ -36,7 +36,6 @@ virtual void doAdvance(); virtual void moveBy(double dx, double dy); - virtual void setVelocity(const Vector& velocity); virtual bool deleteable() const { return false; } @@ -53,9 +52,9 @@ void collisionDetect(); int addStroke() const { return m_addStroke; } - bool placeOnGround(Vector &v) { v = oldVector; return m_placeOnGround; } + bool placeOnGround(Vector &v) { v = m_pogOldVelocity; return m_placeOnGround; } void setAddStroke(int newStrokes) { m_addStroke = newStrokes; } - void setPlaceOnGround(bool placeOnGround) { m_placeOnGround = placeOnGround; oldVector = m_vector; } + void setPlaceOnGround(bool placeOnGround) { m_placeOnGround = placeOnGround; m_pogOldVelocity = velocity(); } bool beginningOfHole() const { return m_beginningOfHole; } void setBeginningOfHole(bool yes) { m_beginningOfHole = yes; } @@ -63,9 +62,6 @@ bool forceStillGoing() const { return m_forceStillGoing; } void setForceStillGoing(bool yes) { m_forceStillGoing = yes; } - Vector curVector() const { return m_vector; } - void setVector(const Vector &newVector); - void shotStarted() { maxBumperBounceSpeed = 8; } void setDoDetect(bool yes) { m_doDetect = yes; } @@ -93,13 +89,11 @@ int m_addStroke; bool m_placeOnGround; + Vector m_pogOldVelocity; bool m_beginningOfHole; bool m_forceStillGoing; - Vector m_vector; - Vector oldVector; - bool m_doDetect; QGraphicsSimpleTextItem *label; --- trunk/KDE/kdegames/kolf/game.cpp #1191714:1191715 @@ -870,7 +870,7 @@ if (ball->isVisible()) { // is center of ball in? - if (contains(ball->pos() - pos()) /* && ball->curVector().magnitude() < 4*/) + if (contains(ball->pos() - pos())) { playSound("puddle"); ball->setAddStroke(ball->addStroke() + 1); @@ -900,9 +900,9 @@ bool Sand::collision(Ball *ball) { // is center of ball in? - if (contains(ball->pos()-pos())/* && ball->curVector().magnitude() < 4*/) + if (contains(ball->pos()-pos())) { - if (ball->curVector().magnitude() > 0) + if (ball->velocity().magnitude() > 0) ball->setFrictionMultiplier(7); else { @@ -1052,18 +1052,16 @@ bool Bumper::collision(Ball *ball) { double maxSpeed = ball->getMaxBumperBounceSpeed(); - double speed = qMin(maxSpeed, 1.8 + ball->curVector().magnitude() * .9); + double speed = qMin(maxSpeed, 1.8 + ball->velocity().magnitude() * .9); ball->reduceMaxBumperBounceSpeed(); - Vector betweenVector(pos() - ball->pos()); + Vector betweenVector(ball->pos() - pos()); betweenVector.setMagnitudeDirection(speed, // add some randomness so we don't go indefinetely betweenVector.direction() + deg2rad((KRandom::random() % 3) - 1) ); - ball->setVector(betweenVector); - // for some reason, x is always switched... - ball->setVelocity(Vector(-ball->velocity().x(), ball->velocity().y())); + ball->setVelocity(betweenVector); ball->setState(Rolling); setSpriteKey(QLatin1String("bumper_on")); @@ -1108,7 +1106,7 @@ { bool wasCenter = false; - switch (result(ball->pos(), ball->curVector().magnitude(), &wasCenter)) + switch (result(ball->pos(), ball->velocity().magnitude(), &wasCenter)) { case Result_Holed: place(ball, wasCenter); @@ -1220,7 +1218,7 @@ { bool wasCenter = false; - switch (result(ball->pos(), ball->curVector().magnitude(), &wasCenter)) + switch (result(ball->pos(), ball->velocity().magnitude(), &wasCenter)) { case Result_Holed: place(ball, wasCenter); @@ -1260,7 +1258,7 @@ playSound("blackholeputin"); const double diff = (m_maxSpeed - m_minSpeed); - const double speed = m_minSpeed + ball->curVector().magnitude() * (diff / 3.75); + const double speed = m_minSpeed + ball->velocity().magnitude() * (diff / 3.75); ball->setVelocity(Vector()); ball->setState(Stopped); @@ -1281,14 +1279,14 @@ { ball->setPos(exitItem->pos()); - Vector v = Vector::fromMagnitudeDirection(10, deg2rad(exitDeg)); - ball->setVector(v); + Vector v = Vector::fromMagnitudeDirection(10, -deg2rad(exitDeg)); + ball->setVelocity(v); - // advance ball 10 + // advance ball by 10 units ball->doAdvance(); v.setMagnitude(speed); - ball->setVector(v); + ball->setVelocity(v); ball->setForceStillGoing(false); ball->setVisible(true); @@ -2671,7 +2669,7 @@ } for (PlayerList::Iterator it = players->begin(); it != players->end(); ++it) - if ((*it).ball()->forceStillGoing() || ((*it).ball()->curState() == Rolling && (*it).ball()->curVector().magnitude() > 0 && (*it).ball()->isVisible())) + if ((*it).ball()->forceStillGoing() || ((*it).ball()->curState() == Rolling && (*it).ball()->velocity().magnitude() > 0 && (*it).ball()->isVisible())) return; int curState = curBall->curState(); @@ -3000,8 +2998,8 @@ if (ball->curState() == Holed) continue; - Vector v; - if (ball->placeOnGround(v)) + Vector oldVelocity; + if (ball->placeOnGround(oldVelocity)) { ball->setPlaceOnGround(false); @@ -3015,7 +3013,9 @@ { (*it).ball()->setDoDetect(false); - double x = ball->x(), y = ball->y(); + QPointF pos = ball->pos(); + //normalize old velocity + const QPointF v = oldVelocity / oldVelocity.magnitude(); while (1) { @@ -3023,25 +3023,17 @@ bool keepMoving = false; while (!list.isEmpty()) { - QGraphicsItem *item = list.first(); + QGraphicsItem *item = list.takeFirst(); if (item->data(0) == Rtti_DontPlaceOn) keepMoving = true; - - list.pop_front(); } if (!keepMoving) break; - const float movePixel = 3.0; - x -= cos(v.direction()) * movePixel; - y += sin(v.direction()) * movePixel; - - ball->setPos(x, y); + const qreal movePixel = 3.0; + pos -= v * movePixel; + ball->setPos(pos); } - - // move another two pixels away - x -= cos(v.direction()) * 2; - y += sin(v.direction()) * 2; } else if (choice == rehit) { @@ -3129,7 +3121,7 @@ emit maxStrokesReached(playerWhoMaxed); } -void KolfGame::startBall(const Vector &vector) +void KolfGame::startBall(const Vector &velocity) { playSound("hit"); @@ -3137,7 +3129,7 @@ putter->setVisible(false); (*curPlayer).ball()->setState(Rolling); - (*curPlayer).ball()->setVector(vector); + (*curPlayer).ball()->setVelocity(velocity); (*curPlayer).ball()->shotStarted(); QList::const_iterator item; @@ -3170,7 +3162,7 @@ (*curPlayer).ball()->collisionDetect(); - startBall(Vector::fromMagnitudeDirection(strength, putter->curAngle() + M_PI)); + startBall(Vector::fromMagnitudeDirection(strength, -(putter->curAngle() + M_PI))); addHoleInfo(ballStateList); } @@ -3178,7 +3170,7 @@ void KolfGame::addHoleInfo(BallStateList &list) { list.player = (*curPlayer).id(); - list.vector = (*curPlayer).ball()->curVector(); + list.vector = (*curPlayer).ball()->velocity(); list.hole = curHole; }