What is wrong with my code?

7 posts (showing 1-7)
Arnas

Market Level 0Community Level 2
144 posts

I'm sorry if I make a lot of new threads here lately.

Anyway, I started working on math collisions today, and I have never done this before. It's a bit of a knot at the moment, so I'm trying to figure out how to make this work.

Here is my current code: (Note: Image gets cut off!)

Preview:

When I only had left and right collisions made, everything seemed fine, but as soon as I created the top/bottom collisions, it seemed to always send the hero to the corners, bottom ones to be specific, depending on which side I collided from.

I had a feeling that it has something to do with the corner intersection on the hero between the vertical and horizontal colliders. Meaning two, or even three colliders can be touching the wall at the same given moment.

I tried to think of ways to reduce the sizes of those colliders, but they are not shapes. Should I use shapes instead, or is there a workaround with this? (Depending which way is better)

posted 2015-06-20T19:46:58-07:00 | edited 2015-06-20T19:58:32-07:00
JohnBlackburne

Market Level 3Community Level 10
2188 posts

It looks like you have everything you need, but you need to refactor it. The most obvious way is this: put the collision tests inside the initial four lines. What I mean is the first line, Key.W, moves you up (presumably).  Only if you are moving up can you collide with your object in the up direction. So do that test immediately after the Hero._y -= 10.

Alternately set a 'direction' flag in the first four lines and use that to decide which direction to test. That might make the code clearer, especially if you are testing multiple object collisions. As a bonus the code is more efficient because you are no longer doing tests you don't need.

posted 2015-06-22T00:13:12-07:00
Arnas

Market Level 0Community Level 2
144 posts

On Jun 21, 2015, JohnBlackburne said:

-

Thank you, that idea was helpful, however, what if I am moving let's say right and down at the same time?

The two colliders still intersect at the corners.

I have actually started using my idea of using shape colliders but have not gone too far with it yet, this is what it looks like:

The light blue shapes are the colliders and as you can see, none of them intersect at the corners. Would you still suggest using purely code or is using shapes better?

posted 2015-06-22T12:33:51-07:00 | edited 2015-06-22T12:34:49-07:00
djstatika

Market Level 10Community Level 7
1255 posts

im surprised that "and" still works, thats really old. Use && instead from now on...

You could just test to see if the position you moved from is above/below/left/right, if it was below then move the hero to the bottom, if it was to the left, then move left etc

posted 2015-06-22T15:34:54-07:00
Arnas

Market Level 0Community Level 2
144 posts

djstatika said:

im surprised that "and" still works, thats really old. Use && instead from now on...

You could just test to see if the position you moved from is above/below/left/right, if it was below then move the hero to the bottom, if it was to the left, then move left etc

Just tried that and it works. Do you know a way to do this without using shape colliders, or are those okay to use?

posted 2015-06-22T19:56:23-07:00 | edited 2015-06-22T19:56:38-07:00
JohnBlackburne

Market Level 3Community Level 10
2188 posts

On Jun 22, 2015, Arnas said:

Thank you, that idea was helpful, however, what if I am moving let's say right and down at the same time?

You can set two directions. So 'down' and 'right'. Then test both if both are set. Depending on your geometry you might only be able to hit in one direction at a time so can clear other flags once collision is detected.

The exception would be collision with convex shapes. E.g. if you go into a corner then you could stopped in both directions, so down and right, at once.

posted 2015-06-22T23:36:34-07:00
djstatika

Market Level 10Community Level 7
1255 posts

Just tried that and it works. Do you know a way to do this without using shape colliders, or are those okay to use?

I would just use the width/height of the Hero like you have in your first post, and not bother with 4 points of collision, because if you use the 4 points, then the collisions wont work very well on the corners if moving diagonally

posted 2015-06-23T10:31:17-07:00