Frankey
Frankey

Reputation: 336

Arrow Box2d Physic in Libgdx

I am following this tutorial to create arrow physic in Libgdx. But my arrow works so weirdly that i suspect my calculation of dragforce is the one creating the problem.... I am not familiar with C++ programming and im really have no idea where am i doing wrong.

Can anyone help check where is my mistake?

Codes of tutorial(C++):

  b2Vec2 pointingDirection = arrowBody->GetWorldVector( b2Vec2( 1, 0 ) );
  b2Vec2 flightDirection = arrowBody->GetLinearVelocity();
  float flightSpeed = flightDirection.Normalize();//normalizes and returns length

  float dot = b2Dot( flightDirection, pointingDirection );
  float dragForceMagnitude = (1 - fabs(dot)) * flightSpeed * flightSpeed * dragConstant * arrowBody->GetMass();

  b2Vec2 arrowTailPosition = arrowBody->GetWorldPoint( b2Vec2( -1.4, 0 ) );
  arrowBody->ApplyForce( dragForceMagnitude * -flightDirection, arrowTailPosition );

Codes of mine(java):

pointingDirection = body.getWorldVector(arrowPoitningDirection);
flightDirection = body.getLinearVelocity();
float flightSpeed = flightDirection.cpy().nor().len();//normalizes and returns length

float dot = Vector2.dot(flightDirection.x,flightDirection.y,pointingDirection.x,pointingDirection.y);
float dragCons = 0.5f;
float dragForceMagnitude = (1 - Math.abs(dot)) * flightSpeed * flightSpeed * dragCons * body.getMass();

arrowTailPos = body.getWorldPoint(arrowTailPointingPos);
body.applyForce( flightDirection.scl(dragForceMagnitude) , arrowTailPos, false);

Thank you.

Upvotes: 0

Views: 160

Answers (1)

Hryb Mikita
Hryb Mikita

Reputation: 56

flightDirection.Normalize() at first returns length of the vector and then normilizes it. In your java code flightDirection.cpy().nor().len() you normalize an then return length. After normilize vector it always has length == 1 (Please read about operations with vectors).

Just change this

flightDirection.cpy().nor().len()

to this

float flightSpeed = flightDirection.len()

flightDirection.nor()

Upvotes: 4

Related Questions