Issue with NewtonCreateBox/Sphere

Report any bugs here and we'll post fixes

Moderators: Sascha Willems, Thomas

Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sat Nov 08, 2014 2:24 pm

Hello everyone.

Here is my problem:
- I try to create a NewtonBody when I press A (for example) from a Key() event in GLFW and it crashes.
- When I press A (for example), I set a bool on true (in function Key() ), and in my update() loop, I create a NewtonBody (if the bool is true): it doesn't crash.

Here is how I create my body:
Code: Select all
    inline NewtonBody * DynamicBox(ml::mat4 m, float mass, float *d, Object *o){
        NewtonCollision *c = NewtonCreateBox(world, d[0], d[1], d[2], 0, NULL); //<-- crashes here
        NewtonBody *b = NewtonCreateDynamicBody(world, c, &m[0].x);

        NewtonBodySetMassProperties(b, mass, c);
        NewtonDestroyCollision(c);

        InitBody(b, o);
        return b;
    }
    inline void InitBody(NewtonBody *b, Object *o){
        ent->b = b;

        NewtonBodySetUserData(b,ent);
        NewtonBodySetForceAndTorqueCallback(b, ApplyForceAndTorque);
        //NewtonBodySetTransformCallback(b, ApplyTransform);
        NewtonBodySetDestructorCallback(b, DestroyBody);
        NewtonBodySetAutoSleep(b, 1);
        NewtonBodySetLinearDamping(b, 0.f);
    }


and it crashes at NewtonCreateBox(). I had this issue some time ago and I "resolved" it with the bool trick.

I tried copying the exact same code in the 2 functions; it doesn't work in Key() but it works in update().

PS: I can't update to the latest newton game dynamics lib (I have the version of 22/7 - something like this) because my internet connection is unstable (disconnects all the time). Hopefully, it will work once again this week...
I don't think I will be able to go to the forums today.
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby AntonSynytsia » Sat Nov 08, 2014 3:37 pm

Is the pointer to the newton world defined and valid?
I will test NewtonCreateBox function with my wrapper and respond on results.
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm

Re: Issue with NewtonCreateBox/Sphere

Postby AntonSynytsia » Sat Nov 08, 2014 4:27 pm

NewtonCreateBox works well on mine with the latest revision (2153).
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sat Nov 08, 2014 4:50 pm

Well it works in one case when I call NewtonCreateBox or NewtonCreateSphere in my update() loop which I call from main()
here is how I do it:
Code: Select all
int main(){
//init
    while (!glfwWindowShouldClose(window)){
        //DRAW
        draw();
        glfwSwapBuffers(window);
        //UPDATE
        update(); // <-- when NewtonCreateBox called from here, it works
        glfwPollEvents();
    }
}


version where it crashes:
Code: Select all
void Key(GLFWwindow* window, int key, int scancode, int action, int mods){
    if(action != GLFW_RELEASE){
        if (key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, GL_TRUE);
        else if(key == GLFW_KEY_A){
            NewtonWaitForUpdateToFinish(pl::world);
            double x, y;
            glfwGetCursorPos(window, &x, &y);
            Object *obj = Add::Soldier(ml::vec2(x, y));
            Entity *ent = Global::entity("soldier");

            ml::vec3 d = ml::vec3(ent->props->size, 1.f);

            pl::DynamicBox(ml::translate(ml::mat4(1.f), ml::vec3(x, y, 0.f)), 1.f, &d.x, obj); // <-- crashes here
        }
    }
}

doesn't crash:
Code: Select all
bool pressed = false;
void Key(GLFWwindow* window, int key, int scancode, int action, int mods){
    if(action != GLFW_RELEASE){
        if (key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, GL_TRUE);
        else if(key == GLFW_KEY_A){
           pressed = true;
        }
    }
}

