[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Help needed with basic 2D collision detection



It's easier to answer if a piece of the buggy code is pasted here.

But one solution would be making the movement in two steps, for
instance (warning, bad pseudocode ahead):

#Step one, horizontal movement.
x, y = guy.get_position()
xd, yd = guy.get_speed()
xend, yend = x+xd, y+yd

for each block in blocks:
   if block.collide(xend, y):
      xend = block.adjust_position_horizontally(xend, xd) #xd is
passed so that block knows in which direction it should push the
caracted

#Step two, vertical movement
for each block in blocks:
   if block.collide(xend, yend):
      yend = block.adjust_position_vertically(yend, yd)

guy.position = (xend, yend)

-Thiago

On Sat, Apr 26, 2008 at 5:58 PM, Bruno <brunowgame@xxxxxxxxx> wrote:
> Hi all,
>
> Recently started learning Python (my first experience with a programming
> language since actionscript back in the time of Flash 5) and have been
> making a simple 2d platformer to learn the basics. So far the collision
> detection (using only rectangles) is pretty much done and works on a basic
> "stop moving, adjust position" function being called by the player
> character, but the problem I'm having is that it's a little too precise - if
> two squares are stacked one on top of the other, rather than the player
> hitting the upper square and sliding to the floor it will snag on the top of
> the lower square.
>
> Obviously this could be avoided by just making the two squares one
> rectangle, but I'd like to be able to build levels out of many small squares
> (plus the fact I can't work it out has made me all the more determined).
> I've tried various different techniques, such as having a smaller collision
> rect, or unifying the rects of the two squares on the fly, but to no avail.
>
> Provided that all made sense, would anyone be able to suggest a new approach
> I could try? It's pretty simple I know, and I could just rip someone else's
> engine out and use it, but I wouldn't learn much in the process.
>
> Thanks,
>
> Bruno
>