Super simple quick-start with 48 lines of C example

From Newton Wiki
Jump to: navigation, search

Other tutorials covers too wide range about using of Newton. This tutorial only covers really essential part for using Newton.

Sample Code

See this C code. (This tutorial made with Newton r615)

//  main.c
//  newton-tutorial-1
//  Created by Eonil on 5/7/11.
//  Copyright 2011 Eonil Company. All rights reserved.

#include <stdio.h>
#include <stdlib.h>
#include "Newton.h"

void	applyForceAndTorque (const NewtonBody* const body, dFloat timestep, int threadIndex)
	float	gravity		=	-9.8f;
	float	mass, ix, iy, iz;
	NewtonBodyGetMassMatrix(body, &mass, &ix, &iy, &iz);
	float	force[4]	=	{ 0.0f, gravity * mass, 0.0f, 1.0f };
	NewtonBodySetForce(body, force);

int main (int argc, const char * argv[])
	float				const	initialTM[16]	=	{ 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
	NewtonWorld*		const	world		=	NewtonCreate();
	NewtonCollision*	const	collision	=	NewtonCreateBox(world, 10, 10, 10, 0, NULL);
	NewtonBody*			const	body		=	NewtonCreateBody(world, collision, initialTM);
	NewtonBodySetForceAndTorqueCallback(body, applyForceAndTorque);
	NewtonBodySetMassMatrix(body, 10, 1, 1, 1);
	for	(int i=0; i<100; i++)
		NewtonUpdate(world, 1/60);
		float	newTM[16];
		NewtonBodyGetMatrix(body, newTM);
		printf("iteration = %d, position = %f, %f, %f\n", i, newTM[12], newTM[13], newTM[14]);

	printf("Hello, World!\n");
	return 0;

48 lines of C code including comment. Newton work only with this. You can verify this code is working by console output of a body's position is changing. These are all essential part to use Newton. You can overview how it works, and expand the code for your purpose.


You can compile this code, but this won't be compiled with pure C. You need to compile with C++. The code itself is completely pure C, but current version(r610) of Newton.h contains C++ features which cannot be compiled in plain C. I compiled successfully with Clang.


Object management codes are straightforward. But others are not general style. Newton is different with other physics engine in some points.

1. Position and rotations. Many other physics engines handles position and rotation as vector form, separated entities. But Newton handles them as matrix form. So you get get/put position and rotation information with matrix related functions. You can get position X/Y/Z from 12/13/14th element of the matrix. See here for more details:

2. Event driven. Newton is event driven. Most big different is applying force is done by callback. You can't apply force directly. You can apply the force when only in the callback. Otherwise, forces will be ignored. I guess there's some kind of special design behind it.

Take Care!

Take care about you have to supply well-made transformation matrix for initialTM. I recommend modifying values from identity matrix. Even if this matrix is wrong no errors occurs, but simulation result just go wrong. In my case it doesn't collide with other bodies.