void update(){
//update...
    if(pressed){
            NewtonWaitForUpdateToFinish(pl::world);
            double x, y;
            glfwGetCursorPos(window, &x, &y);
            Object *obj = Add::Soldier(ml::vec2(x, y));
            Entity *ent = Global::entity("soldier");

            ml::vec3 d = ml::vec3(ent->props->size, 1.f);

            pl::DynamicBox(ml::translate(ml::mat4(1.f), ml::vec3(x, y, 0.f)), 1.f, &d.x, obj); // <-- doesn't crash
pressed = false;
    }
}
       
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby Julio Jerez » Sat Nov 08, 2014 4:59 pm

I you run NewtonAsync you can not crate object during a simulation update, you must call
NewtonWaitForUpdateToFinish first.

why are you creation object in in the middle of and update anyway?
Julio Jerez
Moderator
Moderator
 
Posts: 11040
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sun Nov 09, 2014 5:22 am

Hello
In the code I pasted, I put NewtonWaitForUpdateToFinish but it still crashes.
I put my creations in the update loop (it works there) because polling events and updating are linked. I tried putting them in the draw function and it works but it doesn't work in the Key function (or MousePress, ...).

Edit: If you don't understand what I am doing, ask me. I'll try to clarify the problem

When I debugged, the problem was in dgVector.cpp:
Code: Select all
   DG_INLINE dgVector (const dgVector& copy)
      :m_type(copy.m_type) // <-- here
   {
   }

