What does b2Cross do?

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

What does b2Cross do?

Post by horacebury »

Old versions of Box2D appear to have a library function called B2Cross. I've seen it used in this tutorial in the ComputeCentroid function:

http://www.iforce2d.net/b2dtut/buoyancy

I cannot find it in the current Box2D documentation. Does anyone know what it does?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: What does b2Cross do?

Post by iforce2d »

It has a few different purposes depending on what parameters you give it. You can find them in b2Math.h:

Code: Select all

/// Perform the cross product on two vectors. In 2D this produces a scalar.
inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
{
        return a.x * b.y - a.y * b.x;
}


/// Perform the cross product on a vector and a scalar. In 2D this produces
/// a vector.
inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
{
        return b2Vec2(s * a.y, -s * a.x);
}


/// Perform the cross product on a scalar and a vector. In 2D this produces
/// a vector.
inline b2Vec2 b2Cross(float32 s, const b2Vec2& a)
{
        return b2Vec2(-s * a.y, s * a.x);
}
Actually the last two are the same I guess...

The first one (vec/vec) is the normal cross product, and the second one seems to be mainly used in Box2D to get a vector perpendicular to the one you pass in, by making the float parameter 1 or -1.
horacebury
Posts: 4
Joined: Wed Feb 27, 2013 9:57 am

Re: What does b2Cross do?

Post by horacebury »

Thank you!
Post Reply