Rotating hitbox Rectangle 'dest' using rlgl.h?

TLDR: I need help rotating a hitbox Rectangle ‘dest’ around its Vector2 origin (bottom-left) using mouse position, possibly with rlgl functionality, hereby also rotating the weapon texture on top of it.

I’ve been working on a new weapon swing mechanic in my project and by referring to the moving eyes example on the Raylib site, have come up with a weapon that swings around my player. The rectangle ‘dest’ sits on top of a small invisible circle which follows the mouse as it rotates within a larger invisible circle around my player, representing his arm swing length if you will, so a portion of my code is similar to that Raylib example. I’m pretty happy with this and it looks good - however, the rectangle ‘dest’ itself does not rotate, so at the moment the sword texture faces the same direction regardless of rotation, with sword hilt at the bottom left and blade tip at the top right.

swing

~ ‘dest’ in black, with a white line protruding from ‘origin’ towards the centre of the invisible circle

I want to rotate this of course to follow and face the mouse while the circle it sits on also follows the mouse, but changing the float ‘rotation’ will no longer achieve the desired result, instead the texture will rotate away from the whole shape. This is simply due to how it’s all built up, so rotating the texture itself won’t work. My reasoning is that if I can rotate the rectangle ‘dest’, around it’s new bottom-left origin, then the texture sitting on top of it will also rotate.

I’ve done some online searching into rlgl functionality which I read is required to rotate shapes around it’s origin but unfortunately I’ve hit a brick wall and can freely admit I have no idea how the rlgl functions work. (https://www.reddit.com/r/raylib/comments/mgjgbl/how_do_you_rotate_a_shape_around_its_origin_in/)

My results on attempting to use rlPushMatrix() followed by rlTranslate(), rlRotate() and then rlPopMatrix() have been the following:

  • No rotation whatsoever.
  • The entire map texture rotating instead of the Rectangle ‘dest’.
  • Minimal, barely noticeable rotation/movement of other line shapes that are not the Rectangle ‘dest’.

So my rlgl code is just greened out until I can get it to work.

I need some direction. I notice some apparent solutions for DrawPoly() in this example https://github.com/raysan5/raylib/issues/2251 but I don’t see a Rectangle equivalent for shapes that aren’t filled with colour. I thought about opting for drawing a lined polygon for the ‘dest’ Rectangle but I don’t know how to put a texture on top of that with DrawTexturePro() when that function expects two Rectangles.

Do I have to make a new function? Or is there a simple solution out there that I just haven’t learned yet? Any help would be appreciated.

Note: I removed the course lecture tag as this is beyond the scope of the course

Hello Maezumo,

It’s awesome to see that you’re taking the material from our CPP Fundamentals course and pushing beyond it. Unfortunately I don’t know much about Raylib beyond what’s in the course so I myself can’t provide any guidance for you.

If you haven’t already, you can also try asking this question in either the Raylib community discord or in our own Discord community. Out of the two, the Raylib discord is your best bet but keep in mind that they are not there to provide support for our courses. That said, this specific issue goes beyond our scope so it should be ok.

1 Like

Thanks anyway man, yeah I’ve joined both of those groups now. This Classy Clash example is just a good springboard to add other stuff and work on my C++ I realise, although I’ve likely squeezed as much as I can out of this course. Onwards and upwards!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms