hit test

Open CL 3D Vertex Collision (hit testing) (Composition by dust)

Author: dust
License: MIT
Date: 2010.07.28
Compatibility: 10.6
Categories:
Required plugins:
(none)

so my daughter loves this ipad game "angry birds" its a tower defense type of game in 2d. probably made with something like cocos2d or unity3d with no z depth. so i decided to see if i could wip up something in an evening similar but in unity 3d using forces etc...

well i accomplished that task as unity3d makes things like physics very simple. i began thinking how hard would it really be to do something similar in QC. thinking i would start with a box2d approach and track vertex positions of sprite or something. i dug out some 2d fluid texture maya mel scripts and thought it might be possible to calculate a collision by converting UVs to pixels space and evaluating a hit based on texture data in realtime.

i pondered on this idea for a bit and then said to my self why not just check all the vertex points (xyz) for equivalence using the size of a model as a threshold. so using the handy get component mesh tool, i simply iterated through all my vertex's and conditionally checked x,y,z on each vertex until the two models collided. so this was fairly simple could have been done in java script instead of an iterator. unfortunately the frame rate for comparing two models with a vert count of 420 was only giving me around 30 fps on my machine. which is decent and i was happy with the fps as i could check for collisions and then apply forces based on those collisions etc.

still 30fps with only two low poly count models wasn't cutting it as quartz has all this open cl multi-threaded stuff that was built for this type of thing so i decided open cl was going to be the best way to detect if two models collide in QC.

so at first i thought i would just break down and iterate through two sets of vertexes checking for hits like i did before but after looking at the openCL reference guide and noticing all these sweet matrix and math stuff built in. i figured i could just use a distance function and check for when the models where within the threshold of there respective scales to detect a hit. so this was a different approach to the same problem. the results are similar but by doing this in on two different threads in parallel i was able to get my fps up over 100 at times in just a few lines of code.

so both these patches work for hit testing in 3d with rotations etc.. i still need to do some more testing and get a vertex by vertex comparison made in open cl instead of distance function so that eventually i may be able to do some rudimentary physics calculations with inertia and forces etc..

i don't know if this will help anyone but could be used to make a simple shooter type of game or projectile / target type of thing in 3d with your own models.

QCButton

gtoledo3's picture

I posted a QCButton patch and demo that uses QC to provide the functionality of a button, and an overview: http://www.georgetoledo.com/2010/07/qcbutton-or-standing-on-shoulders-of...

RectHit (Composition by gtoledo3)

Author: gtoledo3
License: (unknown)
Date: 2010.07.24
Compatibility: 10.6
Categories:
Required plugins:
(none)

This composition demonstrates a hit test method that works for rectangles.

It was inspired by a hit test method description that I read about in an interview with Bill Atkinson about the development of QuickDraw that uses logic based on two points to determine if a hit is inside or outside of a rectangular area.

This patch is strictly 2D compatible. Z translation as well as X, Y and Z rotations are not taken into account, and it won't work inside of 3D transforms since it doesn't "know" what happens outside of the patch.

One must hook the outputs of the HitRect patch to the corresponding inputs on a Sprite (or Billboard), for this to work properly.

There IS a more full featured hidden hit test patch (which sometimes provokes oddities in QC). This was done mostly out of curiosity after reading the interview with Bill Atkinson, and to reproduce that particular method by using QC patches.