summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-08-28 16:42:47 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-08-28 16:42:47 +0000
commit3641a74507d692a7fbababfd8beb738f71e48acb (patch)
tree78d9235fe86ff2e958edb2cc2f777bbaade7607c
parent55786731e0517efdc123d4e8245b2aa94fa55cbe (diff)
downloadmana-client-3641a74507d692a7fbababfd8beb738f71e48acb.tar.gz
mana-client-3641a74507d692a7fbababfd8beb738f71e48acb.tar.bz2
mana-client-3641a74507d692a7fbababfd8beb738f71e48acb.tar.xz
mana-client-3641a74507d692a7fbababfd8beb738f71e48acb.zip
Added Vector class and used it in the particle engine.
-rw-r--r--ChangeLog4
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/Makefile.am3
-rw-r--r--src/imageparticle.cpp4
-rw-r--r--src/particle.cpp54
-rw-r--r--src/particle.h35
-rw-r--r--src/textparticle.cpp4
-rw-r--r--src/textparticle.h2
-rw-r--r--src/vector.h134
9 files changed, 184 insertions, 57 deletions
diff --git a/ChangeLog b/ChangeLog
index d960882c..15140521 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@
src/gui/itemshortcutwindow.h, src/CMakeLists.txt, src/Makefile.am:
Made buy dialog resizable and added a WindowListener class for
listening for window resize and move events.
+ * src/textparticle.h, src/particle.h, src/CMakeLists.txt,
+ src/particle.cpp, src/imageparticle.cpp, src/vector.h,
+ src/textparticle.cpp, src/Makefile.am: Added Vector class and used it
+ in the particle engine.
2007-08-27 Bjørn Lindeijer <bjorn@lindeijer.nl>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 725dd5a6..90d2b37f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -337,6 +337,7 @@ SET(SRCS
textparticle.cpp
textparticle.h
tileset.h
+ vector.h
)
ADD_EXECUTABLE(tmw ${SRCS})
diff --git a/src/Makefile.am b/src/Makefile.am
index 5f1ee760..a7f16a26 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -285,7 +285,8 @@ tmw_SOURCES = gui/widgets/resizegrip.cpp \
sprite.h \
textparticle.cpp \
textparticle.h \
- tileset.h
+ tileset.h \
+ vector.h
# set the include path found by configure
INCLUDES = \
diff --git a/src/imageparticle.cpp b/src/imageparticle.cpp
index 35cc21ad..db38e395 100644
--- a/src/imageparticle.cpp
+++ b/src/imageparticle.cpp
@@ -44,8 +44,8 @@ void ImageParticle::draw(Graphics *graphics, int offsetX, int offsetY) const
if (!mAlive)
return;
- int screenX = (int) mPosX + offsetX - mImage->getWidth() / 2;
- int screenY = (int) mPosY - (int) mPosZ + offsetY - mImage->getHeight()/2;
+ int screenX = (int) mPos.x + offsetX - mImage->getWidth() / 2;
+ int screenY = (int) mPos.y - (int)mPos.z + offsetY - mImage->getHeight()/2;
// Check if on screen
if (screenX + mImage->getWidth() < 0 ||
diff --git a/src/particle.cpp b/src/particle.cpp
index 2d0873ff..148bbf6f 100644
--- a/src/particle.cpp
+++ b/src/particle.cpp
@@ -52,14 +52,12 @@ const float Particle::PARTICLE_SKY = 800.0f;
Particle::Particle(Map *map):
mAlive(true),
- mPosX(0.0f), mPosY(0.0f), mPosZ(0.0f),
mLifetimeLeft(-1),
mLifetimePast(0),
mFadeOut(0),
mFadeIn(0),
mAutoDelete(true),
mMap(map),
- mVelocityX(0.0f), mVelocityY(0.0f), mVelocityZ(0.0f),
mGravity(0.0f),
mRandomnes(0),
mBounce(0.0f),
@@ -109,7 +107,7 @@ Particle::update()
p++
)
{
- (*p)->moveBy(mPosX, mPosY, mPosZ);
+ (*p)->moveBy(mPos.x, mPos.y, mPos.z);
mChildParticles.push_back (*p);
}
}
@@ -117,31 +115,28 @@ Particle::update()
if (mMomentum != 1.0f)
{
- mVelocityX *= mMomentum;
- mVelocityY *= mMomentum;
- mVelocityZ *= mMomentum;
+ mVelocity *= mMomentum;
}
if (mTarget && mAcceleration != 0.0f)
{
- float distX = (mPosX - mTarget->getPosX()) * SIN45;
- float distY = mPosY - mTarget->getPosY();
- float distZ = mPosZ - mTarget->getPosZ();
+ Vector dist = mPos - mTarget->getPosition();
+ dist.x *= SIN45;
float invHypotenuse;
- switch(Particle::fastPhysics)
+ switch (Particle::fastPhysics)
{
case 1:
invHypotenuse = fastInvSqrt(
- distX * distX + distY * distY + distZ * distZ);
+ dist.x * dist.x + dist.y * dist.y + dist.z * dist.z);
break;
case 2:
invHypotenuse = 2.0f /
- fabs(distX) + fabs(distY) + fabs(distZ);
+ fabs(dist.x) + fabs(dist.y) + fabs(dist.z);
break;
default:
invHypotenuse = 1.0f / sqrt(
- distX * distX + distY * distY + distZ * distZ);
+ dist.x * dist.x + dist.y * dist.y + dist.z * dist.z);
break;
}
@@ -152,25 +147,23 @@ Particle::update()
mAlive = false;
}
float accFactor = invHypotenuse * mAcceleration;
- mVelocityX -= distX * accFactor;
- mVelocityY -= distY * accFactor;
- mVelocityZ -= distZ * accFactor;
+ mVelocity -= dist * accFactor;
}
}
if (mRandomnes > 0)
{
- mVelocityX += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
- mVelocityY += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
- mVelocityZ += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
+ mVelocity.x += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
+ mVelocity.y += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
+ mVelocity.z += (rand()%mRandomnes - rand()%mRandomnes) / 1000.0f;
}
- mVelocityZ -= mGravity;
+ mVelocity.z -= mGravity;
// Update position
- mPosX += mVelocityX;
- mPosY += mVelocityY * SIN45;
- mPosZ += mVelocityZ * SIN45;
+ mPos.x += mVelocity.x;
+ mPos.y += mVelocity.y * SIN45;
+ mPos.z += mVelocity.z * SIN45;
// Update other stuff
if (mLifetimeLeft > 0)
@@ -179,14 +172,13 @@ Particle::update()
}
mLifetimePast++;
- if (mPosZ > PARTICLE_SKY || mPosZ < 0.0f)
+ if (mPos.z > PARTICLE_SKY || mPos.z < 0.0f)
{
if (mBounce > 0.0f)
{
- mPosZ *= -mBounce;
- mVelocityX *= mBounce;
- mVelocityY *= mBounce;
- mVelocityZ *= -mBounce;
+ mPos.z *= -mBounce;
+ mVelocity *= mBounce;
+ mVelocity.z = -mVelocity.z;
}
else {
mAlive = false;
@@ -282,9 +274,9 @@ Particle::addEffect(const std::string &particleEffectFile,
int offsetY = XML::getProperty(effectChildNode, "position-y", 0);
int offsetZ = XML::getProperty(effectChildNode, "position-z", 0);
- int particleX = (int)mPosX + pixelX + offsetX;
- int particleY = (int)mPosY + pixelY + offsetY;
- int particleZ = (int)mPosZ + offsetZ;
+ int particleX = (int) mPos.x + pixelX + offsetX;
+ int particleY = (int) mPos.y + pixelY + offsetY;
+ int particleZ = (int) mPos.z + offsetZ;
int lifetime = XML::getProperty(effectChildNode, "lifetime", -1);
diff --git a/src/particle.h b/src/particle.h
index e8b835ba..6a13cca1 100644
--- a/src/particle.h
+++ b/src/particle.h
@@ -31,7 +31,7 @@
#include "guichanfwd.h"
#include "sprite.h"
-
+#include "vector.h"
class Map;
class Particle;
@@ -97,7 +97,7 @@ class Particle : public Sprite
*/
virtual int
getPixelY() const
- { return (int) (mPosY + mPosZ) - 64; }
+ { return (int) (mPos.y + mPos.z) - 64; }
/**
* Sets the map the particle is on.
@@ -139,30 +139,27 @@ class Particle : public Sprite
*/
void
setPosition(float x, float y, float z)
- { mPosX = x; mPosY = y; mPosZ = z; }
+ { mPos.x = x; mPos.y = y; mPos.z = z; }
/**
* Sets the position in 2 dimensional space in pixels relative to map.
*/
void
setPosition(float x, float y)
- { mPosX = x; mPosY = y; }
-
- float getPosX() const
- { return mPosX; }
-
- float getPosY() const
- { return mPosY; }
+ { mPos.x = x; mPos.y = y; }
- float getPosZ() const
- { return mPosZ; }
+ /**
+ * Returns the particle position.
+ */
+ const Vector& getPosition() const
+ { return mPos; }
/**
* Changes the particle position relative
*/
void
moveBy(float x, float y, float z)
- { mPosX += x; mPosY += y; mPosZ += z; }
+ { mPos.x += x; mPos.y += y; mPos.z += z; }
/**
* Sets the time in game ticks until the particle is destroyed.
@@ -176,7 +173,7 @@ class Particle : public Sprite
* faded in completely.
*/
void
- setFadeOut (int fadeOut)
+ setFadeOut(int fadeOut)
{ mFadeOut = fadeOut; }
/**
@@ -184,7 +181,7 @@ class Particle : public Sprite
* fade out.
*/
void
- setFadeIn (int fadeIn)
+ setFadeIn(int fadeIn)
{ mFadeIn = fadeIn; }
/**
@@ -207,7 +204,7 @@ class Particle : public Sprite
*/
void
setVelocity(float x, float y, float z)
- { mVelocityX = x; mVelocityY = y; mVelocityZ = z; }
+ { mVelocity.x = x; mVelocity.y = y; mVelocity.z = z; }
/**
* Sets the downward acceleration.
@@ -264,7 +261,7 @@ class Particle : public Sprite
protected:
bool mAlive; /**< Is the particle supposed to be drawn and updated?*/
- float mPosX, mPosY, mPosZ; /**< Position in 3 dimensonal space - pixel based relative to map */
+ Vector mPos; /**< Position in pixels relative to map. */
int mLifetimeLeft; /**< Lifetime left in game ticks*/
int mLifetimePast; /**< Age of the particle in game ticks*/
int mFadeOut; /**< Lifetime in game ticks left where fading out begins*/
@@ -279,9 +276,7 @@ class Particle : public Sprite
Particles mChildParticles; /**< List of particles controlled by this particle */
// dynamic particle
- float mVelocityX; /**< X speed in pixels per game-tick. */
- float mVelocityY; /**< Y speed in pixels per game-tick. */
- float mVelocityZ; /**< Z speed in pixels per game-tick. */
+ Vector mVelocity; /**< Speed in pixels per game-tick. */
float mGravity; /**< Downward acceleration in pixels per game-tick² */
int mRandomnes; /**< Ammount of random vector change */
float mBounce; /**< How much the particle bounces off when hitting the ground */
diff --git a/src/textparticle.cpp b/src/textparticle.cpp
index 4ea4dbdc..4bc859cd 100644
--- a/src/textparticle.cpp
+++ b/src/textparticle.cpp
@@ -42,8 +42,8 @@ void TextParticle::draw(Graphics *graphics, int offsetX, int offsetY) const
if (!mAlive)
return;
- int screenX = (int) mPosX + offsetX;
- int screenY = (int) mPosY - (int) mPosZ + offsetY;
+ int screenX = (int) mPos.x + offsetX;
+ int screenY = (int) mPos.y - (int) mPos.z + offsetY;
int alpha = 255;
diff --git a/src/textparticle.h b/src/textparticle.h
index 134405bb..34badb57 100644
--- a/src/textparticle.h
+++ b/src/textparticle.h
@@ -47,7 +47,7 @@ class TextParticle : public Particle
// hack to improve text visibility
virtual int getPixelY() const
- { return (int) (mPosY + mPosZ); }
+ { return (int) (mPos.y + mPos.z); }
private:
std::string mText; /**< Text of the particle. */
diff --git a/src/vector.h b/src/vector.h
new file mode 100644
index 00000000..d6a74886
--- /dev/null
+++ b/src/vector.h
@@ -0,0 +1,134 @@
+/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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
+ * any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: tileset.h 3218 2007-03-21 21:56:12Z b_lindeijer $
+ */
+
+#ifndef _TMW_VECTOR_H_
+#define _TMW_VECTOR_H_
+
+/**
+ * Vector class. Represents either a 3D point in space, a velocity or a force.
+ * Provides several convenient operator overloads.
+ */
+class Vector
+{
+ public:
+ /**
+ * Constructor.
+ */
+ Vector():
+ x(0.0f),
+ y(0.0f),
+ z(0.0f)
+ {}
+
+ /**
+ * Constructor.
+ */
+ Vector(float x, float y, float z):
+ x(x),
+ y(y),
+ z(z)
+ {}
+
+ /**
+ * Copy constructor.
+ */
+ Vector(const Vector &v):
+ x(v.x),
+ y(v.y),
+ z(v.z)
+ {}
+
+ /**
+ * Scale vector operator.
+ */
+ Vector operator*(float c) const
+ {
+ return Vector(x * c,
+ y * c,
+ z * c);
+ }
+
+ /**
+ * In-place scale vector operator.
+ */
+ void operator*=(float c)
+ {
+ x *= c;
+ y *= c;
+ z *= c;
+ }
+
+ /**
+ * Scale vector operator.
+ */
+ Vector operator/(float c) const
+ {
+ return Vector(x / c,
+ y / c,
+ z / c);
+ }
+
+ /**
+ * Add vector operator.
+ */
+ Vector operator+(const Vector &v) const
+ {
+ return Vector(x + v.x,
+ y + v.y,
+ z + v.z);
+ }
+
+ /**
+ * In-place add vector operator.
+ */
+ void operator+=(const Vector &v)
+ {
+ x += v.x;
+ y += v.y;
+ z += v.z;
+ }
+
+ /**
+ * Substract vector operator.
+ */
+ Vector operator-(const Vector &v) const
+ {
+ return Vector(x - v.x,
+ y - v.y,
+ z - v.z);
+ }
+
+ /**
+ * In-place substract vector operator.
+ */
+ void operator-=(const Vector &v)
+ {
+ x -= v.x;
+ y -= v.y;
+ z -= v.z;
+ }
+
+ float x, y, z;
+};
+
+#endif