Pixel Perfect Collision Example

Post your own tutorials, guides and demos.

Post » Sun Jan 11, 2009 7:13 pm

I whipped up a quick little example for making pixel perfect collisions using TimeDelta and loops. Using this method, objects traveling at extreme speeds will never miss collisions, unlike using TimeDelta by itself, but will still travel at the same speed no matter what the framerate.

Pixel Perfect Collision Example

Tell me what you all think. :)
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sun Jan 11, 2009 7:40 pm

Nice example! :) The effect is more noticable if you use a low fixed framerate eg. 15 fps - the blue object passes right through the barrier.

You could neaten the events using the 'repeat' condition instead of a loop, which keeps everything in the same event, eg:

+ Right arrow key is down
----+ Repeat 5000 / redguy('Accuracy') times
----+ Red does not overlap wall
----> Set X to .X + (TimeDelta * redguy('accuracy'))

This keeps everything inside one subevent. I don't really like the action-based loops - I'm not sure they deserve to be there - I think using condition loops is always superior. The runtime can run condition loops faster, and it keeps events in one place (it's bad for readability that 'start loop' runs the loop body somewhere else).

Also, your example doesn't take in to account if it's running too many loops - if each iteration the object is being moved less than one pixel, it is a waste of processing to check the same pixel twice.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Mon Jan 12, 2009 3:51 am

Hehe looks like people are gonna be needing my move safely extension ported to construct. Although I wonder if it would be better to just add an action like 'move x,y pixels, stopping if you hit >>object type<<' and it would stop the object if it overlapped. Would be a lot easier for people to implement into games etc.
B
4
S
2
G
5
Posts: 641
Reputation: 3,011

Post » Mon Jan 12, 2009 6:27 am

[quote="David":14xmb026]Hehe looks like people are gonna be needing my move safely extension ported to construct. Although I wonder if it would be better to just add an action like 'move x,y pixels, stopping if you hit >>object type<<' and it would stop the object if it overlapped. Would be a lot easier for people to implement into games etc.[/quote:14xmb026]

And maybe it could have a callback-style condition too? 'Move x,y pixels stopping if condition A is true' where condition A would be a function. That would make it very flexible... no, wait, is it possible for extensions to rely other extensions, for example, function object?
B
3
S
2
G
5
Posts: 263
Reputation: 2,201

Post » Mon Jan 12, 2009 11:53 am

Are distance/proximity calculations a cheap way for doing collision detections. And How about distance based acuracy?
B
30
S
15
G
10
Posts: 285
Reputation: 7,144

Post » Tue Jan 13, 2009 1:01 am

That's a good idea, Ashley. :) It certainly helps to condense the events.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Wed Jun 17, 2009 11:06 pm

Nice, this works with 1x1 sprites at 9999 speed. What kind of preformance hit do you get from using this will all your movement events? And since this was posted a while ago is this now obsolete?
B
2
S
2
G
4
Posts: 259
Reputation: 1,968

Post » Wed Jun 17, 2009 11:38 pm

Man, I almost forgot I made this. :P The performance hit really depends on how many objects you're using this method on and what the accuracy level is. An accuracy level of 256 has a minimum effect on performance even across 100 objects.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Thu Jun 18, 2009 10:24 am

Hmm... Guys... I don't really get how it works.

Anyone can explain?
B
2
S
2
G
5
Posts: 512
Reputation: 2,674

Post » Thu Jun 18, 2009 10:31 am

It breaks the distance down into smaller parts. In this case, it breaks down the 5000 pixels distance it would make within one tick into numerous parts and loops through each part, checking for collision, and stopping when it detects the collision.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Next

Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests