Sign in to follow this  
thedodgeruk

large interface for class , whish to break it up

Recommended Posts

thedodgeruk    124
this is an example of my code


[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);


[/code]




tryed this



[code] 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;

[/code]
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()

Share this post


Link to post
Share on other sites
Telastyn    3777
[quote name='thedodgeruk' timestamp='1306949153' post='4818350']
the interface has about 30 funtions in it , in total
[/quote]

Well then post them. The example you posted isn't necessarily large, or (imo) in need of separation.

Share this post


Link to post
Share on other sites
owl    376
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.

Share this post


Link to post
Share on other sites
CadetUmfer    234
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:
[code]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());
}
};
};[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this