raylib icon indicating copy to clipboard operation
raylib copied to clipboard

[Raymath] Add C++ operator overloads for common math function

Open JeffM2501 opened this issue 1 year ago • 5 comments

This PR adds operator overloads for C++ for the math operations that have raymath functions. Vectror2, 3, 4, and Quaternion are supported. All operators are defined inline, and the define RAYLIB_DISABLE_VECTOR_OPERATORS can be used to disable this functionality.

While raylib is C, and not C++ and normally C++ code should be kept out of raylib, these operators offer exceptional utility and thus are an exception.

JeffM2501 avatar Oct 15 '24 20:10 JeffM2501

For credit, this idea was from @Peter0x44 initially.

JeffM2501 avatar Oct 16 '24 00:10 JeffM2501

Actually, I got the idea from box2d which does the same thing.

Peter0x44 avatar Oct 16 '24 00:10 Peter0x44

Should the operators call raymath functions or should they just be inlined? There is a policy that the functions don't call each other, not sure if it should be extended to these operators or not.

Peter0x44 avatar Oct 16 '24 00:10 Peter0x44

I very specifically had them call the raymath functions so that we did not duplicate code. If a change has to be made to some math function, I don't want that change to have to be made in two places. The goal of this is to minimize maintenance cost.

JeffM2501 avatar Oct 16 '24 01:10 JeffM2501

I would say most of the functions the operators call are not subject to change so inlining them might be better to stay consistent. But that's up to Ray.

Peter0x44 avatar Oct 16 '24 01:10 Peter0x44

@JeffM2501 @Peter0x44 Thanks for the improvement, it looks good to me, let me know if it is ready to be merged!

raysan5 avatar Oct 21 '24 11:10 raysan5

I think it is good for 5.5

JeffM2501 avatar Oct 21 '24 13:10 JeffM2501

@JeffM2501 @Peter0x44 thanks for the improvement! merged! I'm reviewing the formating and details myself!

raysan5 avatar Oct 21 '24 14:10 raysan5

@JeffM2501 @Peter0x44 Just noticed there are no operators for Matrix, should there be some added?

raysan5 avatar Oct 21 '24 14:10 raysan5

@JeffM2501 @Peter0x44 Just noticed there are no operators for Matrix, should there be some added? I just added some matrix operators. I'm not sure they are super important.

JeffM2501 avatar Oct 21 '24 15:10 JeffM2501

@JeffM2501 @Peter0x44 Just noticed there are no operators for Matrix, should there be some added?

I have made another PR for matrix operators and merged back the formatting changes.

JeffM2501 avatar Oct 21 '24 16:10 JeffM2501

@RobLoach for visibility, this can have possible conflicts with the operators that raylib-cpp does, you should be aware of it when you update raylib-cpp to 5.5 :)

JeffM2501 avatar Oct 21 '24 17:10 JeffM2501

Oh, this is :fire::fire::fire::fire::fire::fire:

Feel free to move anything else in too :wink:

RobLoach avatar Oct 21 '24 17:10 RobLoach