Trying to improve the collision detection


I thought I’d try and experiment with improving the collision detection so it doesn’t just stop the character in their tracks when they touch a prop. So if you run into the bottom of the rock while moving up and right it would stop moving up but carry on to the right.

To do this I’m using the undoMovement() function I’d previously split into two parts to allow continuation of movement running into the boundaries of the map.


The problem I’m having is that it only seems to work on the left and right sides of the props, on the bottom and top the character just gets stuck in place when it touches them. Even though the code for each is copied with the axis and width/height changed.


Feel like I’m missing something simple, but any help would be much appreciated as I’ve been banging my head against a wall trying to work out why it’s behaving like this.

Try to setup your boolean tests as variables, that way when you step through your code with a breakpoint you can see where things may go wrong. I’d also recommend saving the collision of the prop as a variable to get even more data.

Here’s an example of what I was doing when I tried to figure out this problem myself one day. (this code doesn’t work either).

Keep up with the experimentation, let us know how it goes!

Thanks for the pointers, I’ve tried creating variables to track the values but it just gives me ‘not available’ when I run the game. I’ve put a breakpoint below the while loop so I can move around in game to see the values change but get nothing. Any ideas what I’m doing wrong?

Also a little quirk I’ve found is whichever if statement I put first works properly and the second one doesn’t :upside_down_face:

The little quirk makes sense cause it ends up being an order-of-operations issue where one essentially overrides the other.

I’ve found the watch feature to be unreliable, so putting breakpoints for if-statements you can trigger whenever you want is the better way to go imo

Conditional breakpoints worked like a charm. Noticed that the knight was getting stuck because it was moving inside the collision rectanlge for the prop before it stopped so it was triggering more movement conditions than it was meant to. So I created a buffer zone equivalent to the knight’s speed so it would only undo the movement that went further than knight.getSpeed() into the collision recs. Excuse the messy code, it’s how I had to written down as I was thinking it through.

This worked great, or so I thought. Turns out it can get stuck on the corners if you catch it in the tiny square where the x & y buffer zones overlap.

gamedev collision2

I’ve had a few goes at trying to fix this but nothing worked without breaking something else. I think I’m ready to say this is beyond me and move on.

Thanks for the help, it’s been a learning experience.

Decided to have one left crack at it after I finished the course because I needed to working to experiment with introducing other features. I realised I was thinking about it the wrong way, all that matters is a collision, the direction of collision, and the direction of movement.

The code below appears to be working fully without getting stuck or causing any odd movement. The test will be when I appply the collision to the enemies.


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

Privacy & Terms