coming from dgMatrix line 40
Code: Select all
class dgMatrix //strange but it comes from here
{

coming from Newton.cpp:
Code: Select all
NewtonCollision* NewtonCreateBox(const NewtonWorld* const newtonWorld, dFloat dx, dFloat dy, dFloat dz, int shapeID, const dFloat* const offsetMatrix)
{
   TRACE_FUNCTION(__FUNCTION__);
   Newton* const world = (Newton *)newtonWorld;
   dgMatrix matrix (dgGetIdentityMatrix()); // EDIT: crashes here
   if (offsetMatrix) {
       matrix = dgMatrix (offsetMatrix);
   }
   return (NewtonCollision*) world->CreateBox (dx, dy, dz, shapeID, matrix);
}

and here is dgGetIdentityMatrix()
Code: Select all
const dgMatrix& dgGetIdentityMatrix() //was not in the debug
{
   return dgMatrix::m_identityMatrix;
}

I think it comes from SSE but I am not sure.
Last edited by arkdemon on Sun Nov 09, 2014 5:56 am, edited 2 times in total.
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sun Nov 09, 2014 5:46 am

Ok, I changed a bit NewtonCreateBox like this:
Code: Select all
NewtonCollision* NewtonCreateBox(const NewtonWorld* const newtonWorld, dFloat dx, dFloat dy, dFloat dz, int shapeID, const dFloat* const offsetMatrix)
{
   TRACE_FUNCTION(__FUNCTION__);
   Newton* const world = (Newton *)newtonWorld;
    dgMatrix matrix = (offsetMatrix) ? dgMatrix (offsetMatrix) : dgGetIdentityMatrix();
   return (NewtonCollision*) world->CreateBox (dx, dy, dz, shapeID, matrix);
}


and pl::DynamicBox (now I send an identity matrix in offsetMatrix)
Code: Select all
    inline NewtonBody * DynamicBox(ml::mat4 m, float mass, float *d, Object *o){
        ml::mat4 _m = ml::mat4(1.f);
        NewtonCollision *c = NewtonCreateBox(world, d[0], d[1], d[2], 0, &_m[0].x);
        NewtonBody *b = NewtonCreateDynamicBody(world, c, &m[0].x);

        NewtonBodySetMassProperties(b, mass, c);
        NewtonDestroyCollision(c);
        InitBody(b, o);

        return b;
    }


and I get an error from _mm_set_ps line 920 in xmmintrin.h
The error comes from dgVector line 672
Code: Select all
   DG_INLINE dgVector (dgFloat32 x, dgFloat32 y, dgFloat32 z, dgFloat32 w)
      :m_type(_mm_set_ps(w, z, y, x)) // <-- >>>HERE<<<
   {
   }

coming from dgCollisionBox.cpp l.96:
Code: Select all
void dgCollisionBox::Init (dgFloat32 size_x, dgFloat32 size_y, dgFloat32 size_z)
{
/*some code*/
m_vertex[0]   = dgVector ( m_size[0].m_x,  m_size[0].m_y,  m_size[0].m_z, dgFloat32 (0.0f)); // <-- >>>HERE<<<
   m_vertex[1]   = dgVector (-m_size[0].m_x,  m_size[0].m_y,  m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[2]   = dgVector ( m_size[0].m_x, -m_size[0].m_y,  m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[3]   = dgVector (-m_size[0].m_x, -m_size[0].m_y,  m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[4]   = dgVector (-m_size[0].m_x, -m_size[0].m_y, -m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[5]   = dgVector ( m_size[0].m_x, -m_size[0].m_y, -m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[6]   = dgVector (-m_size[0].m_x,  m_size[0].m_y, -m_size[0].m_z, dgFloat32 (0.0f));
   m_vertex[7]   = dgVector ( m_size[0].m_x,  m_size[0].m_y, -m_size[0].m_z, dgFloat32 (0.0f));

   dgCollisionConvex::m_vertex = m_vertex;
   dgCollisionConvex::m_simplex = m_edgeArray;

   SetVolumeAndCG ();
}


coming from the same file at line 69
Code: Select all
dgCollisionBox::dgCollisionBox(dgMemoryAllocator* allocator, dgUnsigned32 signature, dgFloat32 size_x, dgFloat32 size_y, dgFloat32 size_z)
   :dgCollisionConvex(allocator, signature, m_boxCollision)
{
   Init (size_x, size_y, size_z); // <-- >>>HERE<<<
}

coming from dgNarrowPhaseCollision.cpp line 97
Code: Select all
dgCollisionInstance* dgWorld::CreateBox(dgFloat32 dx, dgFloat32 dy, dgFloat32 dz, dgInt32 shapeID, const dgMatrix& offsetMatrix)
{
/*some code*/
   if (!node) {
       dgCollision* const collision = new  (m_allocator) dgCollisionBox (m_allocator, crc, dx, dy, dz); // <-- >>>HERE<<<
      node = dgBodyCollisionList::Insert (CollisionKeyPair(collision, pinNumber), crc);
   }
   return CreateInstance (node->GetInfo().m_collision, shapeID, offsetMatrix);
}

coming from Newton.cpp l.2272:
Code: Select all
NewtonCollision* NewtonCreateBox(const NewtonWorld* const newtonWorld, dFloat dx, dFloat dy, dFloat dz, int shapeID, const dFloat* const offsetMatrix)
{
   TRACE_FUNCTION(__FUNCTION__);
   Newton* const world = (Newton *)newtonWorld;
    dgMatrix matrix = (offsetMatrix) ? dgMatrix (offsetMatrix) : dgGetIdentityMatrix();
   return (NewtonCollision*) world->CreateBox (dx, dy, dz, shapeID, matrix); // <-- >>>HERE<<<
}
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby JoeJ » Sun Nov 09, 2014 8:32 am

Calling NewtonWaitForUpdateToFinish on key press is not good anyway, bacause it stalls the thread processing user input.
EDIT: And more important, another thread could start newton again after the function returned, but before you create the box.

It should loke somehow like this:

Code: Select all
int keyFlags;

void OnKeyPress () // or similar alled from GLFW - may be called multiple times per frame
{
 keyFlags = 1;
}

void GameLoop // you call once per frame
{
 NewtonWaitForUpdateToFinish();
 if (keyFlags) { CreateBox(); keyFlags = 0;}
 NewtonUpdate();
 RenderStuff();
}


Do you have it this way and it still crashes?

The other thing is, your posted code snippets from newton are confusing.
It may be better to post a single screenshot, where callstack and variables are visible.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Issue with NewtonCreateBox/Sphere

Postby Julio Jerez » Sun Nov 09, 2014 8:56 am

It is no any of the Simd you have a race condition with thread runing at the same time
try not calling NewtonUpdateAsync, call NetwonUpdate instead.
Julio Jerez
Moderator
Moderator
 
Posts: 11040
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sun Nov 09, 2014 9:35 am

@JoeJ
The version that you posted works fine(I posted a similar version :p) but it gets quickly annoying the more keys I have to manage. This is why I am trying to call a function directly from the Key event or OnKeyPress.

And sorry I don't think I can post any images right now because my internet connection is unstable. I will try when it will get stable again

@Julio
I tried the two actually(NewtonUpdate and NewtonUpdateAsync) and they give the same errors. Sometimes though, the debugger records a different error (in dgWorld::CreateBox() function when calling CreateInstance() )



Edit: I think it might be a problem with glfw. I just dug up a project when I used Newton with SFML and it works fine. I can create the body from OnKey() in that project
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby Julio Jerez » Sun Nov 09, 2014 10:01 am

are you sync to the latest version in GitHub?
Julio Jerez
Moderator
Moderator
 
Posts: 11040
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Issue with NewtonCreateBox/Sphere

Postby JoeJ » Sun Nov 09, 2014 10:02 am

arkdemon wrote:I am trying to call a function directly from the Key event or OnKeyPress


... which is a bad idea for many reasons:

* user input runs always in a different thread, you will get undefined behaviour and crashes no matter what update function you use. Even if you get the box creation to work, the next action may give problems as well.

* Bad code design: You merge physics and user input, if at some point you wish to replace either framework or physics lib, it becomes hard to do so. And the code becomes hard to maintain if you do not seperate things a bit.

It may look like additional work, but it's actually the opposite.
Example of tiny keyboard manager:

Code: Select all
struct keyboard
{
enum { // keyCodes
K_UP = 0x01,
K_LEFT = 0x02,
K_RIGHT = 0x04,
K_DOWN = 0x08,
K_FIRE = 0x10,
}

int keys;

// to call from game loop:
int isPressed (int keyCode) {return keys & keyCode};

// to call from GLFW keyboead callback:
void PressKey (int keyCode) {keys |= keyCode};
void ReleaseKey (int keyCode) {keys &= ~keyCode};
};


Using thing like that you have nicely separatey GLFW from Newton.
You avoid threading issues.
You save hours of debugging by some minutes needed to set this up.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sun Nov 09, 2014 11:39 am

@Julio No I am not in sync because my internet connection is unstable :'( (when I download something it fails after 1-10 seconds)
@JoeJ
Ok I will modify my code. Thank you for your advice!
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: Issue with NewtonCreateBox/Sphere

Postby Sweenie » Sun Nov 09, 2014 1:07 pm

arkdemon wrote:@Julio No I am not in sync because my internet connection is unstable :'( (when I download something it fails after 1-10 seconds)


Are you running Windows 8.1 by any chance?

If you do, try this...
http://adamralph.com/2014/01/24/continu ... ndows-8-1/

I was almost going crazy two days ago when i got my new laptop.
Browsing forums and casual surfing worked pretty well but as soon as i started to download stuff or sync code with github the connection just stalled. Odd thing though was that my Win 7 desktop didn't have this problem.

So try it, unless you know your problem is due to your ISP or hardware.
Sweenie
 
Posts: 490
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Issue with NewtonCreateBox/Sphere

Postby arkdemon » Sun Nov 09, 2014 1:18 pm

Ok so I have done the interface, tested it and it seems to work :)

ANd thank you for your help, I think the problem is resolved ^^ !

@Sweenie no, I use Windows 7 :3
Well even with casual surfing it is unstable x). The problem came 3 or 4 days ago, but before that, everything was fine. I am pretty sure the issue comes from hardware / ISP (they're looking at the problem)
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm


Return to Bugs and Fixes

Who is online

Users browsing this forum: Bing [Bot] and 1 guest