[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Help needed with basic 2D collision detection
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Help needed with basic 2D collision detection
- From: "Thiago Chaves" <shundread@xxxxxxxxx>
- Date: Sat, 26 Apr 2008 18:16:36 +0300
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 26 Apr 2008 11:16:45 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=cqyCA5kiukC1VhGF59dTNcJIe7r5+DhPu2JNeKVbgng=; b=QJipW6edD/vT487ZTmzMMwk03FRtY2fKnonOMEWFlXl5Yj/eYs5sD0VG2d/sN4eU9Ryi1UurHlYsjYMCQ6//rgcsC4lGdMieNpibNgPfoQjm408fw8Flr/njvPR+qVIkZtMj3fxjmPJTAZi+6dTWaEXxfAA0cDN60VJwCl4I6AE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=WV/acrXZf3ZxjFWWr0f3QQ6TTy/+td56P5qTB/JNj/Jxz5QbBMpmsl+0ExQEUU4lXhTKPxyBpmuTcNDMeUQHzpimMoxNcs9yvvBV16kz+qG0fFFXzvAqmC1kTuT+NfCUVzzOxrT7AEvPR2U37EkTgoVSMHEZ+w0eTm/E8U9YpBY=
- In-reply-to: <52b120340804260758n73aa6410i2544cd3bad06f0c0@xxxxxxxxxxxxxx>
- References: <52b120340804260758n73aa6410i2544cd3bad06f0c0@xxxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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
>