Game Development Reference
In-Depth Information
The buttons are simply sprites that on selection will be ticked with a green tick mark.
The
InputAdapter
interface processes the touch input from the user. The
touchup()
function in
InputAdapter
will check whether the current touch is inside any button.
If the user selects any button, then the button is set, or the program will continue
with the throw/raypicking functions.
To throw the physics object, the
applyCentralImpulse()
function is called. The
object is thrown at the direction cast by the ray.
The following is the code for ray picking:
Ray ray = cam.getPickRay(screenX, screenY);
Vector3 position = ray.origin.cpy();
rayFrom.set(ray.origin);
rayTo.set(ray.direction).scl(50f).add(rayFrom); // 50 meters
max
rayTestCB.setCollisionObject(null);
rayTestCB.setClosestHitFraction(1f);
worldInstance.getWorld().rayTest(rayFrom, rayTo, rayTestCB);
if (rayTestCB.hasHit()) {
final btCollisionObject obj =
rayTestCB.getCollisionObject();
/**
Do something
*/
}
Here, a ray is created from the input coordinates by the
getPickRay()
function.
The
rayFrom
value, a
Vector3
value, is set to the origin of the ray, whereas
rayTo
is set at a distance of
50
units from the origin point in the ray direction. The callback
object,
rayTestCB
, is cleared by setting
null
to the
setCollisionObject()
function.
Finally, by calling
rayTest()
, we check whether the ray has hit any object within the
rayFrom
and
rayTo
points.
Finally, dispose everything as shown here:
@Override
public void dispose() {
for (UserData data : UserData.data) {
data.dispose();
}
worldInstance.dispose();