# Physics Simulation Forum

 All times are UTC

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Throwing a ball not working as expectedPosted: Sun Apr 01, 2012 6:27 pm

Joined: Wed Mar 28, 2012 11:40 am
Posts: 4
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:
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;

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();
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

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Mon Apr 02, 2012 9:12 am

Joined: Sun Jan 29, 2012 10:01 pm
Posts: 49
Some bullet interfaces do not have a default gravity.

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Mon Apr 02, 2012 3:18 pm

Joined: Wed Mar 28, 2012 11:40 am
Posts: 4
In my initPhysic() method i set gravity like this, so I think this should be ok
Code:
world.setGravity(new Vector3f(0, -9.81f, 0));

..and then in update loop I call this every frame:
Code:
world.applyGravity();
world.stepSimulation(dt);

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Mon Apr 02, 2012 4:42 pm

Joined: Sun Jan 29, 2012 10:01 pm
Posts: 49
applyGravity is already called from stepSimulation.

Code:
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;
}

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Mon Apr 02, 2012 7:33 pm

Joined: Wed Mar 28, 2012 11:40 am
Posts: 4
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

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Mon Apr 02, 2012 8:19 pm

Joined: Sun Jan 29, 2012 10:01 pm
Posts: 49
I would make my own constraint by measuring distance and applying a limited force against the target.

Top

 Post subject: Re: Throwing a ball not working as expectedPosted: Thu Apr 05, 2012 6:48 am

Joined: Wed Mar 28, 2012 11:40 am
Posts: 4
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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 7 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Google [Bot] and 9 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ BULLET PHYSICS LIBRARY USERS    General Bullet Physics Support and Feedback    Release Announcements    Applications, Games, Demos or Movies using Bullet PHYSICS AUTHORING TOOLS, SERIALIZATION AND STANDARDS    Physics authoring tools, serialization, standards and related topics RESEARCH AND DEVELOPMENT IN COLLISION DETECTION & PHYSICS. Don't post Bullet support questions here!    Research and development discussion about Collision Detection and Physics Simulation    Links, Papers, Libraries, Demos, Movies, Comparisons       Non-technical forum and license/patent discussion    Career Opportunities