large interface for class , whish to break it up

Started by
5 comments, last by CadetUmfer 12 years, 10 months ago
this is an example of my code


float m_moveSpeed;
bool m_isAlive;
D3DXVECTOR3 m_position;



D3DXVECTOR3 m_rotation;
D3DXVECTOR3 m_rotationLocal;
void CheckRotation(D3DXVECTOR3& amount);


inline D3DXVECTOR3 GetPosition() {return m_position;}
inline void SetPosition(const D3DXVECTOR3& position) { m_position = position; UpdateMatrix();}
inline void UpdatePosition(const D3DXVECTOR3& amount) { m_position += (amount * m_moveSpeed); UpdateMatrix();}
void UpdatePositionLocal(const D3DXVECTOR3& amount);







tryed this



float m_moveSpeed;
bool m_isAlive;
D3DXVECTOR3 m_position;



D3DXVECTOR3 m_rotation;
D3DXVECTOR3 m_rotationLocal;
void CheckRotation(D3DXVECTOR3& amount);
struct SMove
{

inline D3DXVECTOR3 GetPosition() {return m_position;}
inline void SetPosition(const D3DXVECTOR3& position) { m_position = position; UpdateMatrix();}
inline void UpdatePosition(const D3DXVECTOR3& amount) { m_position += (amount * m_moveSpeed); UpdateMatrix();}
void UpdatePositionLocal(const D3DXVECTOR3& amount);
} move;


but it could not find m_position , or any other variables


how can i break it up , cause my code is a lot larger

its so i can do this->move.SetPosition()
Advertisement
Move the member variables in question inside the Move type?
That's not a large interface. And it pretty much handles a single topic, so seems fine.
the interface has about 30 funtions in it , in total

the interface has about 30 funtions in it , in total


Well then post them. The example you posted isn't necessarily large, or (imo) in need of separation.
30 functions isn't really that large neither if each one of them is there for a useful reason and makes sense in the context of the class.
[size="2"]I like the Walrus best.
30 functions is a hell of a lot, imo.

OP, what is the purpose of this class? Even from the brief snippit you posted, it seems to perform different unrelated functions. On one hand, it exposes an interface to manipulate a transform matrix via its individual components (position, rotation, scale). Then it also handles the relationship between global and local transforms? Ok not terrible... But then it also handles the physics of moving the object. Then there is "m_IsAlive". If this represents whether "UpdateMatrix" is doing anything or not (so you can set the object to inactive and save some matrix operations), then fine. But if it has to do with some sort of gameplay state, that's very bad. Hopefully there's not graphics-related code in there too...

So you have 1 class handling maintaining transform matrices, performing rudimentary physics simulation, and probably some gameplay functions as well. That's 3 different classes right there.

EDIT: Then there is also the smelliness of those UpdateMatrix calls. I understand wanting to keep the underlying matrices in sync. Still, you're pumping your game loop every frame. You're probably already touching every (active) instance of this class every frame. IME it's better to have an update function called every frame, than to recompute those matrices multiple (who knows how many?) times per frame. Then you just make sure that that update function is called before the renderer needs the matrix. Or you don't, and you let the renderer be a frame behind the physics, not usually a big deal unless it's an FPS.

EDIT2: Here I'll take a stab:
class PhysicsEntity {
D3DXVECTOR3 position;
D3DXVECTOR3 rotation;
D3DXVECTOR3 velocity;
D3DXVECTOR3 angularVelocity;

public:
const D3DXVECTOR3& GetPosition() const;
void SetPosition(const D3DXVECTOR3&);
// etc

void Update(float dt) {
position += velocity * dt;
rotation += angularVelocity * dt;
};
};

class SceneEntity {
D3DXMATRIX transform;

public:
void Update(const D3DXVECTOR3& position, const D3DXVECTOR3& rotation) {
// your UpdateMatrix stuff here
// also updating children and whatnot if it's a full scene graph
};
};

class GameEntity {
PhysicsEntity sim;
SceneEntity draw;
bool isAlive;
float moveSpeed;

public:
void Update(float dt) {
if (isAlive) {
D3DXVECTOR3 v = // calculate velocity from angle and moveSpeed and whether a move command has been issued
sim.SetVelocity(v);
sim.Update(dt);
draw.Update(sim.GetPosition(), sim.GetRotation());
}
};
};
Anthony Umfer

This topic is closed to new replies.

Advertisement