C++ Course - Prop Collision, The collision box for the character is wrong

I am not sure how in the video your collisions worked properly. For me to get the collisions to work properly I had to divide the width of the character texture by the number of frames the sprite sheet had.
Without this it was producing an incorrect collision box for the character like so.
Screen Shot 2021-07-29 at 12.52.28

DrawRectangle(screenPos.x, screenPos.y, texture.width * textureScale, texture.height * textureScale, WHITE);

The top box is the collision box for the log, the bottom box is the collision box for the rock, the middle is the character if you don’t divide by the number of frames on the sprite sheet.

Screen Shot 2021-07-29 at 12.54.38

DrawRectangle(screenPos.x, screenPos.y, texture.width / maxFrame * textureScale, texture.height * textureScale, WHITE);

As you can see when you divide by the number of frames the collision box is correct.

If anyone can explain why his code worked in the video that’d be great, as I believe it is incorrect, or I may have missed something in his explanation.

Otherwise, I hope this helps someone if they encounter this issue while following along.

P.S if you are interested in how I made the collision boxes show, just draw rectangles at the end of the draw calls for the objects and pass in the same params as the collision rectangle within the GetCollisionRec() function.

Happy Coding!

JyounzuSan

Hi JyounzuSan,

I just started this course and I’m reading now your comment. I think the reason why the collision box for the character works fine in the video is because he is already using the width divided by the number of frames.

In the video, the code for the character’s getCollisionRec is:

Rectangle Character::getCollisionRec()
{
    return Rectangle{screenPos.x, screenPos.y, width*scale, height*scale};
}

 
Now the variable width is defined in the character constructor as:

width = texture.width/maxFrames;

So you’re right about needing to divide the character’s width by the number of frames for the collision box. You just achieved it in a different way :slight_smile:

Hope that explains it. Happy coding!

Nicole

2 Likes

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

Privacy & Terms