World wrap around

Official forum for the Chipmunk2D Physics Library.
mstk
Posts: 1
Joined: Thu May 27, 2010 6:29 pm
Contact:

Re: World wrap around

Post by mstk »

This might help; here are the ways I've managed to implement a toroidal world within a physics engine.

1) Modulo (%) is your friend. If your screen goes from 0 to w, in order to find the new, wrapped position of the object, when the object is at p, it's simply p % w (if your screen's left side is a number not 0, it can still be used with simple arithmetic). For example, -1 % 100 = 99; 101 % 100 = 1. This can be set to run every single frame on every object, or only when an object is checked to be outside of your boundaries.
It's like someone invented a hardware-accelerated (?) subroutine for just what you need.

2) When updating the 2d environment hash, use your normal algorithm, but substitute the bounding box points for the %'d points.
Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests