Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Box 2D - 2 Pivots and other questions

Author
Message
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Oct 2010 21:28
I have a couple of questions on the best way to im[lement a couple of my requirements...

1. 2 Pivots on a horizontal bar. Imagine the bat in Pong, but horizontal on the floor. I want to be able to flip it left or right by 15 degrees, a bit like a pinball flipper. When it flips right, it must pivot on the left, and when I flip it left, it must pivot on the right. That is, it's base always remains at "0" and one of the ends is raised higher to make it flip. What's the best way to achieve this. Right now, based on a central pivot, I just set the body transform +/- 15 degrees.


2. Mouse Joint. What is the purpose of first body in a mouse joint? I currently have it set to the ground, but for no reason other than I had to pick an object. I've also found that the mouse joint is capable of pulling the attached object through other solid objects. For example, if I set the mouse joint to ground - 20 pixels, it pulls the bar through the floor. What do I need to know about mouse joints to make them work?

3. Contact Touch. Has anyone found that contact "Touches" can be overtriggered? I have a situation where every now and then, the number of touches can be a dozen or more for one collision where the objects bounce off in different directions.

Thanks in Advance.

BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Oct 2010 17:12
Another question to add to the list:

I want to get the point of contact between 2 bodies, so I have used the following:


b2FindBodyContacts myBody
contact = b2GetContact()
while contact > 0
x = b2GetContactManifoldWorldPointX(contact)
y = b2GetContactManifoldWorldPointY(contact)
`... do stuff
contact = b2GetContact()
endwhile

but these return zero. How do I get the contact point?

Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Oct 2010 18:13
Hi,
1) You can create the body as a kinematic body (as opposed to a static or dynamic body). You can then directly position and rotate the body how you want, without the simulation becoming unstable. It will not be affected by other bodies colliding with it.

To acheive what you want, a small bit of trig is necessary. If you need any help with that just ask.

2) The first body in a mouse joint has no real purpose. The underlying Box2D API requires you to specify a body, so I had a choice: either automatically create a static body in every new world, and use that as the first body when creating mouse joints, or make the user specify one.

The problem with the first option is that it affects other commands. The total number of bodies will be one more than the user expects. When looping through bodies in a world, this special body would have to be skipped, etc. Overall, I decided it would be better to keep to the original API.

As for your problems with mouse joints: make sure you haven't set the optional "collide connected" flag to zero when you create the mouse joint.
If that's not the problem, try reducing the force applied by the mouse joint (b2SetMouseJointMaxForce).

3/4) Change your loop to resemble the following:


For simplicity and performance, contacts work using a "current contact" instead of an ID. The b2FindXXXContacts commands generate an internal list of contacts. b2GetContact() removes the first element from this list, and makes it the current contact. If the list was empty it returns zero, otherwise it returns one.

The lifetime of contacts is so short and unpredictable that it would waste time generating IDs for all of them.

[b]
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Oct 2010 19:07
thanks Diggsey

The collision point is sorted. To keep the rest of the logic in line the answer is simply:

x = b2GetContactManifoldWorldPointX(0)

Now to sort the other points, this is very easy to implement, nice work on the plugin

Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 29th Oct 2010 13:53 Edited at: 29th Oct 2010 13:58
Make sure you check that there is at least one contact point before doing that though. I'm not sure it ever happens, but the Box2D docs say that there can either be 0, 1 or 2 contact points per contact.

edit:
I though of another way to do the pivoting. If you create a hidden sensor body behind the paddle, attached at one end to the paddle with a hinge, and the other end to the ground with a hinge, you can make it swing either way simply by putting motors on the hinges.
Give the hidden body a fairly small mass, so that the paddle behaves the same way for both sides.

[b]

Login to post a reply

Server time is: 2025-08-08 17:30:19
Your offset time is: 2025-08-08 17:30:19