Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite collection implementation?  (Read 2188 times)

0 Members and 1 Guest are viewing this topic.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Sprite collection implementation?
« on: October 27, 2011, 10:01:50 pm »
Currently I have a vector that contains all of Blocks to render on the screen, and another vector that contains all of the FloatRects for collision detection.  

I loop through each sprite in the block vector:

Code: [Select]

App.Draw(Blocks[i]);


And for collision, I loop through the FloatRects vector and check my collision logic against each part.

I don't plan on updating the ground or FloatRect vector after everything is generated, so I'm wondering if it's possible to make them into a collection so that I only need to draw one and check against one.  Essentially create a large image to draw, and create an obscure shape with the FloatRect.  

I'm not too sure about the FloatRect, because I assume the rectangle must be a rectangle.

I'm only interested in implementing this because occasionally my friends will fall through the ground, or the performance will get very slow occasionally.  I haven't encountered falling through the ground, but I assume it's because it hasn't reached that point in iteration just yet.

I was also thinking about looping through Blocks that are only in the view, but I'm not sure how to exclude blocks that aren't in the view.  I know how I can check to see if objects are viewable the player, but I'm not sure about removing them from the vector or excluding them from the loop.

Thanks for any help!

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Sprite collection implementation?
« Reply #1 on: October 27, 2011, 11:36:14 pm »
You could create a new custom class containing a SFML sprite and one rectangle. And update both of them at the same time, even better, you can update the rectangle using sprite class methods for getting coords.

For checking to exclude some blocks from the vector, you could calculate the distance to your player, or check if they are inside the view. But, anyway you have to check ALL blocks.

But if you do a collision check against every block in your world, you'll get a slow program. So you need to priorize and partition the world space. Use an orthogonal 2D grid or BSP trees. (http://en.wikipedia.org/wiki/Binary_space_partitioning)

Now I haven't time, but maybe I'll do a little example later.

Let your imagination fly..

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Sprite collection implementation?
« Reply #2 on: October 29, 2011, 12:30:26 am »
Quote from: "julen26"
You could create a new custom class containing a SFML sprite and one rectangle. And update both of them at the same time, even better, you can update the rectangle using sprite class methods for getting coords.

For checking to exclude some blocks from the vector, you could calculate the distance to your player, or check if they are inside the view. But, anyway you have to check ALL blocks.

But if you do a collision check against every block in your world, you'll get a slow program. So you need to priorize and partition the world space. Use an orthogonal 2D grid or BSP trees. (http://en.wikipedia.org/wiki/Binary_space_partitioning)

Now I haven't time, but maybe I'll do a little example later.

Let your imagination fly..


Yup, that's actually what I used to do.  I check if there are objects outside of my CameraView rectangle, and if they are, don't draw them.  I think I've noticed a slight performance increase, I suppose it doesn't take long to loop through the for loop, only drawing and updating the objects requires more time.

Thanks!

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Sprite collection implementation?
« Reply #3 on: October 29, 2011, 12:52:43 pm »
Quote from: "fatum"

Yup, that's actually what I used to do.  I check if there are objects outside of my CameraView rectangle, and if they are, don't draw them.  I think I've noticed a slight performance increase, I suppose it doesn't take long to loop through the for loop, only drawing and updating the objects requires more time.

Thanks!


Anyway, I think SFML does it automatically, so you shouldn't worry if you try to draw something outside the view.

For the collision checking, implementing BSP trees doesn't become easy and one of the best solutions in 2D games usually is to partition the world space with a grid.



You can implement this using arrays or a hash table. To get the grid cell where you are, you can divide your player's position (x, y) by grid size. This way you ONLY check collisions between objects in that cell.

Something like this in pseudo-code, note that you have to do an int conversion:
Code: [Select]
gridX = obj.x / gridSize.x
gridY = obj.y / gridSize.y


Good luck!

 

anything