From a7923fa19f81ea145f3ce97123567970e88d9df3 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Tue, 30 Aug 2011 19:25:11 +1000 Subject: [PATCH] Add more proportionality modes to PointAttractor Inverse proportionality is now properly named, direct proportionality is added, and constant as well (in case they all just draw towards a point). Change-Id: I6187c0df908fdf20e2bb0b0a1007ebcc38dd3ac2 Reviewed-on: http://codereview.qt.nokia.com/3868 Reviewed-by: Qt Sanity Bot Reviewed-by: Martin Jones --- .../declarative/particles/asteroid/blackhole.qml | 2 +- .../particles/spaceexplorer/spaceexplorer.qml | 6 ++-- src/declarative/particles/qsgpointattractor.cpp | 21 ++++++++++++++----- src/declarative/particles/qsgpointattractor_p.h | 5 +++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/declarative/particles/asteroid/blackhole.qml b/examples/declarative/particles/asteroid/blackhole.qml index 4a7ce02..441f4c3 100644 --- a/examples/declarative/particles/asteroid/blackhole.qml +++ b/examples/declarative/particles/asteroid/blackhole.qml @@ -149,7 +149,7 @@ Rectangle{ id: gs; x: root.width/2; y: root.height/2; strength: 4000000; system: particles physics: PointAttractor.Acceleration - proportionalToDistance: PointAttractor.Quadratic + proportionalToDistance: PointAttractor.InverseQuadratic } Kill{ system: particles diff --git a/examples/declarative/particles/spaceexplorer/spaceexplorer.qml b/examples/declarative/particles/spaceexplorer/spaceexplorer.qml index 1bb3cda..727d711 100644 --- a/examples/declarative/particles/spaceexplorer/spaceexplorer.qml +++ b/examples/declarative/particles/spaceexplorer/spaceexplorer.qml @@ -276,7 +276,7 @@ Rectangle{ } PointAttractor{ id: gs1; x: vorteX; y: vorteY; strength: 800000; - proportionalToDistance: PointAttractor.Quadratic; + proportionalToDistance: PointAttractor.InverseQuadratic; system: foreground } Kill{ @@ -289,7 +289,7 @@ Rectangle{ PointAttractor{ id: gs2; x: vorteX2; y: vorteY2; strength: 800000; - proportionalToDistance: PointAttractor.Quadratic; + proportionalToDistance: PointAttractor.InverseQuadratic; system: foreground } Kill{ @@ -302,7 +302,7 @@ Rectangle{ PointAttractor{ id: gs3; x: vorteX3; y: vorteY3; strength: 800000; - proportionalToDistance: PointAttractor.Quadratic; + proportionalToDistance: PointAttractor.InverseQuadratic; system: foreground } Kill{ diff --git a/src/declarative/particles/qsgpointattractor.cpp b/src/declarative/particles/qsgpointattractor.cpp index 55b6cb6..21eaeaa 100644 --- a/src/declarative/particles/qsgpointattractor.cpp +++ b/src/declarative/particles/qsgpointattractor.cpp @@ -65,19 +65,28 @@ bool QSGPointAttractorAffector::affectParticle(QSGParticleData *d, qreal dt) { if (m_strength == 0.0) return false; - qreal dx = m_y - d->curX(); - qreal dy = m_x - d->curY(); + qreal dx = m_x - d->curX(); + qreal dy = m_y - d->curY(); qreal r = sqrt((dx*dx) + (dy*dy)); qreal theta = atan2(dy,dx); qreal ds = 0; switch (m_proportionalToDistance){ + case InverseQuadratic: + ds = (m_strength / qMax(1.,r*r)); + break; + case InverseLinear: + ds = (m_strength / qMax(1.,r)); + break; case Quadratic: - ds = (m_strength / qMax(1.,r*r)) * dt; + ds = (m_strength * qMax(1.,r*r)); break; - case Linear://also default - default: - ds = (m_strength / qMax(1.,r)) * dt; + case Linear: + ds = (m_strength * qMax(1.,r)); + break; + default: //also Constant + ds = m_strength; } + ds *= dt; dx = ds * cos(theta); dy = ds * sin(theta); qreal vx,vy; diff --git a/src/declarative/particles/qsgpointattractor_p.h b/src/declarative/particles/qsgpointattractor_p.h index 9571648..298965a 100644 --- a/src/declarative/particles/qsgpointattractor_p.h +++ b/src/declarative/particles/qsgpointattractor_p.h @@ -64,8 +64,11 @@ class QSGPointAttractorAffector : public QSGParticleAffector public: enum Proportion{ + Constant, Linear, - Quadratic + Quadratic, + InverseLinear, + InverseQuadratic }; enum PhysicsAffects { -- 1.7.2.5