Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box2d fixes #25

Merged
merged 9 commits into from
Jan 9, 2012
7 changes: 4 additions & 3 deletions src/moaicore/MOAIBox2DBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int MOAIBox2DBody::_applyLinearImpulse ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DBody self
@in number torque Torque in degrees.
@opt number torque Converted to N-m. Default value is 0.
@out nil
*/
int MOAIBox2DBody::_applyTorque ( lua_State* L ) {
Expand All @@ -296,8 +296,9 @@ int MOAIBox2DBody::_applyTorque ( lua_State* L ) {
MOAILog ( state, MOAILogMessages::MOAIBox2DBody_MissingInstance );
return 0;
}

float torque = state.GetValue < float >( 2, 0.0f ) * ( float )D2R;
float unitsToMeters = self->GetUnitsToMeters();
/* Convert from N-m (kg m / s^2) * m => (kg unit / s^2) * unit */
float torque = state.GetValue < float >( 2, 0.0f ) * unitsToMeters * unitsToMeters;
self->mBody->ApplyTorque ( torque );

return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/moaicore/MOAIBox2DDistanceJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int MOAIBox2DDistanceJoint::_setDampingRatio ( lua_State* L ) {
return 0;
}

float dampingRatio = state.GetValue < float >( 1, 0.0f );
float dampingRatio = state.GetValue < float >( 2, 0.0f );

b2DistanceJoint* joint = ( b2DistanceJoint* )self->mJoint;
joint->SetDampingRatio ( dampingRatio );
Expand All @@ -120,7 +120,7 @@ int MOAIBox2DDistanceJoint::_setFrequency ( lua_State* L ) {
return 0;
}

float frequency = state.GetValue < float >( 1, 0.0f );
float frequency = state.GetValue < float >( 2, 0.0f );

b2DistanceJoint* joint = ( b2DistanceJoint* )self->mJoint;
joint->SetFrequency ( frequency );
Expand All @@ -145,7 +145,7 @@ int MOAIBox2DDistanceJoint::_setLength ( lua_State* L ) {
return 0;
}

float length = state.GetValue < float >( 1, 0.0f ) * unitsToMeters;
float length = state.GetValue < float >( 2, 0.0f ) * unitsToMeters;

b2DistanceJoint* joint = ( b2DistanceJoint* )self->mJoint;
joint->SetFrequency ( length );
Expand Down
14 changes: 9 additions & 5 deletions src/moaicore/MOAIBox2DFrictionJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int MOAIBox2DFrictionJoint::_getMaxForce ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DDistanceJoint self
@out number maxTorque
@out number maxTorque Converted from N-m.
*/
int MOAIBox2DFrictionJoint::_getMaxTorque ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIBox2DFrictionJoint, "U" )
Expand All @@ -54,7 +54,9 @@ int MOAIBox2DFrictionJoint::_getMaxTorque ( lua_State* L ) {
}

b2FrictionJoint* joint = ( b2FrictionJoint* )self->mJoint;
state.Push ( joint->GetMaxTorque () * ( float )R2D );
float unitsToMeters = self->GetUnitsToMeters();
/* Convert to/from N-m (kg m / s^2) * m from/to (kg unit / s^2) * unit */
state.Push ( joint->GetMaxTorque () / ( unitsToMeters * unitsToMeters ) );

return 1;
}
Expand All @@ -76,7 +78,7 @@ int MOAIBox2DFrictionJoint::_setMaxForce ( lua_State* L ) {
return 0;
}

float maxForce = state.GetValue < float >( 1, 0.0f ) * unitsToMeters;
float maxForce = state.GetValue < float >( 2, 0.0f ) * unitsToMeters;

