views:

89

answers:

1

Hi,

This is a pure physic question but i don't know why it doesn't work....i have a moving object.i get the value of vcos(theta) and vsin(theta)...from this i calculate the velocity and angle of motion.....also i know another point (x,y) and want to direct the object to this point.I think i need to apply a certain force(force must have X and Y axis value)to direct the object towards the point....so to get the amount of force required i just follow the formula:

fX=V2cos(theta2)-V1cos(theta1) fY=V2sin(theta2)-V1sin(theta1)

no matter what ever the syntex are given bellow(i give it for those people know objective c).........my equation doesn't work.....can anyone help......

    if (acceleration.x>1.5 || acceleration.y>1.5) {

            shakeCounter++;
            [_label setString:[NSString stringWithFormat:@"%d",shakeCounter]];  


        //get the velocity of moving object.......................  
        b2Vec2 mVelik = ballBody->GetLinearVelocityFromLocalPoint(localPoint);


        float angleOfCurrentDirectionOfMotion;
        float angleOfDesiredDirectionOfMotion;
         //calculate first velocity
        float V1=sqrt(pow(mVelik.x, 2)+pow(mVelik.y, 2));
        //calculate second velocity
        float V2=V1+factor;
        //calculate current  angle
        angleOfCurrentDirectionOfMotion=atan(mVelik.y/mVelik.x);
         //calculate desired angle
        angleOfDesiredDirectionOfMotion=atan(acceleration.y/acceleration.x);
///calculate FX and FY
        float X=V2*cos(angleOfDesiredDirectionOfMotion)-V1*cos(angleOfCurrentDirectionOfMotion);
        float Y=V2*sin(angleOfDesiredDirectionOfMotion)-V1*sin(angleOfCurrentDirectionOfMotion);

        b2Vec2 force = b2Vec2(X,Y);

 ///apply Force to change direction....   
        ballBody->ApplyForce(force, ballBody->GetPosition());

}
+1  A: 

I do not have box2d to experiment with, but I will assume it works as it should.

You cannot disregard the mass of the object; the greater the mass, the less the effect of a force.

Your calculation of X and Y seems correct (although overcomplicated). You can change the motion with an impulse applied to the center of mass::

b2Vec2 impulse = b2Vec2(X,Y) * ballBody->GetMass() ;
ballBody->ApplyLinearImpulse(impulse, ballBody->GetLocalCenter());

If you really want to use a force and not an impulse, there is a range of solutions. Generally you can choose the magnitude of the force first, then calculate the direction, or the other way around. I can give you the equations (and maybe code) but it will not make any sense without an understanding of basic physics.

EDIT:
All right, the equation of one-dimensional motion is x = x0 + V0t + at2/2, so WLOG assume ax = 1 in the direction of the target point, and solve for time (the time when the x of the ball will equal the x of the target point). Then put that time into the equation for y and solve for ay, and you're done.

Beta
i have basic understanding of physic:)....but as i studied many days ago can't remember everything.....u can provide equation...plz
Rony