I am a total noob when it comes to any language in the C family of programming languages.(C, C++, C#, etc).
I am trying to understand the basics about how a physics engine works, and I found Randy Gaul's tutorial on how to make a 2d physics engine from scratch. I downloaded the source code from this repo and I tried to understand what was going on. I am a decent (not the best) Java programmer, so I know whats going on, until I hit this section.
There is an enum in a structure/class called shape:
struct Shape
{
enum Type
{
eCircle,
ePoly,
eCount
};
...
}
And then later in Collision.h
:
typedef void (*CollisionCallback)( Manifold *m, Body *a, Body *b );
extern CollisionCallback Dispatch[Shape::eCount][Shape::eCount];
in Collision.cpp
:
CollisionCallback Dispatch[Shape::eCount][Shape::eCount] =
{
{
CircletoCircle, CircletoPolygon
},
{
PolygontoCircle, PolygontoPolygon
},
};
CircletoCircle, CircletoPolygon, PolyontoCircle, PolygontoPolygon are all functions that are in the form functionName( Manifold *m, Body *a, Body *b )
From my poor understandings, I believe that this guy declared a type that returns void called CollisionCallBack with all the paremeters, to save up some pain from typing up void Name(Manifold *m, Body *a, Body *b)
all the time. The line with the extern is just to tell the compiler to hush up.
What I don't get is that chunk of code in Collision.cpp. So here is a list of things about this that I don't understand.
Firstly, what is with those square brackets? It definitely isn't an array, because he used it like this
Dispatch[A->shape->GetType( )][B->shape->GetType( )]( this, A, B );
getType() gives you the type of the enum the shape in object A is.Secondly, they typedef makes this look like a method, yet you can assign a value to the method, or is this something else that you can do with C++? How does this work?
- Thirdly, I ran the application and it worked, so how does this actually call the methods without even giving parameters to the methods?
- Fourthly, and finally, why did he put
Shape::eCount
inside that block?
Thanks for your attention so far, I know this is very long, because I am terrible at explaining. I would prefer if someone could provide me with a java equivilent (if it exists) for this or at least answer the four questions I asked above.
Thanks in advance.