b2FrictionJoint* joint = ( b2FrictionJoint* )self->mJoint;
joint->SetMaxForce ( maxForce );
Expand All @@ -89,7 +91,7 @@ int MOAIBox2DFrictionJoint::_setMaxForce ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DDistanceJoint self
@opt number maxTorque Default value is 0.
@opt number maxTorque Converted to N-m. Default value is 0.
@out nil
*/
int MOAIBox2DFrictionJoint::_setMaxTorque ( lua_State* L ) {
Expand All @@ -100,7 +102,9 @@ int MOAIBox2DFrictionJoint::_setMaxTorque ( lua_State* L ) {
return 0;
}

float maxTorque = state.GetValue < float >( 1, 0.0f ) * ( float )D2R;
float unitsToMeters = self->GetUnitsToMeters();
/* Convert to/from N-m (kg m / s^2) * m from/to (kg unit / s^2) * unit */
float maxTorque = state.GetValue < float >( 2, 0.0f ) * unitsToMeters * unitsToMeters;

b2FrictionJoint* joint = ( b2FrictionJoint* )self->mJoint;
joint->SetMaxTorque ( maxTorque );
Expand Down
2 changes: 1 addition & 1 deletion src/moaicore/MOAIBox2DGearJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int MOAIBox2DGearJoint::_setRatio ( lua_State* L ) {
return 0;
}

float ratio = state.GetValue < float >( 1, 0.0f );
float ratio = state.GetValue < float >( 2, 0.0f );

b2GearJoint* joint = ( b2GearJoint* )self->mJoint;
joint->SetRatio ( ratio );
Expand Down
6 changes: 4 additions & 2 deletions src/moaicore/MOAIBox2DJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ int MOAIBox2DJoint::_getReactionForce ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DJoint self
@out number reactionTorque In degrees.
@out number reactionTorque Converted from N-m.
*/
int MOAIBox2DJoint::_getReactionTorque ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIBox2DJoint, "U" )
Expand All @@ -165,8 +165,10 @@ int MOAIBox2DJoint::_getReactionTorque ( lua_State* L ) {

float step = ( float )( 1.0 / MOAISim::Get ().GetStep ());

/* Convert from N-m (kg m / s^2) * m to (kg unit / s^2) * unit */
float unitsToMeters = self->GetUnitsToMeters();
float torque = self->mJoint->GetReactionTorque ( step );
lua_pushnumber ( state, torque );
lua_pushnumber ( state, torque / ( unitsToMeters * unitsToMeters ) );

return 1;
}
Expand Down
38 changes: 35 additions & 3 deletions src/moaicore/MOAIBox2DPrismaticJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include <Box2D/Box2D.h>
#include <moaicore/MOAISim.h>
#include <moaicore/MOAIBox2DArbiter.h>
#include <moaicore/MOAIBox2DBody.h>
#include <moaicore/MOAIBox2DPrismaticJoint.h>
Expand Down Expand Up @@ -100,7 +101,8 @@ int MOAIBox2DPrismaticJoint::_getMotorForce ( lua_State* L ) {

b2PrismaticJoint* joint = ( b2PrismaticJoint* )self->mJoint;

state.Push ( joint->GetMotorForce (1.0f) / unitsToMeters );
float step = ( float )( 1.0 / MOAISim::Get ().GetStep ());
state.Push ( joint->GetMotorForce (step) / unitsToMeters );

return 1;
}
Expand Down Expand Up @@ -261,7 +263,7 @@ int MOAIBox2DPrismaticJoint::_setMaxMotorForce ( lua_State* L ) {
return 0;
}

float maxMotorForce = state.GetValue < float >( 1, 0.0f );
float maxMotorForce = state.GetValue < float >( 2, 0.0f );

