Hey! I need help with my first platformer game, goddess of the wind.
The collision isn't working as I hoped, and the reason is either im doing it completely wrong, or the collision box is too small.
Heres the code for the collision:
if btn(0) then
p.dx-=.2
end
if btn(1) then
p.dx+=.2
end
if p.dx<0 and not fget(mget((p.x-1)\8,(p.y+3)\8),0) or p.dx<0
and not fget(mget((p.x-1)\8,(p.y+4)\8),0) or p.dx>0
and not fget(mget((p.x+8)\8,(p.y+3)\8),0) or p.dx>0
and not fget(mget((p.x+8)\8,(p.y+4)\8),0) then
p.x+=p.dx
end
if p.dy<0 and not fget(mget((p.x+3)\8,(p.y)\8),0) or p.dy<0
and not fget(mget((p.x+4)\8,(p.y)\8),0) or p.dy>0
and not fget(mget((p.x+3)\8,(p.y+9)\8),0) or p.dy>0
and not fget(mget((p.x+4)\8,(p.y+9)\8),0) then
p.y+=p.dy
end
If you have any ideas or better ways to write the code, please tell me. Thanks!
I don't know for sure that this will solve your problem, but my immediate reaction to seeing the code is that you might need some parentheses around your groups of ands because it seems like you're looking to do the increments to p.x and p.y if any of the 4 ands is true in each case.
if (p.dx<0 and not fget(mget((p.x-1)\8,(p.y+3)\8),0)) or (p.dx<0 and not fget(mget((p.x-1)\8,(p.y+4)\8),0)) or (p.dx>0 and not fget(mget((p.x+8)\8,(p.y+3)\8),0)) or (p.dx>0 and not fget(mget((p.x+8)\8,(p.y+4)\8),0)) then p.x+=p.dx end if (p.dy<0 and not fget(mget((p.x+3)\8,(p.y)\8),0)) or (p.dy<0 and not fget(mget((p.x+4)\8,(p.y)\8),0)) or (p.dy>0 and not fget(mget((p.x+3)\8,(p.y+9)\8),0)) or (p.dy>0 and not fget(mget((p.x+4)\8,(p.y+9)\8),0)) then p.y+=p.dy end |
We will respect the original code as much as possible and extract only the X coordinate of the modified code.
checkpx=p.x+p.dx -- p.dx==0 prevmapx=p.x\8 -- The MAP x-coordinate returned by the collision cmx=prevmapx -- X coordinate to check for collision if p.dx<0 then cmx=checkpx\8 -- update cmx for left move elseif p.dx>0 then cmx=(checkpx+7)\8 -- update cmx for right move prevmapx=cmx-1 -- update prev-map-x for right move end -- X coordinate collision check if fget(mget(cmx,(p.y+3)\8))~=0 or fget(mget(cmx,(p.y+4)\8))~=0 then p.x=prevmapx*8 -- Convert MAP coordinate scale to pixel coordinate scale p.dx=0 else p.x=checkpx -- Update to the checked x coordinate. end |
Thanks, @2bitchuck and @shiftalow! I applied the parentheses to the code immediatetly, but a slight problem with the checkpx is with adding p.x+p.dx, because it doubles the speed of the player. Also, with the gravity constantly pushing the player down, the collision can't stop the player fast enough. I appreciate the code though, and I found a way to fix the gravity bug, but with high jumping, the collision breaks through.
@derpyworm2
If you are using my code, you will need to do the same thing for the Y coordinate as for the X coordinate.
[Please log in to post a comment]