# Platforms

• 04-05-2004
okinrus
Platforms
I was wondering what is the best way to do moving platforms, since I'm thinking of redesigning the current way. I've done the Platform class which is responsible for moving and drawing the platforms. It looks something like this
Code:

/** Responsible for the moving platforms within the game, a Platform object consists
*  of a list of tiles along with coordinates to keep track of where the platform
*  is */
class Platform {
protected:
Pos m_x;
Pos m_y;
private:
int m_nrow;
int m_ncol;
Speed m_xspeed;
Speed m_yspeed;
typedef std::vector<Tile*> GridType;
GridType m_grid;
MotionGridView& m_gv;
public:
/** @return width of the platform */
int getWidth() const {
return m_ncol * m_gv.getTileWidth();
}

/** @return height of the platform */
int getHeight() const {
return m_nrow * m_gv.getTileHeight();
}

/** set the x-speed of the platform */
void setXspeed(Speed xspeed) {
m_xspeed = xspeed;
}

/** set the y-speed of the platform */
void setYspeed(Speed yspeed) {
m_yspeed = yspeed;
}

/** @return x-position in logical units */
Pos getX() const {
return m_x;
}

/** @return y-position in logical units */
Pos getY() const {
return m_y;
}
protected:
/** Determine how far right it is posible to go
*  @param maxRight maximum distance right to return
*  @return maximum distance right <= <em>maxRight</em> */
int howFarRight(int maxRight) const
{
return m_gv.howFarRight(m_x, m_y + getHeight() - 1,
getWidth(), getHeight(), maxRight, false);
}

/** Determine how far left it is posible to go
*  @param maxLeft  maximum distance left to return
*  @return maximum distance left <= <em>maxLeft</em> */
int howFarLeft(int maxLeft) const
{
return m_gv.howFarLeft(m_x, m_y + getHeight() - 1, getWidth(),
getHeight(), maxLeft, false);
}

/** Determine how far up it is possible to go
*  @param maxUp  maximum distance up to return
*  @return maximum distance up <= <em>maxUp</em> */
int howFarUp(int maxUp) const
{
return m_gv.howFarUp(m_x, m_y + getHeight() - 1,
getWidth(), getHeight(), maxUp, false);
}

/** Determine how far down it is possible to go
*  @param  maxDown maximum distance down to return
*  @return maximum distance down <= <em>maxDown</em> */
int howFarDown(int maxDown) const
{
return m_gv.howFarDown(m_x, m_y + getHeight() - 1, getWidth(),
getHeight(), maxDown, false);
}
public:
typedef std::vector<int> Tiles;

/** Construct a platform
*  @param  x    initial logical x-coordinate of the platform
*  @param  y    initial logical y-coordinate of the platform
*  @param  ncol  number of columns the platform has
*  @param  tiles list of tiles in a vector
*  @param  gv    grid view responsible for the platform */
Platform(int x, int y, int ncol, const Tiles& tiles,
MotionGridView& gv);

/** Determeine whether a given rectangle collides with this platform
*  @param x x-coordinate of the rectangle
*  @param y y-coordinate of the ractangle
*  @param w width of the rectangle
*  @param h height of the rectangle */
bool collide(int x, int y, int w, int h);

/** Draw the platform onto <em>surface</em>
*  @param surface surface where the platform will be drawn */
virtual void draw(util::Surface& surface);

/** Update the platform's position */
virtual void update();

/** @return horizontal speed of the platform */
Speed getXspeed() const {
return m_xspeed;
}

/** @return vertical speed of the platform */
Speed getYspeed() const {
return m_yspeed;
}
};

The MotionGridView object is used by derived Platform object for their motion. For example, PlatformHorizontal will use m_gv.howFarRight to determine when to turn around and go left. Basically this is when the distance returned by howFarRight is 0.

However, the game objects, primarily the CaveMan object will use howFarDown to determine whether the object is standing on a solid surface. If its detected the CaveMan object is standing on a tile, nothing needs to be done, yet if caveman is standing on a platform then the the CaveMan object must be attached to the platform--his y and x movement must coincide with the platforms.

Since the motionGridView objects do not need to detect platforms, a flag is passed into the howFar method functions to determine whether to check for the platforms or not.

This works well for the most part, except for it being a bit kludgy, but when the CaveMan objects jumps up to hit a platform with his head, sometimes he goes through the platform if the platform is moving downward.