Wall Collision With Player
Hey there! I've added walls to my project that I'm working on, I just need help with adding collision to the walls with the player. Note: to see the walls, you need to slice and dice the enemy once. Heres the link: https://microstudio.io/Heythat/testrun2/.
Thanks for taking a look into this :D
You have two options:
- If the player is moving over TileMap ("Map" in MicroStudio), you check the player's position and add the direction in which the player is walking to these coordinates,
you round to the nearest integer,
you check on the map whether there is any element (tile) under the obtained number (map.get(x, y) method)
It is good for beginners and 2D games.
Check out this code I recently made - https://microstudio.io/i/Loginus/mapcollision/
- AABB - a collision detection method. Read on Wikipedia. Look for examples.
I also made an example - https://microstudio.io/i/Loginus/collision/
You create a list of objects, check each object with each other (if they are all moving). For a simple game it will be a list of faces (square tiles) and you will only need to check with the player.
If the world is very large you will have to use QuadTree - https://microstudio.io/i/Loginus/quadtree/
Edit :
There is also an option to use a physical library such as Matter.js - it can be easily added to the project in the options.
For large projects and advanced programming.
Edit :
It is also made in MicroStudio "QuickEngine" - https://microstudio.dev/i/gilles/quickengine/ - Has player collisions with tiles.
So i used this code to figure out when they were colliding (the player sprite and the enemy sprite)
if distance(player.x, player.y, enemy.x, enemy.y) < 35 then
what should I do to make the player not just phase through it? I want to do this for my walls, but I'm gonna try on my enemy first.
local next_pos_x = player.x + player.direct.x
local next_pos_y = player.y + player.direct.y
if distance(next_pox_x, next_pos_y, enemy.x, enemy.y) < 35 then
print( "collision" )
else
player.x = next_pos_x
player.y = next_pos_y
end
In player.direct.x and player.direct.y there are values in which direction the player is going.
You can set these values in the update function
direct_update = function()
if keyborard.UP then player.direct.y = -1 end
......
end