Author Topic: What's the use of FlxG.worldBounds?  (Read 2305 times)

Galtrhan

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
What's the use of FlxG.worldBounds?
« on: Sun, Aug 14, 2011 »
Seems that it affects collission detection in my space shooter but i'd like to know what it really does?

I have a very large map and for now I have set world bounds to FlxRect(-16000, -16000, 32000, 32000);.

And how map size affects performance?
« Last Edit: Sun, Aug 14, 2011 by Galtrhan »

Titch

  • Contributor
  • ****
  • Posts: 270
  • Karma: +0/-0
  • Thing with the guy in the place.
    • View Profile
Re: What's the use of FlxG.worldBounds?
« Reply #1 on: Mon, Aug 15, 2011 »
I believe bounds lock the camera scroll area and the quad tree solving area.
Free cake whippings every day at #flixel on irc.freenode.net.

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: What's the use of FlxG.worldBounds?
« Reply #2 on: Mon, Aug 15, 2011 »
The collision detection of Flixel is set up to work within a rectangle. Every time you check for a collision, everything in this rectangle is tested using a quad-tree. The larger this rectangle, the more collision checks are done in the game. If you have a large world bound and a lot of objects, your game is likely to get sluggish.

Depending on the type of game you have, you can consider updating bounds more often (like only the area the camera is showing).

Galtrhan

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: What's the use of FlxG.worldBounds?
« Reply #3 on: Mon, Aug 15, 2011 »
Ok, so my game shouldn't be sluggish if I update worldBounds in my PlayState like in the following example?
Code: [Select]
public override function update():void {
  // added 10px margin so the bullets could still explode if off screen
  // I have big explosion sprites that could still be seen if bullets hit smth just by the visible area :D
  FlxG.worldBounds = new FlxRect((Player.x - FlxG.width / 2) - 10, (Player.y - FlxG.height / 2) - 10, FlxG.width + 20, FlxG.height + 20);
  // all overlap checks
  FlxG.camera.follow(Player);
  super.update();
}

I haven't completed the map designs yet to test this completely, but so far it seems to be stable 57 to 60 fps and 25MB memory use.

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: What's the use of FlxG.worldBounds?
« Reply #4 on: Tue, Aug 16, 2011 »
Yes that should be good.

One thing you can do to optimize it further is to create a single FlxRect var at the start, and just change the the x, y, width and height in update. Creating a new instance in update is a bad practice because you will end up creating a lot more objects than needed.