2D collisions (wall)

Blitz3D Forums/Blitz3D Beginners Area/2D collisions (wall)

psychicbottle(Posted 2013) [#1]
So, I'm trying to make a simple wall in blitz3d(2d part) it's just a simple thing I'm doing while on vacation but everytime I try to make "wall-like" collisions my character flips out haha


Matty(Posted 2013) [#2]
Logic -

Calculate new x/y position in world of character when in motion.

If new x/y position has passed through a wall then check if it is possible to move along only 1 axis instead without passing through a wall.

Depending on results above either don't move the character, move along one axis only, or move to new position.

Simple.


Kryzon(Posted 2013) [#3]
Cooode, where are you~


psychicbottle(Posted 2013) [#4]
So this is the function I use right now but it is very flawed like I was trying to think of a way to make a good platformer so if you were to like jump on top of blocks then they would act like floors and walls would act like walls ect. right now it's a top down so you move Left Right Up Down and what happens is "LK$" (Last Key) records which key you pressed last then when you run into a wall it pushes you in the opposite direction of that key that was pressed.



Last edited 2013


psychicbottle(Posted 2013) [#5]
@Kryzon how should I check if that space is occupied? It would need to be fairly fast should I make a transparent image and check with that? or the image to rect collision?


Kryzon(Posted 2013) [#6]
Hi psychic.
What happens is this; instead of using the pixel-based approach you should use a math one. It's faster and has generally better results.
Plus it allows for extention: you can add more complex types of fences and obstacles later.

It is not easier to understand, however.
What we're going to do is attach "colliders" to every object we want our player to be able to bump against. For your character, imagine a single rectangle that perfectly encloses the sprite image.

A collider is an abstract rectangle.
A rectangle, as you may know, is formed by 4 line segments:



The way with which you move your character is by applying a vector to him, an oriented velocty:



This vector can be seen as being formed by two components: a horizontal (X) and a vertical (Y).
Depending on the SIGN of these components (negative or positive) we can see what will be the FRONT FACING sides of that rectangle. The ones that point toward the direction it's moving:



Since that rectangle is moving up (negative Y value in Blitz), we know side A is the one that can potentially hit anything ahead. Since that rectangle is also moving right (positive X value in Blitz), we know side B can also hit something that may be ahead.

With this in mind we can build a system that, based on the motion of a rectangle, compares the intersection of the front sides (the ones that are first going to hit something) against the OPPOSITE sides of all other rectangles.
Imagine this: if a rectangle moves right it's going to hit the LEFT side of something. There's no way its right side will hit another right side as well. It's always the opposite sides of rectangles that collide.
If that rectangle moves up, its top side is going to hit the bottom side of anything that comes ahead.

So I've coded and built a demo to work with this behavior; it seems to be doing ok.

- Download this image: 2rfstnl.png (it has this weird name from TinyPic.com)
- Save it as "level.png" in a certain folder.
- Then copy the following code to your Blitz, save it as a .bb file in the same folder you downloaded that image and run it.

Code:


There's more stuff to this method, we'll talk later.


psychicbottle(Posted 2013) [#7]
whoa, thanks so much. Kinda confused but I think I am piecing it together slowly haha


Kryzon(Posted 2013) [#8]
Hi. You're right, it's gonna take some time to digest it.

Replace that ReactColliders() function with this. I've replaced the calculations with appropriately-named variables. Should make more sense: