Author Topic: Collision in an "infinite" world?  (Read 2373 times)

Shadow

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Collision in an "infinite" world?
« on: Thu, Apr 14, 2011 »
I was having issues with objects not colliding if you moved the player to far in any direction from the starting location. I read this article http://flixel.org/forums/index.php?action=printpage;topic=2253.0, which states the solution as being FlxU.setWorldBounds(). The only problem is I wanted to create a "theoretically" infinite world. Therefore, how can I set the world bounds but not be limited by them?

Update
Looked at Canabalt iOS code and the parameters of followBounds() are set like this:
Code: [Select]
[FlxG followBoundsWithParam1:0 param2:0 param3:INT_MAX param4:480];I'm assuming I'll have to set mine similarly.

Thanks
« Last Edit: Thu, Apr 14, 2011 by Shadow »

Shadow

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: Collision in an "infinite" world?
« Reply #1 on: Thu, Apr 14, 2011 »
I think this is the best solution.
Code: [Select]
FlxG.followBounds(-int.MAX_VALUE, -int.MAX_VALUE, int.MAX_VALUE, int.MAX_VALUE);

ChainedLupine

  • Member
  • **
  • Posts: 91
  • Karma: +0/-0
    • View Profile
    • Dit Dah Games
Re: Collision in an "infinite" world?
« Reply #2 on: Thu, Apr 14, 2011 »
BTW, you might want to be careful with using MAXINT definitions for world bounds.

That sets the boundary for the overall space of the quadtree that Flixel uses for resolution collisions.  You probably won't notice any issue with that, unless you end up with a very vast representational space due to objects being lost into the ether.  (ie: An enemy bullet flies offscreen and continues moving forever.)  Then the quadtree will continue to grow in size, infinitely, until it takes up most of your CPU time. :)

A better solution would be to set a boundary that encapsulates some region which you are interested in.  For example, it's an infinite world but do you really need collisions with every object at every point at all times?  Likely not, so you can use setWorldBounds to some zone area the player, such as:

Code: [Select]
FlxU.setWorldBounds (player.x - FlxG.width, player.y - FlxG.height, player.x + FlxG.width, player.y + FlxG.height) ;

That would resolution collisions within a screen's worth of space near the player.

Also, note that as of 2.43, collision boundaries are set using FlxU::setWorldBounds and the camera follow boundaries are set using FlxG::followBounds.  followBounds will call setWorldBounds automatically, but the two can be complete separate things.

Shadow

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: Collision in an "infinite" world?
« Reply #3 on: Thu, Apr 14, 2011 »
Thanks ChainedLupine.

Code works great but had to use FlxG.followBounds() instead of FlxU.setWorldBounds(), not sure why but thanks.

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Collision in an "infinite" world?
« Reply #4 on: Thu, Apr 14, 2011 »
you can also move around the "collision window" on a frame to frame basis.  so for example, if i was building canabalt in a quad-tree based system, I could create the world bounds rectangle to be say the size of the screen plus some padding around the edges, and then just shift that over to match the amount of camera scroll that's occured so far.

however, if you do need to do collisions forever everywhere, the other advice you've gotten makes more sense :)

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: Collision in an "infinite" world?
« Reply #5 on: Sun, Apr 17, 2011 »
I'm doing the whole "infinite world" type game. I want to check for collisions only in the area of the screen. So I've got this code:

I have this in my create()
Code: [Select]
FlxG.camera.follow(_player, FlxCamera.STYLE_PLATFORMER);

And this in my update()

Code: [Select]
FlxG.camera.setBounds (_player.x - FlxG.width, _player.y - FlxG.height, _player.x + FlxG.width, _player.y + FlxG.height, true  )  ;
When the player goes less than 0 in the Y, so actualy going up since y=0 is the top corner, the camera goes past the player. Even with STYLE_LOCKON the camera still goes past the player.

Is there something else I should be doing if I want to make an infinite world that goes to the right and upwards infinitely?

Thanks
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

werem

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Re: Collision in an "infinite" world?
« Reply #6 on: Mon, Jun 4, 2012 »
I just tryed enlarging the collision window with FlxU.setWorldBounds but doesnt work. I'm using flixel v2.43