C++ question on "clean code design"

I knew I was thinking about the instance->memberFunction() too hard. Whoops. And I have actually already downloaded your code but have not had a chance to look through it yet.

As for having spheres, cylinders, and plain polygons (usually just for terrain), I find all objects are approximated well, reactions are easy and feel natural, and it is very efficient.

Thanks again everyone’s responses.

I really did not want to continue this thread but any how…

Iceman - how does macha’s code solve this problem? All it does is print the object you are colliding against. The problem here is to get 3 things: -The object you are colliding against
-The object you are colliding with
-A way to access a method to perform the collision between the two objects of known types.

With macha’s code all we can get is one of the collision objects, not the other object or how to collide against it. Or perhaps I am reading the code wrong? (elighten me please)

If you do write the code the way sqrt[-1]
listed you will have to watch out that you have a TestCollisionSphere(…) method for every type of collision. I used this type of code for handeling collisions. What happens when Object A collids into object B. Do they explode, Enter the structure, etc. Anyway the part that you have to watch out for is if you happen to forget a collision method, it will crash the program. Believe me, I had my share of “Program has performed illeagal operaton…” plenty of times until I found what was crashing the program.

There are two great c++ books out there written by Scott Meyers
called “Effective C++: 50 Specific Ways to Improve Your Programs and Design” and “Effective C++ Cd: 85 Specific Ways to Improve Your Programs and Designs”. One of these books, I forget which one, decribes using virtual functions to handle the collisions and you have the opprotunity to catch any collisions that you do not have registered and display a message telling you that you need to impliment this collision code as well. Trust Me. It is worth the money to read these books. The best thing is that you do NOT have to read the book from cover to cover.

Uh… Thug how can you forget to implement TestCollisionSphere? It is a pure virtual method in the base class. If you forget to implement it, the compiler will complain.

Also what you do with the result of the collision is not (and should not be) part of this code, it only determines if a collision occured between two objects (and if you wish the position of the collision)

I also fail to see how this design could crash your program. Could you elaborate more?

You could use a slightly modified version of sqrt[-1]'s code, and you wouldn’t need to modify the derived types to add a new collision type.

Simply make the base class functions virtual, but not pure virtual, and implment an empty function for each one, or put an assert in.

Then when you add TestCollisionCube(Cube*) {} to the base class, you just implement it in the cube geometry class.

Some ppl will probably say thats really dirty but it gets around the problem nickels pointed out, with no ugly casts involved.

Nutty