b2PrismaticJoint* joint = ( b2PrismaticJoint* )self->mJoint;
joint->SetMaxMotorForce ( maxMotorForce * unitsToMeters );
Expand All @@ -272,10 +274,12 @@ int MOAIBox2DPrismaticJoint::_setMaxMotorForce ( lua_State* L ) {
//----------------------------------------------------------------//
/** @name setMotor
@text See Box2D documentation.
If speed is determined to be zero, the motor is disabled, unless forceEnable is set.

@in MOAIBox2DPrismaticJoint self
@opt number speed Default value is 0.
@opt number max Default value is 0.
@opt boolean forceEnable Default value is false.
@out nil
*/
int MOAIBox2DPrismaticJoint::_setMotor ( lua_State* L ) {
Expand All @@ -288,13 +292,14 @@ int MOAIBox2DPrismaticJoint::_setMotor ( lua_State* L ) {

float speed = state.GetValue < float >( 2, 0.0f );
float max = state.GetValue < float >( 3, 0.0f );
bool forceEnable = state.GetValue < bool >( 4, false );

float unitsToMeters = self->GetUnitsToMeters ();
b2PrismaticJoint* joint = ( b2PrismaticJoint* )self->mJoint;

joint->SetMotorSpeed ( speed * unitsToMeters );
joint->SetMaxMotorForce ( max * unitsToMeters );
joint->EnableMotor ( speed != 0.0f );
joint->EnableMotor ( forceEnable ? true : ( speed != 0.0f ) );

return 0;
}
Expand Down Expand Up @@ -323,6 +328,32 @@ int MOAIBox2DPrismaticJoint::_setMotorEnabled ( lua_State* L ) {
return 0;
}

//----------------------------------------------------------------//
/** @name setMotorSpeed
@text See Box2D documentation.

@in MOAIBox2DPrismaticJoint self
@opt number motorSpeed Converted from units of distance/s to m/s. Default value is 0.

*/
int MOAIBox2DPrismaticJoint::_setMotorSpeed ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIBox2DPrismaticJoint, "U" )

if ( !self->mJoint ) {
MOAILog ( state, MOAILogMessages::MOAIBox2DJoint_MissingInstance );
return 0;
}

float unitsToMeters = self->GetUnitsToMeters ();
float speed = state.GetValue < float >( 2, 0.0f ) * unitsToMeters;

b2PrismaticJoint* joint = ( b2PrismaticJoint* )self->mJoint;
joint->SetMotorSpeed ( speed );

return 0;
}


//================================================================//
// MOAIBox2DPrismaticJoint
//================================================================//
Expand Down Expand Up @@ -361,6 +392,7 @@ void MOAIBox2DPrismaticJoint::RegisterLuaFuncs ( MOAILuaState& state ) {
{ "setLimitEnabled", _setLimitEnabled },
{ "setMaxMotorForce", _setMaxMotorForce },
{ "setMotor", _setMotor },
{ "setMotorSpeed", _setMotorSpeed },
{ "setMotorEnabled", _setMotorEnabled },
{ NULL, NULL }
};
Expand Down
1 change: 1 addition & 0 deletions src/moaicore/MOAIBox2DPrismaticJoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MOAIBox2DPrismaticJoint :
static int _setLimitEnabled ( lua_State* L );
static int _setMaxMotorForce ( lua_State* L );
static int _setMotor ( lua_State* L );
static int _setMotorSpeed ( lua_State* L );
static int _setMotorEnabled ( lua_State* L );

public:
Expand Down
52 changes: 43 additions & 9 deletions src/moaicore/MOAIBox2DRevoluteJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int MOAIBox2DRevoluteJoint::_getMotorSpeed ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DRevoluteJoint self
@out number motorTorque
@out number motorTorque Converted from N-m.
*/
int MOAIBox2DRevoluteJoint::_getMotorTorque ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIBox2DRevoluteJoint, "U" )
Expand All @@ -116,9 +116,11 @@ int MOAIBox2DRevoluteJoint::_getMotorTorque ( lua_State* L ) {
return 0;
}

float unitsToMeters = self->GetUnitsToMeters ();
b2RevoluteJoint* joint = ( b2RevoluteJoint* )self->mJoint;
float step = ( float )( 1.0 / MOAISim::Get ().GetStep ());
state.Push ( joint->GetMotorTorque (step) * ( float )R2D );
/* Convert from N-m (kg m / s^2) * m => (kg unit / s^2) * unit */
state.Push ( joint->GetMotorTorque (step) / (unitsToMeters * unitsToMeters));

