I want the user to tap on my player then move finger towards basket and then untap.
Once the user untaps the finger my player on basketball court would throw the ball towards the hoop/basket.
Currently I have dragging enabled for testing purposes but mine main problem is that's really really hard to throw into hoop and get some points. Ball goes kinda high and quick.
I don't know what I'm doing wrong but it looks like i may have some parameter settings issues or my code for picking and throwing is wrong for this, I used P2P constraint.
My ball settings are as follows: ballMass=0.65, ballRadius=0.13, bodyRestitution=0.6, bodyDamping=0.3, bodyFriction=0.6, and I set restitution on static objects to 1.
Can somebody be please kind enough and help me resolve this issue.
I should probably note that I'm using jBullet and libGDX.
And here's the code for picking and throwing I have so far:
Code: Select all
private void throwBall()
{
if (ball_inflight)
return;
Input in = Gdx.input;
Camera cam = Renderer.getInstance().getCamera();
Vector3f camPos = cvt(cam.position); //cvt converts libgdx vector to java vecmath vector
Ray r = cam.getPickRay(in.getX(), in.getY());
Vector3f rayTo = new Vector3f(cvt(r.origin));
// Finger just touched the screen
if (in.justTouched())
{
touchdown = true;
if (player.touched() && ball != null)
{
pickedBody = ball;
Vector3f pickPos = pickedBody.getWorldTransform(new Transform()).origin;
// pickedBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
Transform tmpTrans = pickedBody.getCenterOfMassTransform(new Transform());
tmpTrans.inverse();
Vector3f localPivot = new Vector3f(pickPos);
tmpTrans.transform(localPivot);
Point2PointConstraint p2p = new Point2PointConstraint(pickedBody, localPivot);
p2p.setting.impulseClamp = 3f;
p2p.setting.damping = 1f;
world.addConstraint(p2p);
pickConstraint = p2p;
// save mouse position for dragging
BulletStats.gOldPickingPos.set(rayTo);
Vector3f tmp = new Vector3f();
tmp.sub(pickPos, camPos);
BulletStats.gOldPickingDist = tmp.length();
// very weak constraint for picking
// p2p.setting.tau = 0.5f;
}
}
// Touched-down finger movement
if (in.isTouched())
{
touchdown = true;
if (pickConstraint != null)
{
Point2PointConstraint p2p = (Point2PointConstraint) pickConstraint;
// move the constraint pivot
if (p2p != null)
{
// keep it at the same picking distance
Vector3f dir = new Vector3f();
dir.sub(rayTo, camPos);
dir.normalize();
dir.scale(BulletStats.gOldPickingDist);
Vector3f newPos = new Vector3f();
newPos.add(camPos, dir);
p2p.setPivotB(newPos);
}
}
}
// Finger release
if (touchdown && !in.justTouched() && !in.isTouched())
{
touchdown = false;
if (pickConstraint != null)
{
world.removeConstraint(pickConstraint);
pickConstraint = null;
pickedBody.forceActivationState(CollisionObject.ACTIVE_TAG);
pickedBody.setDeactivationTime(0f);
pickedBody = null;
player.startAnimation();
ball_inflight = true;
}
}
}