Matthewek
Matthewek

Reputation: 1559

Android Game, 'LEVER' - box2D

I'm trying to implement 'lever' into my android game, here's image showing what do I want, showing how does it work:

1)

enter image description here

2)

enter image description here

I managed to do basic of it by using joint:

final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());
revoluteJointDef.enableLimit = true;
revoluteJointDef.upperAngle = MathUtils.degToRad(40);
revoluteJointDef.lowerAngle = MathUtils.degToRad(-40);
physicsWorld.createJoint(revoluteJointDef);   

And it works, I can move lever stick in left/right direction, and as it should you can not exceed proper angle, so this part is done. But now I'm looking for a way, when execute actions after moving this lever (for example open some doors/gate)

Here's my basic idea, how to check which part of the stick has been touched by player (left or right) by creating stick's body with this way:

enter image description here

So explaining, by adding 2 sensors, one on left side and one on right side, so in contact listener I would check which side has been touched.

But still I have no idea how to check if action should be performed, I know I could check on every update if stick angle is 40 or -40, but is it effective way? Or maybe there is better? I will be greatly thankful for any tips! Thanks

Upvotes: 2

Views: 391

Answers (2)

JohnEye
JohnEye

Reputation: 6895

You don't need to worry about efficiency here, the performance penalty for checking the angle is absolutely negligible. I measured the times needed to get the angle of a Sprite and a Body using the following code snippet:

double testEnd;
double testStart;
testStart = System.nanoTime();
for (int i = 0; i<1000000; i++) {doStuff()}
testEnd = System.nanoTime();
Log.v("speed test", "it takes "+ (testEnd - testStart)/1000000 + " nanoseconds");

So, on Desire Z it takes only 157 ns to find the angle of a Sprite and 393 ns to do the same with Body. It is also much simpler than using contact listeners. Just a side note, the angles of Sprites can be outside of (-360, +360) degrees if you rotate the Sprite.

Upvotes: 1

iforce2d
iforce2d

Reputation: 8262

Both the methods you mention would work, it's just a matter of which you find more convenient. It also seems like you already understand the implementation, so I don't think anyone could answer this question any better than to say, 'whichever suits you better'.

Putting a sensor at each end of the lever's range and using a contact listener to detect when the lever touches them is the most 'correct' way. It conveniently gives you events both when the lever touches the sensors and when they stop touching, but it's a little more work to set up.

Checking the joint angle after every time step will work fine too, but you'll need to compare the angle with the value in the previous step if you want to detect a start/finish touching 'event'. If you want a lever that can give continuous values (eg. speed control for some other object) instead of a simple on/off switch then you would be better off with this method.

Upvotes: 1

Related Questions