Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411518 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 28, 2024, 03:29:59 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperArt (Moderator: JWK5)Convert image to a number of squares?
Pages: [1]
Print
Author Topic: Convert image to a number of squares?  (Read 3994 times)
Christian Knudsen
Level 10
*****



View Profile WWW
« on: August 27, 2009, 09:46:27 AM »

I'm working on a 2D game using sprites. Instead of making exact collision masks for each sprite, I just want to assign a number of rectangles to each sprite, so that if the object that I'm checking for collision is within any of these squares, it's colliding with my sprite. I've tried drawing the squares on top of my sprite image manually and then inputing the coordinates of each square into my code, but drawing these squares takes a lot of time, so I was wondering if there's a program that can convert an image into X number of squares (all of them within the edges of the image sprite), or a plugin/script for GIMP that can do that?

-->

(Hope this makes sense!)
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Massena
Level 4
****


Satisfied.


View Profile
« Reply #1 on: August 30, 2009, 05:37:13 AM »



I made this by gradually evolving a picture using only quads, but it doesn't help you at all  :D

In all seriousness though, your problem may be a variation of the packing problem which is, I think, NP-hard.

edit: You should probably just try to implement pixel perfect collisions.
« Last Edit: August 30, 2009, 05:40:58 AM by Massena » Logged

Farbs
Man
Level 10
*


/Farbs


View Profile WWW
« Reply #2 on: August 30, 2009, 06:16:13 AM »

It's not exactly what you're after, but I've previously had luck exporting a path from GIMP to svg and using the svg data to build collision geometry.
Logged
Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #3 on: August 30, 2009, 08:59:49 AM »

I made this by gradually evolving a picture using only quads, but it doesn't help you at all  :D

In all seriousness though, your problem may be a variation of the packing problem which is, I think, NP-hard.

edit: You should probably just try to implement pixel perfect collisions.

I actually remember reading about this in another thread and actually went looking for it before posting my original message. Smiley Looks very nice!

I'm currently trying to make a program to do what I want. The algorithm I'm currently pursuing is dividing the image into a bunch of squares (4x4 pixels, as that's the collision movement resolution I'm using), then mark all squares that only have pixels with alpha > 0.5, and then connecting these marked squares into larger squares. I think this should work in theory, but time will tell.

As to pixel perfect collision: that will be too slow for what I have in mind (I'll have to do a lot of collision checking). My multiple squares approach worked really well in the test I did (where I manually calculated the squares). It's really fast and I actually like it better than pixel perfect collision, as it doesn't always register the hit as soon as the bullet reaches a pixel, but sometimes further "inside" the image -- this almost gives it a 3D effect, as you seem to not just be hitting the edges of the sprite, but stuff on the sprite.

It's not exactly what you're after, but I've previously had luck exporting a path from GIMP to svg and using the svg data to build collision geometry.

Sounds interesting, but also sounds slower than just checking the bullet X,Y coordinates against 10-12 squares...?
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #4 on: September 01, 2009, 11:27:15 AM »

I've managed to make my own program to generate collision boxes. Works pretty well:

Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Radnom
Level 8
***


BANNED


View Profile
« Reply #5 on: September 01, 2009, 02:20:26 PM »

Hm, would converting to a bunch of circles potentially be more efficient? Not as accurate for squarish shapes I guess, but it could potentially be faster for collisions.
Logged

Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #6 on: September 01, 2009, 03:32:09 PM »

I imagine it would be a lot slower. For squares you just check that the object is within the square. No calculations required, as it's just comparing X and Y coordinates. With a circle, you have to calculate the distance from object to circle center. A lot slower.
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
nihilocrat
Level 10
*****


Full of stars.


View Profile WWW
« Reply #7 on: September 01, 2009, 06:41:54 PM »

I've managed to make my own program to generate collision boxes. Works pretty well:



Holy crap, that's a good idea! It looks like you might have divided the image into a grid, and if it had more than X number of filled pixels, you treated that square as "filled". From there, you simplified adjacent squares into rectangles. Is this even remotely correct?
Logged

Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #8 on: September 02, 2009, 01:20:38 AM »

Spot-on! Grin
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic