Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jag_oes

I need some OOP advice

This topic is 6017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hope this is the correct place to post ... it seemed that this could go under "Game Design," "General Programming," or "Game Programming" but the few posts in this forum on OOP made me post here ... I am making a series of classes for a future school project. Of the many classes I need to make, I need to write a 2d and 3d vector class, and an NxN matrix, 3x3 matrix, and 2x2 matrix class. I need separate classes for 2d and 3d vectors (as well as NxN, 3x3, and 2x2 matrices) for speed purposes ... sometimes I just don''t need 3d vectors or general size matrices and I can optimize many of the operations by creating new classes. My confusion comes into play when I think about how I am going to organize those classes. I can easily say that a 2d vector "is a type of" 3d vector which makes me think I should inherit the 2d vector class from the 3d vector class. Is this what you guys would do? Also, would the 3x3 matrix inherit from the NxN matrix class, and the 2x2 from the 3x3? I searched the internet for sources to see the layout of others'' work, but most projects were so small that the organization was not a problem. Thanks ...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I would think it would be the other way around, ie your 3x3 matrix class inherits from your 2x2 matrix, or your 3d vector inherits from your 2d class. The whole point of inheritance is to add to the existing code, so using a 2d vector class that inherits from the 3d class is wasteful. The extra variable will be included in the class when it won''t be used, so in my opinion you should inherit the other way around.

Share this post


Link to post
Share on other sites
Just my humble and simple opinion.

I think all of your vector classes should be independant. Would it be useful to make a 3D vector a kind-of 2D vector? I mean, would you have code that needs a 2D vector, and use a 3D vector instead? What would happen with the last dimension?

All your vectors would work the same way. But I don''t see any situation where a 3D vector could be treated like a 2D vector, without having a knowledge that maybe the 3rd dimension is some kind of constant.

I am no math or OOP guru, but that was my two cents.

Good luck

Share this post


Link to post
Share on other sites
Thank you both ... I don''t know OOP from crap and I posted to get some new, insightful thoughts on the subject ... you both provide very good arguments so I will just think about it more ... anyone else have additional suggestions, or more to add?

Thanks again ...

Share this post


Link to post
Share on other sites
Ok, I think I am going to go with Blaster''s advice, but before I start to redo everything can anyone give me some pitfalls I may encounter (if there are any)? The code has been rewritten many times and it is starting to look messy ...

Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
don''t use inheritance. You might want to use implicit constructors though (constructors with one argument that don''t use the explicit keyword) So if you used a 2d vector where you needed a 3d it would automatically turn into a 3d by calling the constructor.

Share this post


Link to post
Share on other sites
Vector and matrices clases are too granular for polymorphism, and the fact is, a 3D vector is NOT a 2D vector.
A 2D vector IS a 3D vector - whose''s z component is always 0.
The equations that govern the behavior of 2D vectors do not apply to 3D vectors - however the 3D generalizations hold true for 2D vectors. So if you used inheritence, you''d have to start with the N case and specialize downward. You could represent every 2D vector as a 3D vector as a nD vector - but for efficentcies sake you want a serperate class that consumes less space and avoids unneccessary calculations.

If you define any constructors, make sure to also define an empty default constructor that does nothing, so that you don''t have to pay initialization cost when it''s unneeded.

You could use a namespace or two if you want to organize it a little.

If you really wanted to make generic vector (and matrix) classes, you could use templates and (2D) iterators. That way the you could write the algorithms once, and they would work with any classes that provided the correct type of iterator. Vector algorithms require random-access iterators, and a matrix algorith would require a new kind of random-access 2D iterator.
If you used inline templates, all the abstraction overhead would compile away to nothing. There are template libraries of such things out there - The Matrix Template Library from Notre Dame comes to mind.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
My 2 cents:

Look at it more abstractly. It's not that a 3d vector IS_A a 2d vector, or vice versa, it's that a 2d and 3d vector IS_A vector.

Write a pure-virtual class CVector, and then C2DVector and C3DVector which inherit from CVector.

CVector would define the inherent member functions such as dot producting and operator overloading or whatever you want.

same goes for matrices, a 2x2 and 3x3 matrix IS_A matrix, so you could do the same.

that's the way i'd do it anyway.

PS: remember that a 3x3 matrix HAS_A 6 3d vectors, and a 2x2 matrix HAS_A 4 2d vectors. So if you do the vectors first then you can save yourself some code (maybe) if your derived matrix classes HAS_A 2d or 3d vectors.

Edited by - MelvinElvin on December 28, 2001 7:13:27 AM

[edit: oops, nearly toasted your post while trying to write a response, those edit and quote button should be further apart]


Edited by - Magmai Kai Holmlor on December 30, 2001 4:40:23 AM

Share this post


Link to post
Share on other sites
Magmai''s right. Vectors, matrices and other lowlevel primitives should be excluded from inheritance. There is simply too much overhead associated with virtual calls to justify it.

You can define a few custom operators to prevent typecasts. Like so:


operator Vector3(const Vector2 &v){
return Vector3(v.x, v.y, 0.0f);
}





Share this post


Link to post
Share on other sites
quote:

Look at it more abstractly. It''s not that a 3d vector IS_A a 2d vector, or vice versa, it''s that a 2d and 3d vector IS_A vector.


True, they are both vectors, but a 2D vector is a 3D vector, however the converse is not true. Also, the computation required for a 3D dot product can be dramaticaly simplified from the general case - and simplified more for 2D (if such a dot product makes any sense at all).

OOP gone horribly wrong:
quote:

Write a pure-virtual class CVector...


Realize that would cause a 50% space increase for the storage of 2D vectors (33% for 3D, etc...)? And I''d hazard about a 20% performance penalty?

For a 100x100 matrix, you might never know the difference...

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!