return 1;
}
Expand Down Expand Up @@ -242,7 +244,7 @@ int MOAIBox2DRevoluteJoint::_setLimitEnabled ( lua_State* L ) {
@text See Box2D documentation.

@in MOAIBox2DRevoluteJoint self
@opt number maxMotorTorque Default value is 0.
@opt number maxMotorTorque Converted to N-m. Default value is 0.
@out nil
*/
int MOAIBox2DRevoluteJoint::_setMaxMotorTorque ( lua_State* L ) {
Expand All @@ -253,7 +255,9 @@ int MOAIBox2DRevoluteJoint::_setMaxMotorTorque ( lua_State* L ) {
return 0;
}

float maxMotorTorque = state.GetValue < float >( 1, 0.0f ) * ( float )D2R;
float unitsToMeters = self->GetUnitsToMeters ();
/* Convert to N-m (kg m / s^2) * m from (kg unit / s^2) * unit */
float maxMotorTorque = state.GetValue < float >( 2, 0.0f ) * unitsToMeters * unitsToMeters;

b2RevoluteJoint* joint = ( b2RevoluteJoint* )self->mJoint;
joint->SetMaxMotorTorque ( maxMotorTorque );
Expand All @@ -264,10 +268,12 @@ int MOAIBox2DRevoluteJoint::_setMaxMotorTorque ( lua_State* L ) {
//----------------------------------------------------------------//
/** @name setMotor
@text See Box2D documentation.

If speed is determined to be zero, the motor is disabled, unless forceEnable is set.

@in MOAIBox2DRevoluteJoint self
@opt number speed Default value is 0.
@opt number max Default value is 0.
@opt number maxMotorTorque Converted to N-m. Default value is 0.
@opt boolean forceEnable Default value is false.
@out nil
*/
int MOAIBox2DRevoluteJoint::_setMotor ( lua_State* L ) {
Expand All @@ -278,17 +284,44 @@ int MOAIBox2DRevoluteJoint::_setMotor ( lua_State* L ) {
return 0;
}

float unitsToMeters = self->GetUnitsToMeters ();
float speed = state.GetValue < float >( 2, 0.0f );
float max = state.GetValue < float >( 3, 0.0f );

bool forceEnable = state.GetValue < bool >( 4, false );

b2RevoluteJoint* joint = ( b2RevoluteJoint* )self->mJoint;
joint->SetMotorSpeed ( speed * ( float )D2R );
joint->SetMaxMotorTorque ( max * ( float )D2R );
joint->EnableMotor ( speed != 0.0f );
/* Convert from N-m (kg m / s^2) * m => (kg unit / s^2) * unit */
joint->SetMaxMotorTorque ( max * unitsToMeters * unitsToMeters );
joint->EnableMotor ( forceEnable ? true : ( speed != 0.0f ) );

return 0;
}

//----------------------------------------------------------------//
/** @name setMotorSpeed
@text See Box2D documentation.

@in MOAIBox2DRevoluteJoint self
@opt number motorSpeed Converted from Deg/s to Rad/s. Default value is 0.

*/
int MOAIBox2DRevoluteJoint::_setMotorSpeed ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIBox2DRevoluteJoint, "U" )

if ( !self->mJoint ) {
MOAILog ( state, MOAILogMessages::MOAIBox2DJoint_MissingInstance );
return 0;
}

float speed = state.GetValue < float >( 2, 0.0f ) * ( float )D2R;

b2RevoluteJoint* joint = ( b2RevoluteJoint* )self->mJoint;
joint->SetMotorSpeed ( speed );

return 0;
}

//----------------------------------------------------------------//
/** @name setMotorEnabled
@text See Box2D documentation.
Expand Down Expand Up @@ -351,6 +384,7 @@ void MOAIBox2DRevoluteJoint::RegisterLuaFuncs ( MOAILuaState& state ) {
{ "setLimitEnabled", _setLimitEnabled },
{ "setMaxMotorTorque", _setMaxMotorTorque },
{ "setMotor", _setMotor },
{ "setMotorSpeed", _setMotorSpeed },
{ "setMotorEnabled", _setMotorEnabled },
{ NULL, NULL }
};
Expand Down
1 change: 1 addition & 0 deletions src/moaicore/MOAIBox2DRevoluteJoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MOAIBox2DRevoluteJoint :
static int _setLimitEnabled ( lua_State* L );
static int _setMaxMotorTorque ( lua_State* L );
static int _setMotor ( lua_State* L );
static int _setMotorSpeed ( lua_State* L );
static int _setMotorEnabled ( lua_State* L );

public:
Expand Down
3 changes: 1 addition & 2 deletions src/moaicore/MOAIBox2DRopeJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ int MOAIBox2DRopeJoint::_setMaxLength ( lua_State* L ) {
return 0;
}

float length = state.GetValue < float >( 1, 0.0f ) * unitsToMeters;
printf("\n\n\nmax length = %f", length);
float length = state.GetValue < float >( 2, 0.0f ) * unitsToMeters;
b2RopeJoint* joint = ( b2RopeJoint* )self->mJoint;
joint->SetMaxLength ( length );

Expand Down
Loading