Extending ti.box2d : Mouse Joints

It is common in physics based games to need to be able to drag bodies around. However, setting the position of a body instantaneously based on the current touch position can introduce excessive forces to the physics simulation, and break it. So we need another way to solve this problem…

Enter Box2D’s mouse joint. In our modifications to the ti.box2d module, we have added the mouse joint for use in Titanium games using Box2D. Creating a mouse joint is simple, as shown in the code below.


var mouseJoint = world.createMouseJoint ( groundBody, draggableBody, {
maxForce : 1000,
dampingRatio : 0.6,
frequencyHz : 1.0,
collideConnected : true
});

In the above code sample , world is the result of a call to ti.box2d’s createWorld function. All joints require 2 bodies, even though we generally think of the mouse joint as really only effecting a single body. When creating mouse joints, that second body (the first parameter in this function) is generally a static ground body. The second parameter in our createMouseJoint function is the body that we want to drag around.

We’ll come back to the other options in a moment. First, let’s actually drag the body around. In order to have the body dragged towards a point, we must call the setTarget function on the mouse joint with the x and y coordinates that we want to drag the body to. You’ll generally want to do this in an update loop or anytime the mouse (touch point) moves.


view.addEventListener('touchmove', function(e)
{
mouseJoint.setTarget(e.x, e.y);
});

That’s all you need to start dragging a body around. Now lets circle back to those other options in the creation of the mouse joint. The maxForce property is the maximum amount of force that will be applied to the body in order to get it to its target point. collideConnected indicates whether the draggableBody should be allowed to collide with the groundBody.

The last two properties, dampingRatio and frequencyHz, are the parameters you will need to tweak the most to get the kind of dragging feel that you want. If you want very tight dragging, you will need a higher frequency and damping ratio. If you want some oscillation after the body reaches it’s target point, you will want a damping ratio less than 1.0. Check out the Box2D Manual to learn more about these options.

ADB with multiple devices

Have you ever had multiple emulators or physical devices or a combination of the two attached at once? It’s frustrating to shut off or unplug your devices in order to single out the one that needs to be targeted. Look no further, adb includes a command line switch to choose the specific device you want to target.

Let’s begin by listing our currently attached devices.

$ adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
HT09PR217646 device

As you can see, we have three devices attached to our system. Two emulators and one physical phone. Now let’s logcat our physical device because it’s running our application that we want to debug. Just pass the -d switch to adb to target our device.

$ adb -d logcat

If we had a single emulator instance and one or more physical devices attached we could pass in the -e switch to adb which targets emulator.

$ adb -e logcat

The final switch we can use for device targeting is the -s switch, which stands for serial number.

$ adb -s emulator-5554 logcat
$ adb -s emulator-5556 logcat
$ adb -s HT09PR217646 logcat

March Mobile News & Trends

We’ve got some hot news: our buddies at Appcelerator won the GSMA Global Mobile 2012 Best Cloud Based Tech Award in Barcelona last week at the Mobile World Congress in Barcelona. The judge’s comment on the award announcement: “Quintessential cloud, write once, deploy many times drives speed and cost reductions.”

Titanium is indeed a powerful development platform that allows you to build and deploy mobile apps quickly that will work on any platform. We’re proud of Appcelerator’s win, since we can bask in the glow ourselves as Titanium developers and trainers. Our CEO, Edwin Huertas, just picked up a Master Certification in Titanium last month, and on the way back home to Richmond he stopped by Accenture-Austin to teach mobile development and best practices to their developer team.

So that’s the local mobile news, with a global spin. Here are some other trends we’re watching:

iPad 3 launch

Apple is launching the iPad 3 this week (if they want to take advantage of SXSW crowd hype) or next week. We’ll be watching closely to see what changes are really in the tablet after all the pre-launch speculation is either proven or myth-busted. One big question: will it include LTE? GigaOm’s Kevin Fitchard has a great post on what LTE’s inclusion, or exclusion, would mean for the mobile space. Stay tuned for that answer.

Augmented reality for navigation

Texting while driving is a bad idea. There are, however, a few apps in development that use augmented reality to help drivers navigate traffic by using the camera lens on their smartphone. One in particular got a lot of attention at Mobile World Congress last month: iOnRoad, an Android app developed by an Israeli company who envisions the technology being integrated into existing vehicle navigation systems.

Are you looking to build a mobile business?

That’s one trend that will continue to grow for the foreseeable future, as more people across the globe get connected via mobile devices even when terrestrial connections aren’t available. If you need help exploring the idea of making your business, your idea, or your cause mobile, we’ve got expertise and the tech savvy to make it happen. And the training programs to help you build your own mobile developer team.

Got a trend you’re watching? Let us know!