Hello,
calling NewtonBodyEnableSimulation on an already enabled body cause a memory corruption.
a body member with the enable state could permit to add a test on enable / disable to prevent multiple enable / disable call.
Moderators: Sascha Willems, Thomas
void dgWorld::BodyEnableSimulation (dgBody* const body)
{
if (!body->m_masterNode) {
m_disableBodies.Remove(body);
dgBodyMasterList::AddBody(body);
body->SetMassMatrix(body->m_mass.m_w, body->m_mass.m_x, body->m_mass.m_y, body->m_mass.m_z);
m_broadPhase->Add (body);
dgAssert (body->m_masterNode);
}
}
I added the function
int NewtonBodyGetEnableDisableSimulationState(const NewtonBody* const body);
// Name: NewtonBodyGetSimulationState
// Gets the current simulation state of the specified body.
//
// Parameters:
// *const NewtonBody* *bodyPtr - pointer to the body to be inspected.
//
// Return: the current simulation state 0: disabled 1: active.
//
// See also: NewtonBodySetSimulationState
int NewtonBodyGetSimulationState(const NewtonBody* const bodyPtr)
{
TRACE_FUNCTION(__FUNCTION__);
dgBody* const body = (dgBody *)bodyPtr;
dgWorld* const world = body->GetWorld();
return world->GetBodyEnableDisableSimulationState(body) ? 1 : 0;
}
// Name: NewtonBodySetSimulationState
// Sets the current simulation state of the specified body.
//
// Parameters:
// *const NewtonBody* *bodyPtr - pointer to the body to be changed.
// *const int* state - the new similation state 0: disabled 1: active
//
// Return: Nothing.
//
// See also: NewtonBodyGetSimulationState
void NewtonBodySetSimulationState(const NewtonBody* const bodyPtr, const int state)
{
TRACE_FUNCTION(__FUNCTION__);
dgBody* const body = (dgBody *)bodyPtr;
dgWorld* const world = body->GetWorld();
if (state)
world->BodyEnableSimulation(body);
else
world->BodyDisableSimulation(body);
}
NEWTON_DEPRECATED_API void NewtonBodyEnableSimulation(const NewtonBody* const body);
NEWTON_DEPRECATED_API void NewtonBodyDisableSimulation(const NewtonBody* const body);
d.l.i.w wrote:Btw. it's bad coding style to leave out the brackets on if - else.
Now - what does this mean simulation state = true?
Of course one could add constants / enums for the state(s), but this means more typing...
This not something that can be boiled down to a good or bad.
well, where did you find that bool? it's an int
...and that follows the coding convetion of the rest of the api.
It's allows for more states that just 0/1 as other physics libraries do
If it's done correctly, I don't mind when brackets are missing, but unfortunately often enough you get to see big blocks of nested if - else and similar constructs with as few brackets as possible.
yeah, I can understand that. I am also a bit of a code jäger in our company, I always review code and if it does not match our style somebody gets blamed for it.d.l.i.w wrote:Some time ago I had to maintain and extend ("a bit") an existing code base with such bad quality (it was PHP actually). It took me almost a year to get any sense out of this big mess and the "small extensions" needed ended up in almost a whole rewrite of the code - just for you to know, why I reacted such "allergic"...
don't want to nitpick, but it's part of C99 <stdbool.h> and even if a project is not using C++ or C99 it's nearly always clearly typedef'd so the size is uniform in the project, it's either typedef uint8_t bool or typedef int32_t bool - but I'm sure you get the point: a bool type is clearly defined most of the time. And if the coder didn't know typedef back then, he probably did a #define bool intThat's not that clear with C...
Maybe I did too much header conversion lately, being too much frustrated of deciding if an int is actually a bool or not
Although I wouldn't orientate on other libraries too much, that's a valid point, too...
NEWTON_API int NewtonJointIsActive (const NewtonJoint* const joint);
NEWTON_API void NewtonJointGetInfo (const NewtonJoint* const joint, NewtonJointRecord* const info);
NEWTON_API int NewtonJointGetCollisionState (const NewtonJoint* const joint);
NEWTON_API void NewtonJointSetCollisionState (const NewtonJoint* const joint, int state);
NEWTON_API void NewtonUpVectorGetPin (const NewtonJoint* const upVector, dFloat *pin);
NEWTON_API void NewtonUpVectorSetPin (const NewtonJoint* const upVector, const dFloat *pin);
NEWTON_API int NewtonBodyGetSleepState (const NewtonBody* const body);
NEWTON_API void NewtonBodySetSleepState (const NewtonBody* const body, int state);
NEWTON_API int NewtonBodyGetAutoSleep (const NewtonBody* const body);
NEWTON_API void NewtonBodySetAutoSleep (const NewtonBody* const body, int state);
NEWTON_API int NewtonBodyGetFreezeState(const NewtonBody* const body);
NEWTON_API void NewtonBodySetFreezeState (const NewtonBody* const body, int state);
Users browsing this forum: No registered users and 15 guests