Log In  

Could be me been tired. But I don't get why my character is falling through the map.

I thought I had done some basic collision detection, after following some tutorials. However it doesn't seem to work and my character simply falls straight through the map.

--player variables
p1=
{
        --initial sprite start
        --point and velocities
        x=64,
        y=24,
        vx=0,
        vy=0,

        isgrounded=false,

        grav=0.2,
}

function _update()

--left and right movement
p1.dx=0
if btn(0) then --left
        p1.dx=-2
end

if btn(1) then --right
        p1.dx+=2
end

p1.x+=p1.dx

--gravity
p1.vy+=p1.grav

p1.y+=p1.vy

local v=mget((p1.x+4)/8,(p1.y+8)/8)

p1.isgrounded=false

if p1.vy>=0 then
        --look for a solid tile
        if fget(v,0) then
            --place p1 on top of tile
            p1.y = flr((p1.y)/8)*8
            --halt velocity
            p1.dy = 0
            --allow jumping again
            p1.isgrounded=true
        end
end

end

function _draw()
cls()

map(0,0,0,0,128,128)
spr(48,p1.x,p1.y)
end
P#29948 2016-10-04 08:42 ( Edited 2016-10-04 15:31)

Do you need to flr your mget arguments? I'm not sure what rounding would do to that. I would print v to check.

P#29951 2016-10-04 09:04 ( Edited 2016-10-04 13:04)

It is coming back as nil.

How can I get it to check for a filled tile on a map?

I have the gravity and movement sorted.

P#29952 2016-10-04 09:24 ( Edited 2016-10-04 13:24)

Remove the "local". Otherwise you're not going to see v outside of its local scope.

P#29953 2016-10-04 09:27 ( Edited 2016-10-04 13:27)

Ok so one step closer lol.

V remains as 0 until it gets about a tile close to a filled tiles. Then it shoots up to 38.

P#29954 2016-10-04 09:32 ( Edited 2016-10-04 13:32)

V will just be the number of the Sprite used for the map tile (so I presume you might be using Sprite 38 as a background tile?). Fget is then checking to see if that Sprite has a flag set. You need to set a flag, then look for that (I believe).

Set the first flag of your background sprites (you set this in the Sprite editor), then see if your code works.

P#29955 2016-10-04 09:54 ( Edited 2016-10-04 13:55)

So Sprite 38 has its 0 flag set? Yeah what blue said, though I think Sprite 38 would be the ground.

P#29957 2016-10-04 10:00 ( Edited 2016-10-04 14:02)

in "if fget(v,0) then" you should do "p1.vy=0"

otherwise the vertical velocity would finish to be so high that it would be higher than 1 sprite/frame, so it would normal it pass through the ground.

P#29962 2016-10-04 10:26 ( Edited 2016-10-04 14:26)

Which is exactly what I found lol.

The guy sits there for a couple of frames then decides to fall off the face of planet, so to speak.

P#29964 2016-10-04 10:31 ( Edited 2016-10-04 14:31)

Actually where do I put the p1.vy=0?

When I replace anything in there I get a syntax error.

Sorry. I am trying to learn and this is helpful.

P#29965 2016-10-04 10:36 ( Edited 2016-10-04 14:36)

Never mind got it

P#29966 2016-10-04 10:37 ( Edited 2016-10-04 14:42)

Woo hoo!!

P#29967 2016-10-04 10:44 ( Edited 2016-10-04 14:44)

Another safety could be to set a maximum fall value, to avoid issues in case you are falling from very high.

--gravity
p1.vy+=p1.grav
if (p1.vy>3) p1.vy=3 --new

P#29968 2016-10-04 11:03 ( Edited 2016-10-04 15:03)

That was the other thing I want to look into. But for the moment I want to try and get the biggest mechanic in the game done.

But thanks all for your help!

It has bee frustrating but rewarding lol.

Now for a whisky.

P#29973 2016-10-04 11:31 ( Edited 2016-10-04 15:31)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 23:00:19 | 0.008s | Q:25