Skip to content

Instantly share code, notes, and snippets.

@SilverIce
Created July 18, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilverIce/3138687 to your computer and use it in GitHub Desktop.
Save SilverIce/3138687 to your computer and use it in GitHub Desktop.
/** Represents abstract environment for objects that inside */
struct IMoveEnvironment
{
/** Converts relative object position that environment contains into global position
Takes 'outGlobal' parameter as local object position offset and converts it into global position */
virtual void ComputeGlobalPosition(Vector3& outGlobal) = 0;
/** Converts global into relative object position */
virtual void ComputeLocalPosition(Vector3& outLocal) = 0;
};
struct MovingEntity0
{
IMoveEnvironment* m_Environment;
/** Relative position */
Vector3 Position;
explicit MovingEntity0() : m_Environment(nullptr) {}
/** Attaches entity to environment.
Null environment can be passed also. In this case entity will be attached to global coordinate system.
Note: environment switch does not changes global position. */
void Environment(IMoveEnvironment* Env)
{
if (Env == m_Environment)
return;
Position = GlobalPosition(); // "move" our entity into global coord system
if (Env == nullptr)
; // special case: null environment recognized as global coordinate system, nothing to do
else
Env->ComputeLocalPosition(Position); // convert global into relative position
m_Environment = Env;
}
/** Current environment entity attached.
In case environment is null - it means that entity attached to global coordinate system. */
IMoveEnvironment* Environment() const {
return m_Environment;
}
Vector3 GlobalPosition() const
{
Vector3 global(Position);
if (m_Environment != nullptr)
m_Environment->ComputeGlobalPosition(global);
return global;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment