Log In  

Cart #basicmovement-1 | 2023-08-17 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

I need help with collision please
update 1.2: Added cls() because i forgot

thank you very much thePixelXb_ for helping me

I will work on this game until it is the best game ever!!!!!!

P#133159 2023-08-16 19:52 ( Edited 2023-08-17 14:19)

you might be interested in this

https://www.youtube.com/watch?v=J1wvvbVQ5zo

P#133173 2023-08-17 01:03
1

For collision, I recommend the function ↓

function collid(x,y,con)
    local x1,x2,y1,y2=0,0,0,0
    local cflag=0

    if con==⬅️ then x1,x2,y1,y2=x,x-1,y,y+7 end
    if con==➡️ then x1,x2,y1,y2=x+7,x+8,y,y+7 end
    if con==⬆️ then x1,x2,y1,y2=x,x+7,y,y-1 end
    if con==⬇️ then x1,x2,y1,y2=x,x+7,y+7,y+8 end

    if fm(x1,y1,1) or fm(x2,y1,1) or fm(x1,y2,1) or fm(x2,y2,1) then cflag=1    end
    return cflag    
end

This function depicts whether 2 virtual 8x8 box collide, and outputs a number (0 or 1) to indicate that. With it, you can insert it into movement update to realize the basic collision.

Note:

function fm(x, y, flag)
    return fget(mget(x\8, y\8), flag)
end

In that, you have to use sprite flags, which can be set on the right of the sprite editor. The collid function assumes solid block flag is 1, but you can edit that after knowing how it works.

Plus: when I walk to border I found out this ↓

For the situation, maybe a cls()(clear screen) is helpful:

...
function _draw()
 cls()
 map()
... 
P#133177 2023-08-17 01:52 ( Edited 2023-08-17 01:55)
1

Thanks for the help!

P#133201 2023-08-17 14:20

@thePixelXb
_how do i integrate the collison into my script? I cant figure it out.

P#133205 2023-08-17 14:30 ( Edited 2023-08-17 14:33)
1

Altough it is working you were given a rather unreadable function that uses some shorthand.
The basic idea is to check whether the position your character is being moved to is free and to only move the character if it is indeed free.
That is what that function is doing, but what you were not told is that that function needs a few arguments(the things in parenthesis(in this case x,y,con)).
That function changes the coordinates of what position to check according to the input of the player. So if con==⬅️ check the left side of my player.
The fm(x,y,flag) function checks whether a certain sprite flag(the little dots under the colors) is activated for a chosen sprite on the map. You can find them under the colors and sliders in the sprite editor. Clicking them turns them "on" or true.

Here is an easier to read and use function for collisions:

function check_for_collisions(x,y)
    local x1=flr((x)/8)
    local y1=flr((y)/8)
    local x2=flr((x+7)/8)
    local y2=flr((y+7)/8)
    local ul=fget(mget(x1,y1),0)
    local ur=fget(mget(x2,y1),0)
    local dl=fget(mget(x1,y2),0)
    local dr=fget(mget(x2,y2),0)
    if dr==true or dl==true or ur==true or ul==true then
    return false
    else
    return true
    end
end

You would use this function to check for future positions, so when you are about to move the player you first check whether that future position is clear. The simplest, albeit flawed version would look like this:

if btn(⬇️) and check_for_collisions(px,py+1) then
py+=1
end

The new functions only checks the corners of our sprite. x1 is the left side, x2 the right side, y1 the top and y2 the bottom. It scales these numbers down and rounds themn as it assumes that the players position is an eightfold version of the map position(allowing for finer movements) Adjust these margins to fit your character.
If any of the corners collide with something the function will return false making the if statement fail.

P#133238 2023-08-18 20:15 ( Edited 2023-08-18 20:15)

[Please log in to post a comment]