pixel perfect collision

BlitzMax Forums/BlitzMax Programming/pixel perfect collision

Cruis.In(Posted 2013) [#1]
Hey there, anyone ever had trouble with pixel perfect collision?

I've used imagescollide2, where you specify the rotation and scale of your images to the function, so no matter what they are the function knows. Rather than having to have all your images synchronized in rotation/scale with using imagescollide.

Trouble is when I zoom out, its off a bit. I scale out to about 0.3 and the projectiles miss like tips of the object, have to aim more for the centre. fully zoomed in to scale 2.0 and the projectiles collide slightly off from the target.

I then tried collideimage, since when its written to the mask, it is written at its current transform, scale/zoom etc. Same exact result as imagescollide.

So two different ways and the same result, any idea on what I could be doing wrong?


Who was John Galt?(Posted 2013) [#2]
Not sure. Can you post a simple code example?


Cruis.In(Posted 2013) [#3]


the collision is fine by default and pixel perfect. the execution starts with everything at scale 1.0

the scale is set according to the viewfactor. There are no errors with the scaling as everything scales in/out based on viewfactor, so it's not seeing different scales for the different images.

the missiles are scaled according to viewfactor and so is the ships.

Last edited 2013


Midimaster(Posted 2013) [#4]
Maybe I'm wrong, but wasn't it like that:

If the scaling code line is inside your graphic creation function and the collision function is not... you have to repeat the SCALE command before testing the collision.

or you have to test collision even in the moment when you draw the objects.


Cruis.In(Posted 2013) [#5]
at the moment I have removed all scale commands from before drawing graphics. All scaling is done from the main loop.

setscale viewfactor, viewfactor

which scales everything.

outside the loop i have globaled viewfactor to be 1, so eevrything starts off normal size.

I did a test whereby i set the scale of the projectile to be .3 from before its draw in the draw method, and the scale of the ship graphic to be .3 in its draw method, then set the imagescollide function parameter to check at scale .3

still slightly awry. could my actual graphic be the problem?

Last edited 2013


After these tests, this led me to believe imagescollide wasn't the issue, nor the use of it perse. the transforms were all correct.

I'm drawing my stuff based on the scale, so even though there world coordinates do not change when zooming in/out (scaling up/down)... the drawing coordinates of the image themsevles change because they are taking in the *viewfactor into account. And I had the imagescollide checking for the world coordinates only not the drawing coordinates. Before I added zooming the drawing coordinates used to be only x,y so the collision test was written for x,y. But now I am drawing the images at an offset and had not taken that into account.

So I created a field for each offset, offsetx and offsety and stored all the offset data into the fields of each object, and draw at the offset instead of just x,y and check the collision at the offsets and voila. Since my collision function is separate, it has to be able to see the fields of the object.

my offset is this:

offset = ship.x + ( (x - ship.x) *viewfactor

where x is the object, and you are offsetting that object in relation to where ship.x is (which is the player) since everything is being draw and offset according to the player who is drawn fixed at the centre of the screen.

thanks for your help!

Last edited 2013


Alberto-Diablo(Posted 2013) [#6]
publish a small example of the problem.


Cruis.In(Posted 2013) [#7]
i got it fixed thanks. I had edited my post, to show where I went wrong! Thanks.

Last edited 3 days ago