After spending about 3.5 hours yesterday banging my head against the wall trying to figure out how to make the sword work I fixed it completely in about 20 minutes. Not only that, but my solution involves a lot less trigonometry compared to the original. I realized that the issue was that I needed to get the center of the image in the right spot, and not worry about the corners.
New Math: //extremely simple compared to my previous trig heavy approach
atk.x = collBox.x – 17.5;
atk.y = collBox.y – 32.5;
The first two lines are just setting the center on the player character’s center. Necessary, since the two images are different sizes. The third and fourth lines are where we “push” the image center to the right spot from the PC image. BTW, the /57.29… is to convert from degrees to radians, and the 40 is the distance that we must push the image.
There was also a small bug where pressing any of the movement keys (wasd, or updownleftright), threw the angle of my images off. It was weird, but I quickly caught it using some command line debugging.
I was previously using eventQueue.motion.x to get the x coordinate of the mouse and the equivalent for y. That doesn’t work surprisingly, but SDL_GetMouseState(&mouseX, &mouseY) works perfectly. Go figure.
I’d highly recommend making this video full screen.