Search code examples
cvectorcollision-detection

Gravity simulator, collision detection, vectors


Im creating a gravity simulator using Vector manipulation and the gravitational constant. I have defined 2 structs for the bodies.

typedef struct {
    double vector[3];
} Vector;

typedef struct {
    Vector colour;
    double mass;
    double radius;
    Vector position;
    Vector velocity;
    Vector accel;
} Object;

I have many vector-arithmetic functions including:

Vector VectorUnit(Vector a) {

Vector b;
int i;

for (i = 0; i < VECTOR_DIM; i++)
    b.vector[i] = (a.vector[i]) / (VectorMag(a));

return (b);
}

When I run the internals of the function, it compiles fine. Though when I explicitly call the function VectorUnit() using any vector quantity it claims a "conflicting type" error..

GRAV.c:463:8: error: conflicting types for ‘VectorUnit’
 Vector VectorUnit(Vector a)
    ^
GRAV.c:341:3: note: previous implicit declaration of ‘VectorUnit’ was here
   VectorUnit(bodies[j].position);

What issue is it having with a function call such as VectorUnit(bodies[j].position); When as mentioned, using the internals of my function compile flawlessly..


Solution

  • You are calling functions without declaring them first. This is illegal since C99. In C89 it meant to implicitly declare the function as returning int.

    You need to provide prototypes for your functions before they are called , e.g. have a header file with:

    Vector VectorUnit(Vector a);
    

    or if that function only appears in one .c file, have a declaration near the top of the file:

    static Vector VectorUnit(Vector a);
    

    or place your functions in a different order so that VectorUnit's body comes before any function that calls it.