Character Controller

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Character Controller

Postby shybovycha » Mon Jan 04, 2010 7:59 pm

I wanted to make some application using Newton 2.13 for Linux and its CharacterController. Everything compiled and runned well (with just rigid bodies only) but when i tried to add some CC functionality app even don't compiles... I'm using Ubuntu 9.10, easyEclipse 1.3.0 and Newton 2.13.

Here is my source code without everything unnecessary:

Code: Select all
#include <SFML/Graphics.hpp>
#include <Newton/Newton.h>
#include <Newton/dMath/dVector.h>
#include <Newton/dMath/dMatrix.cpp>
#include <Newton/dMath/dQuaternion.h>
#include <Newton/dMath/dMathDefines.h>
#include <Newton/JointLibrary.h>
#include <Newton/dCustomJoints/CustomJointLibraryStdAfx.h>
#include <Newton/dCustomJoints/CustomPlayerController.h>

#define PLAYER_SPEED      10.0f
#define PLAYER_SIDE_SPEED    5.0f
#define PLAYER_JOINT_ID      0xEF38AB01

void playerMove(CustomPlayerController *player, dFloat velocity, dFloat strafe, dFloat rotation)
   velocity *= PLAYER_SPEED;
   strafe *= PLAYER_SIDE_SPEED;
   player-> SetVelocity(velocity, strafe, rotation);

// add force and torque to rigid body
void  PhysicsApplyGravityForce (const NewtonBody* body, dFloat timestep, int threadIndex)
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;

   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
   dVector force (0.0f, -mass * 10.0f, 0.0f);
   NewtonBodySetForce (body, &force.m_x);

NewtonBody* CreatePlayBody (
   NewtonWorld* world,
   dFloat mass,
   const dMatrix& srcMatrix,
   const dVector& size,
   int matrixlId)
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   NewtonBody* rigidBody;
   NewtonCollision* collision;
   dVector origin;
   dVector inertia;

   dMatrix matrix (srcMatrix);
   matrix.m_front = matrix.m_front.Scale (1.0f / dSqrt (matrix.m_front % matrix.m_front));
   matrix.m_right = matrix.m_front * matrix.m_up;
   matrix.m_right = matrix.m_right.Scale (1.0f / dSqrt (matrix.m_right % matrix.m_right));
   matrix.m_up = matrix.m_right * matrix.m_front;

   // create the collision
   dMatrix location (dRollMatrix(-3.1416f * 0.5f));
   collision = NewtonCreateCapsule (world, size.m_y, size.m_x, 0, &location[0][0]);
//   collision = NewtonCreateCylinder (world, size.m_y, size.m_x, 0, &location[0][0]);

   // calculate the moment of inertia and the relative center of mass of the solid
   NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
   Ixx = mass * inertia[0];
   Iyy = mass * inertia[1];
   Izz = mass * inertia[2];

   //create the rigid body
   rigidBody = NewtonCreateBody (world, collision);

   // release the collision geometry when not need it
   NewtonReleaseCollision (world, collision);

   // set the correct center of gravity for this body
   NewtonBodySetCentreOfMass (rigidBody, &origin[0]);

   // set the mass matrix
   NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz);

   // assign the wood id
   NewtonBodySetMaterialGroupID (rigidBody, matrixlId);

   // set the force and torque call back function
   NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce);

   // set the matrix for both the rigid body and the graphic body
   NewtonBodySetMatrix (rigidBody, &matrix[0][0]);

   return rigidBody;

int main()
   NewtonWorld* g_world;

    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "мій курсовий кусень коду");

    // Enable Z-buffer read and write

    // Setup a perspective projection
    gluPerspective(90.f, 1.f, 1.f, 500.f);

   // Create a clock for measuring the time elapsed
    sf::Clock Clock;

    g_world = NewtonCreate();
    NewtonSetPlatformArchitecture(g_world, 0);

    // set a fix world size
    dVector minSize (-500.0f, -500.0f, -500.0f);
    dVector maxSize ( 500.0f,  500.0f,  500.0f);
    NewtonSetWorldSize (g_world, &minSize[0], &maxSize[0]);

    // configure the Newton world to use iterative solve mode 0
    // this is the most efficient but the less accurate mode
    NewtonSetSolverModel (g_world, 1);

    // NEWTON CC
    CustomPlayerController* player;
    NewtonBody* playerBody;

    dMatrix location (GetIdentityMatrix());
    location.m_posit.m_y = 0;

    dFloat kinematicCushion = 1.0f / 64.0f;
    dVector size (1.8748f, 0.4885f, 1.0f);

    playerBody = CreatePlayBody(g_world, 100, location, size, 0);

    // make sure the player is active
   dVector pin (0.0f, 1.0f, 0.0f, 0.0f);
   dMatrix globalFrame (GetIdentityMatrix());
   globalFrame.m_front = dVector (0.0f, 1.0f, 0.0f, 0.0f);        // up direction in global Space
   globalFrame.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);        // front direction in global Space
   globalFrame.m_right = globalFrame.m_front * globalFrame.m_up;  // strafing direction in global Space

   dFloat maxStairStepFactor = 0.7f / size.m_x;
   player = new CustomPlayerController(globalFrame, playerBody, maxStairStepFactor, kinematicCushion);

    dVector eye, lookat, up;
    eye = dVector(0, 250, 40);
    lookat  = dVector(0, 0, 0);
    up = dVector(0, 1, 0);

    // Start game loop
    while (App.IsOpened())
        // Process events
        sf::Event Event;

        NewtonUpdate(g_world, Clock.GetElapsedTime());

        while (App.GetEvent(Event))
            // Close window : exit
            if (Event.Type == sf::Event::Closed)

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed))
                switch (Event.Key.Code)
                    case sf::Key::Escape:

            // Adjust the viewport when the window is resized
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);

        // Clear depth buffer

        // Apply some transformations

        gluLookAt(eye.m_x, eye.m_y, eye.m_z, lookat.m_x, lookat.m_y, lookat.m_z, up.m_x, up.m_y, up.m_z);

        // Finally, display the rendered frame on screen

    // destroy all rigid bodies, this is not necessary because Newton Destroy world will also destroy all bodies
      // but if you want to change level and restart you can call this function.
      NewtonDestroyAllBodies (g_world);

      // finally destroy the newton world
      NewtonDestroy (g_world);

    return EXIT_SUCCESS;

And here is the compiler output:
**** Build of configuration Debug for project myGLApp ****

make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp" -I/usr/include/Newton
Finished building: ../main.cpp

Building target: myGLApp
Invoking: GCC C++ Linker
g++ -o"myGLApp" ./main.o -lsfml-graphics -lsfml-window -lpthread -lsfml-system -lGL -lGLU /usr/lib/libNewton.a -I/usr/include/Newton
./main.o: In function `main':
/home/errorist/workspace/myGLApp/Debug/../main.cpp:145: undefined reference to `NewtonCustomJoint::operator new(unsigned int)'
/home/errorist/workspace/myGLApp/Debug/../main.cpp:145: undefined reference to `CustomPlayerController::CustomPlayerController(dMatrix const&, NewtonBody const*, float, float)'
/home/errorist/workspace/myGLApp/Debug/../main.cpp:145: undefined reference to `NewtonCustomJoint::operator delete(void*)'
collect2: ld returned 1 exit status
make: *** [myGLApp] Error 1

Someone, tell me, please, what should i do to make CC work properly...
User avatar
Posts: 52
Joined: Fri Oct 23, 2009 6:15 am
Location: Poland

Return to General Discussion

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest