Throwing a ball not working as expected

jojo
Posts: 4
Joined: Wed Mar 28, 2012 11:40 am

Throwing a ball not working as expected

Post by jojo »

Hy guys. I'm developing a simple phone-based basketball game and I have a problem with user input and handling it.

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;
			}
		}
	}
regards
CookieMonster
Posts: 49
Joined: Sun Jan 29, 2012 10:01 pm

Re: Throwing a ball not working as expected

Post by CookieMonster »

Some bullet interfaces do not have a default gravity.
jojo
Posts: 4
Joined: Wed Mar 28, 2012 11:40 am

Re: Throwing a ball not working as expected

Post by jojo »

Hy, thanks for reply.
In my initPhysic() method i set gravity like this, so I think this should be ok

Code: Select all

world.setGravity(new Vector3f(0, -9.81f, 0));
..and then in update loop I call this every frame:

Code: Select all

world.applyGravity();
world.stepSimulation(dt);
CookieMonster
Posts: 49
Joined: Sun Jan 29, 2012 10:01 pm

Re: Throwing a ball not working as expected

Post by CookieMonster »

applyGravity is already called from stepSimulation.

Code: Select all

int	btDiscreteDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep)
{
	startProfiling(timeStep);
	BT_PROFILE("stepSimulation");
	int numSimulationSubSteps = 0;
	if (maxSubSteps)
	{
		//fixed timestep with interpolation
		m_localTime += timeStep;
		if (m_localTime >= fixedTimeStep)
		{
			numSimulationSubSteps = int( m_localTime / fixedTimeStep);
			m_localTime -= numSimulationSubSteps * fixedTimeStep;
		}
	} else
	{
		//variable timestep
		fixedTimeStep = timeStep;
		m_localTime = timeStep;
		if (btFuzzyZero(timeStep))
		{
			numSimulationSubSteps = 0;
			maxSubSteps = 0;
		} else
		{
			numSimulationSubSteps = 1;
			maxSubSteps = 1;
		}
	}
	//process some debugging flags
	if (getDebugDrawer())
	{
		btIDebugDraw* debugDrawer = getDebugDrawer ();
		gDisableDeactivation = (debugDrawer->getDebugMode() & btIDebugDraw::DBG_NoDeactivation) != 0;
	}
	if (numSimulationSubSteps)
	{
		//clamp the number of substeps, to prevent simulation grinding spiralling down to a halt
		int clampedSimulationSteps = (numSimulationSubSteps > maxSubSteps)? maxSubSteps : numSimulationSubSteps;
		saveKinematicState(fixedTimeStep*clampedSimulationSteps);
		applyGravity();
		for (int i=0;i<clampedSimulationSteps;i++)
		{
			internalSingleStepSimulation(fixedTimeStep);
			synchronizeMotionStates();
		}
	} else
	{
		synchronizeMotionStates();
	}
	clearForces();
#ifndef BT_NO_PROFILE
	CProfileManager::Increment_Frame_Counter();
#endif //BT_NO_PROFILE
	return numSimulationSubSteps;
}
jojo
Posts: 4
Joined: Wed Mar 28, 2012 11:40 am

Re: Throwing a ball not working as expected

Post by jojo »

Oh I see, I should have looked there, thanks.
I think it's maybe a little bit better, but still it goes very high and only a little bit forward.

I was thinking is this the right approach to use constraint here or should I probably implement some kind of cos/sin path for my ball to follow, if yes, how could I do this since everything is physics driven.

thanks for help
regard
CookieMonster
Posts: 49
Joined: Sun Jan 29, 2012 10:01 pm

Re: Throwing a ball not working as expected

Post by CookieMonster »

I would make my own constraint by measuring distance and applying a limited force against the target.
jojo
Posts: 4
Joined: Wed Mar 28, 2012 11:40 am

Re: Throwing a ball not working as expected

Post by jojo »

Thanks for info CookieMonster. That sounds like a wonderful idea.
I know I would have to implement TypedConstraint but how exactly I would do it to achieve my goal is unknown to me at this point because I'm fairly new to gamedev. So any examples or help would be greatly appreciated.
As per jBullet which is based on Bullet 2.72 It looks I should implement at least this two abstract methods buildJacobian() and solveConstraint(float);

regards