x


Ball hits bat or any rigidbody...turn off force...

Hello, I help special needs children focus...

I am trying to prove the concept of a pitcher / batter experience to help some of the wheelchair children "experience" athletics. I've scoured the answers and forums, and can get some basic physics going well on the effort. The problem is that I haven't yet discovered how to turn off the initial velocity of the pitchspeed...

var pitchspeed : float;
var collidedwith : boolean;


pitchspeed = -45;


function Update() {
    if(collidedwith != true)
    rigidbody.AddForce (0, 0, pitchspeed);
    }

function OnCollisionEnter(collision : Collision) {
    collidedwith = true;
    rigidbody.velocity = -rigidbody.velocity;

    }

Sincere thanks

more ▼

asked Aug 25 '10 at 08:09 PM

Jim 3 gravatar image

Jim 3
1 1 1 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First, consider what your code does.

//Every Frame - Physics are meant to be done in FixedUpdate() - See the docs
function Update() {
    //when you haven't collided with anything, add more force?
    //what's pushing the ball after it gets thrown?
    if(collidedwith != true)
        rigidbody.AddForce (0, 0, pitchspeed);
}

//When there's a collision
function OnCollisionEnter(collision : Collision) {
    collidedwith = true;
    //Set it to always move at the same speed in the opposite direction
    //I don't think that a bat hitting a ball will hit it back with the same speed.
    rigidbody.velocity = -rigidbody.velocity;
}

You probably want the ball to slow down after being thrown. You probably also want your bat to hit the ball with a certain speed, but to have the ball slow down in the air. Setting the velocity makes it always have that velocity unless you apply something to change that. Forces are also constant. What you likely want is drag - drag will slow down rigidbody motion.

What it seems like you want is:

  1. When you throw the ball (in Start() is fine for your tests), add an appropriate force or set the velocity to represent the pitch speed. This could be calculated based on some throwing animation or whatever. Note that to add spin, you would add torque (likely with angular drag) and you might consider adding a slight relative force to get the ball to actually spin through the air.
  2. When the bat hits the ball, add an appropriate force or set the velocity appropriately relative to the mass, speed and direction of the bat. Using AddForceAtPosition with the appropriately calculated force can provide a very realistic approximation. Also bear in mind that if the bat is moving fast enough or the ball slow enough, it will carry the ball for a time, changing the equation some. -Time to pull out those junior high/high school physics notes or start googling (try wikipedia).

As an alternative, if you really want to use the physics, then why not use them for all of it? Have both the ball and the bat have rigidbodies with appropriate mass and drag settings, etc. If you apply the appropriate forces, velocities, torque, etc. you should get a realistic approximation of how physics would treat these objects. Bear in mind that the bat's motion may be a bit deterministic with this approach depending on how you do it.

more ▼

answered Aug 25 '10 at 10:47 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Thank you very much for the in depth plain english! I will keep going on this and let you know how it unfolds. Sincere Gratitude for the effort!!!

Aug 25 '10 at 10:57 PM jim Langford
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2485
x227
x6

asked: Aug 25 '10 at 08:09 PM

Seen: 1730 times

Last Updated: Aug 25 '10 at 09:42 PM