Ball Physics - Need Help About Force

Hello all,

I’m making a quick game for my brother and i have a problem now, i’m calculating hit point of a ball using raycast and got Vector3,

Now i’m using bullet physics and need to know how to apply simple force to ball? (relative to hit position, for ex: Left bottom corner, ball supposed to go right upper corner)

How can i calculate Force vector? i got Position vector from code.

Regards.

When you apply a force to some rigidBody, you can choos where to apply the force.
See http://hub.jmonkeyengine.org/javadoc/com/jme3/bullet/objects/PhysicsRigidBody.html#applyImpulse(com.jme3.math.Vector3f, com.jme3.math.Vector3f)

Here is the thing Yang,

i want to calculate a force something like that:

If i target Right Bottom Corner of the ball, i want it to move it towards Left Top and vice versa.

Any idead how to implement that?

Thanks so much for help.

Well, this was exactly what his mentioned method does. :wink:
http://hub.jmonkeyengine.org/javadoc/com/jme3/bullet/objects/PhysicsRigidBody.html#applyImpulse(com.jme3.math.Vector3f,%20com.jme3.math.Vector3f)
(impule = The “strength” of the applied force
rel_pos = The relative position where the force is applied (The position on your ball)
)

You can e.g. set the relative position to your lower right corner and apply some force -The ball should automatically move towards the upper left.
(Take care of gravity, the movement could result in a curve instead of a straight line the stronger the gravity is)

Hope, that helps you. :slight_smile:

Hello destroflyer,

I’m using this now:

Vector3f pt = results.getCollision(i).getContactPoint(); // Clicked Point
ballNode.applyImpulse(new Vector3f(-1f, 1f, -1f), pt); // Applied Force.

But no luck at all, applied force is always left upper corner, how can i calculate strenght vector for that?

I’m really new at jMonkey and really appriciate helps :slight_smile:
Thanks.

Well… May be you could use the normal vector at the impact point ?
If this is a sphere, you can just draw a vector from the impact point to the ball center. Then just have to modulate with the strength of the kick.

[java]
Vector3f pt = results.getCollision(i).getContactPoint();
Vector3f force = ballCenter.getWorldTranslation().subtract(pt).normalize().mult(kick_force);
[/java]

@yang71 said: Well... May be you could use the normal vector at the impact point ? If this is a sphere, you can just draw a vector from the impact point to the ball center. Then just have to modulate with the strength of the kick.

Yes it is a sphere, with a SphereCollisionShape how can i get its center? is there a method or something?

Then how to calculate that vector? Vector3f.add or Vector3f.substract ?

Sorry I edited my post to add this information…

@yang71 said: Sorry I edited my post to add this information...

Thanks so much!

Now i just need to adjust gravity :slight_smile: Thanks so much man! I Really appriciate it.

You’re welcome. You will soon give a hand too, You’ll see…

How to get the center of the ball in order to applyImpulse() on a sphere?

The center of mass is always at the center of the object (0/0/0 local)

Hum… Are you serious ?

You just use getWorldTranslation() !

Edit : But Normen is certainly right… Forces may be applied into the local coordinates. Never done it, so I would follow his advice!

I don’t get the ball pushed into the direction of the camera properly. The ball begins to rotate and starts rolling towards me (the camera position) with the following code:
[java]

Vector3f camDirection = app.getCamera().getDirection();
Vector3f normalizedDirection = new Vector3f(camDirection.getX(), 0, camDirection.getZ()).normalize();
Vector3f impulse = normalizedDirection.mult(power);
Vector3f impactVector = normalizedDirection.negate().mult(GlobalConstants.BALL_RADIUS);
Vector3f ballCenter = ball_phy.getPhysicsLocation();
Vector3f impactPoint = ballCenter.add(impactVector);
ball_phy.applyImpulse(impulse, impactPoint);

[/java]

BTW, the impulse keeps the ball pushing when hitting a wall. How can this be possible?

Well. I really don’t know. I already told you, I don’t use impulses. I simply set the linear velocity… Maybe your impulse is not strong enough ?

As for your second problem, how often do you apply your impulse ? Only once or at each update ?

1 Like

Again, impact_point is local in this case, you probably want it to be 0/0/0 simply. You jave to apply forces when a physics tick is occurring, see the ohysics documentation.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless theres emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

I simply set the linear velocity
setLinearVelocity() works great... Tha'ts exactly what I needed. Thank you yang.

I there more physics documentation than “F1” and the Javacoc?

@mathias4u said: I there more physics documentation than "F1" and the Javacoc?
Proper research started before 1800 even and since Einstein it only became more interesting.. What exactly do you want to know? ;) The javadoc tells you what it does and the docs tell you how you are supposed to use it. Whats the question? :)

Btw, I always think “Balls, Physics, I need help from the Force” when I read this topic title xD Nothing about you though :slight_smile:
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless theres emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.