I have been working with @drummyfish’s excellent small3dlib library to try to make a game. I asked some questions in Discord and was encouraged to post my questions here. My questions are a bit scattered, but I’ll try to compile them in one post.
I wanted to move the camera perpendicular to its facing direction on the XZ axis (basically strafing), and I didn’t know the “canonical” way to do it. I figured out a way that worked, but then found a demo example that I expect is the better way.
My original approach was to try to convert the camera’s angle of rotation into a vector and calculated its individual components on the x and z axes. The library header says that a full 360 degree rotation is S3L_FRACTIONS_PER_UNIT, so I thought a 90 degree rotation would be S3L_FRACTIONS_PER_UNIT/4, but that moved along the direction the camera was facing. Instead, S3L_FRACTIONS_PER_UNIT/2 worked. Why is that? Here is the code I was using to strafe right (the hal3d functions are just wrapper functions I wrote):
*camera.x -= hal3d_cos(*camera.ry + S3L_FRACTIONS_PER_UNIT/2);
*camera.z -= hal3d_sin(*camera.ry + S3L_FRACTIONS_PER_UNIT/2);
After going through the code of some of the included examples, I found one already did strafing and used vector math, which looks like it’s how it should be done (code from level.c example showing strafing right):
S3L_Vec4 camF, camR;
S3L_rotationToDirections(scene.camera.transform.rotation,20,&camF,&camR,0);
...
else if (state[SDL_SCANCODE_RIGHT])
S3L_vec3Add(&scene.camera.transform.translation,camR);
However, I have been having trouble getting this to work because I think only one file can include small3dlib.h at a time without problems. I’m trying to divide my project where each game entity is in its own file, but I can’t refer to any of the S3L_* functions without creating a wrapper function in the file that can include the header library. I have encountered some header libraries that allow for setting a preprocessor variable to include only function and type declarations so a header library can be included in multiple files. Is there a way to do this with this library?
Right now I have been using a single file as an interface with small3dlib that I’m calling hal3d, and all other components call that as an abstraction layer. However, I also have a types.h header that defines some of the types from small3dlib.h such as S3L_Unit and S3L_Index so I can use them in other files. This worked until I needed the definition of S3L_Vec4, and now I’m getting errors like
../small3dlib/small3dlib.h:1595:6: note: expected ‘S3L_Vec4 {aka struct <anonymous>}’ but argument is of type ‘S3L_Vec4 {aka struct <anonymous>}’
probably because it doesn’t encounter the two definitions identical.
My main questions are:
- Why does subtracting S3L_FRACTIONS_PER_UNIT/2 from an angle correspond to a 90 degree rotation?
- Is the method of strafing implemented in the examples a better/more efficient approach?
- Is there a way I can include the library in multiple files without conflicting definitions? If not, @drummyfish, is that a feature that you would be interested in adding?