cocos2d iphone - Finding point of touch on a shape -


in box2d project need create revolutejoint between 2 polygonshaped bodies @ exact point user double tapped.

i have point of touch, , list of vertices each of 2 shapes (obviously set not collide each other.) if understand correctly, point of touch in terms of world location, , vertices describing shape, reside in world. how "translate" 1 another?

for example, user double tapped @ (3.63,5.07).

the vertices shape 1 are: [(0.37485206,-0.17777264), (-0.2880008,-0.033603553), (-0.1609711,-0.2395713), (-0.04113376,-0.31708732), (0.26274818,-0.38661742), (0.35696936,-0.29765773)]

the vertices shape 2: [(-0.015462875,0.24353802), (-0.13529873,0.31698608), (-0.36852637,0.4127499), (-0.4136281,0.17009032), (-0.2880008,-0.033603553)]

the revolutejoint:

revolutejointdef jd = new revolutejointdef(); jd.bodya = touchedbodies.get(0); jd.bodyb = touchedbodies.get(1); jd.collideconnected = false; jd.localanchora.set(?????); jd.localanchorb.set(?????); world.createjoint(jd); 

any appreciated.

edit:

i made bit of progress still not there. i'm doing finding "average" location, based on shape's vertices. i'm taking first entry, , knowing vertices go counter clock wise calculate 1 middle vertice, , assumes furthest first one. i'm doing average of two, , setting joint there.

here's code:

shape shape1 = touchedfixtures.get(0).getshape(); vec2 oneaverage = null; if(shape1.getclass() == polygonshape.class) {     polygonshape s = (polygonshape)shape1;     vec2[] vs = s.m_vertices;     list<vec2> vsnonzero = new linkedlist<vec2>();     for(int i=0; i<vs.length; i++) {         if(vs[i].x != 0 || vs[i].y != 0) {             vsnonzero.add(vs[i]);         }     }     vec2 oneone = vsnonzero.get(0);     vec2 onemiddle = vsnonzero.get(math.round(vsnonzero.size()/2));     oneaverage = new vec2((oneone.x + onemiddle.x)/2, (oneone.y + onemiddle.y)/2); } 

and joint's local anchor:

jointdef.localanchora.set(oneaverage); 

i'm doing same second body of joint. finds decent proximity, it's not perfect, if shape larger.

any idea how go closer actual touched point? need shape.gettouchedpoint() api doesn't seem provide it.

this how it:

jd.localanchora = touchedbodies.get(0).getlocalpoint(locationworld); jd.localanchorb = touchedbodies.get(1).getlocalpoint(locationworld); 

touchedbodies above list 2 bodies touched, above can replaced this:

jd.localanchora = bodya.getlocalpoint(locationworld); jd.localanchorb = bodyb.getlocalpoint(locationworld); 

thank iforce2d providing answer here (http://www.cocos2d-iphone.org/forum/topic/343137#post-507904) explanation, , c++ code.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -