Box2D Forums

It is currently Wed May 22, 2013 5:46 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Dec 02, 2010 8:46 pm 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
I'm working on a platformer game, where the player's character needs to be able to hold on to an object, either to drag it somewhere or be dragged with it. So, when the player presses the grab button, I check the ContactLists for sensors positioned left and right of the character's solid geometry, to see if there's anything to grab. If there is, I want to construct a distance joint (or maybe revolute?) between the character and the object...

Code:
         var sensor:BodyShape = (direction < 0) ? left : right;
         var list:ContactList = contacts[sensor];
         list.clean();
         
         if (list.values.length > 0) {
            var contact:ContactEvent = list.values[0];
            var shape:BodyShape = contact.relatedObject as BodyShape;
            grabJoint = new Joint();
            grabJoint.type = "Distance";
            grabJoint.target1Object = middle;
            grabJoint.target2Object = shape;
            grabJoint.x = (middle.x + shape.x) * 0.5;
            grabJoint.y = (middle.y + shape.y) * 0.5;
            world.addChild(grabJoint);
         }


...but this has absolutely no effect whatsoever-- no errors are thrown, and the joint does not function. Inspecting the value of shape, reveals that it's frequently an object that the character is nowhere near, although even when it is the correct object the joint doesn't function.


Top
 Profile  
 
PostPosted: Fri Dec 03, 2010 2:38 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Try this instead:
Code:
grabJoint.bodyShape1 = middle;
grabJoint.bodyShape2 = shape;


I'm going to fix it in the next commit so setting target1Object will also work. I guess I intended bodyShape1/2 to be used in code, but there's no way to set the second target as a joint if you want to (this doesn't effect you).


Top
 Profile  
 
PostPosted: Fri Dec 03, 2010 5:06 pm 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
Great! I can successfully make a joint that works as expected. Thank you.

I still have an issue with the results in my ContactList. Sometimes, the shape that appears in that list is nowhere near the character, and the object that should be grabbed, isn't even in the ContactList. Any ideas?


Top
 Profile  
 
PostPosted: Fri Dec 03, 2010 5:20 pm 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
...ah, looks like the same problem sketchbookgames is dicussing in this thread:
viewtopic.php?f=8&t=5649

...I'll tune in over there for the subject of ContactLists.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group