Normalize

General discussion about Box2D tutorials
Post Reply
horacebury
Posts: 4
Joined: Wed Feb 27, 2013 9:57 am

Normalize

Post by horacebury »

In the Buoyancy tutorial, under the section "Applying simple drag" (http://www.iforce2d.net/b2dtut/buoyancy) there is a code snippet:

Code: Select all

  //find relative velocity between object and fluid
  b2Vec2 velDir = fixtureB->GetBody()->GetLinearVelocityFromWorldPoint( centroid ) -
                  fixtureA->GetBody()->GetLinearVelocityFromWorldPoint( centroid );
  float vel = velDir.Normalize();
      
  //apply simple linear drag
  float dragMag = fixtureA->GetDensity() * vel * vel;
  b2Vec2 dragForce = dragMag * -velDir;
  fixtureB->GetBody()->ApplyForce( dragForce, centroid );
While I understand that calculating the Normalized value of the 'velDir' essentially divides the X and Y values of the linear velocity values by the length of the vector and updates the 'velDir' vector object, but I am unsure of what the value of 'vel' will actually be.

If the magnitude of 'velDir' is, say, 10.0, what would the value of 'vel' be? Would it be 1.0 after Normalization?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Normalize

Post by iforce2d »

Normalization gives you a vector of length 1, without changing the direction, eg.
(0, 10) -> (0, 1)
(0, 10000) -> (0, 1)
(0.1245, 0) -> (1, 0)
(1, 1) -> (0.707, 0.707)

It's useful when you want to make a vector in a certain direction, of a certain length, because you can just multiply it by the length:

b2Vec2 vectorOfDesiredLengthInDesiredDirection = desiredLength * normalizedDirectionVector;
tescott
Posts: 68
Joined: Wed Feb 06, 2013 6:32 pm

Re: Normalize

Post by tescott »

Here's the code from b2Math.h for the b2Vec2 struct:

Code: Select all

	/// Convert this vector into a unit vector. Returns the length.
	float32 Normalize()
	{
		float32 length = Length();
		if (length < b2_epsilon)
		{
			return 0.0f;
		}
		float32 invLength = 1.0f / length;
		x *= invLength;
		y *= invLength;

		return length;
	}
.Normalize() is doing a couple of things for you. First, it is taking the vector and normalizing it (as you mention). Second, it's returning the length of the vector before normalization. This operation should return a scalar value that is proportional to the length of velDir to 'vel' before it is normalized. Note that if it returned the length after normalization, consider that a unit vector always has a length of '1' so obtaining that length wouldn't have much benefit.

If you need to refresh on unit vectors, this reference might help:

http://en.wikipedia.org/wiki/Unit_vector

So, in answer to your question, if the magnitude of velDir is 10, then 'vel' will equal 10.

--tim
horacebury
Posts: 4
Joined: Wed Feb 27, 2013 9:57 am

Re: Normalize

Post by horacebury »

Hey, thank you. That's very helpful. One last question:

What is the value of "b2_epsilon" - I've looked it up and assume that the logic in the code will work in other languages (I'm using Lua with Corona SDK) if I supplement 0.00002

Does that sound reasonable?
tescott
Posts: 68
Joined: Wed Feb 06, 2013 6:32 pm

Re: Normalize

Post by tescott »

From a math perspective, epsilon simply represents "an infinitesimally small amount".

Here's how it is defined in Box2D:

#define b2_epsilon FLT_EPSILON

FLT_EPSILON translates to 1e-5 according to Google.

I think your solution is reasonable.

--tim
Post Reply