Log In  


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!



1

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

Cart #collision_wind_bc-0 | 2024-10-23 | Code ▽ | Embed ▽ | No License



[Please log in to post a comment]