Newton + Irrlicht Question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
GavRo
Posts: 32
Joined: Fri Dec 29, 2006 12:15 am

Newton + Irrlicht Question

Post by GavRo »

I wanted to implement the newton physics engine, and I wasn't having any problems until I tried to create a camera that is linked to Newton using a Newton Body. Can someone please point me to a code snippet with a camera containing one?

Thanks in advance.
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Post by Yustme »

Hi,

I'm not sure but I think you can only apply the camera to a node. With newton you can apply the physics to that node.

Paste some code so we can see what you're trying :wink:
GavRo
Posts: 32
Joined: Fri Dec 29, 2006 12:15 am

Post by GavRo »

Code: Select all

class CNewtonianNode;

class CNewtonNodeManager
{
private:
	list<CNewtonianNode *> nodes;
	static u32 id_counter;
	unsigned long lastTime;
	bool firstRun;
public:
	void Update(u32 timeMS);
	CNewtonianNode * addMeshSceneNode(IMesh * mesh,NewtonCollision * collision,vector3df pos,vector3df rot,vector3df sca,float mass,float max_vel,float I[3]);
	CNewtonianNode * addAnimatedMeshSceneNode(IAnimatedMesh * mesh,NewtonCollision * collision,vector3df pos,vector3df rot,vector3df sca,float mass,float max_vel,float I[3])
	CNewtonianNode * addCameraSceneNode(vector3df position,vector3df target,float mass,float max_vel);
	void clearAll();
	void clear(u32 id);
	CNewtonianNodeManager();
	~CNewtonianNodeManager();
};

class CNewtonianNode : public IReferenceCounted
{
private:
	ISceneNode * node;
	NewtonBody * body;
	float max_velocity;
	vector3df velocity;
	u32 id;
	CNewtonianNodeManager * manager;
	friend class CNewtonianManager;
public:
	void setMatrix(matrix4 &mat);
	matrix4 getMatrix();
	ISceneNode * getNode();
	NewtonBody * getBody();
	void setMassMatrix(float mass,float Ixx,float Iyy,float Izz);
	float * getMassMatrix();
	void Update(u32 timeMS);
	CNewtonianNode();
	~CNewtonianNode();
	void setDestructorCallback(NewtonBodyDestructor callback);
	void setForceAndTorqueCallback(NewtonApplyForceAndTorque callback);
	void setTransformationCallback(NewtonSetTransform callback);
	float getForce();
	unsigned int getID();
	void setPosition(vector3df pos);
	void setRotation(vector3df rot);
	void remove();
	void setMaxVelocity(float vel);
	vector3df getVelocity();
	void setVelocity(vector3df velocity);
};

class ICameraNewtonSceneNode : public ICameraSceneNode
{
	public::
	CCameraSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id, 
			const vector3df& position = vector3df(0,0,0),
			const vector3df& lookat = vector3df(0,0,100),float mass,float max_vel);
	virtual ~CCameraSceneNode();
	virtual void setProjectionMatrix(const matrix4& projection);
	virtual const matrix4& getProjectionMatrix() const;
	virtual const matrix4& getViewMatrix() const;
	virtual bool OnEvent(const SEvent& event);
	virtual void setTarget(const vector3df& pos);
	virtual vector3df getTarget() const;
	virtual void setUpVector(const vector3df& pos);
	virtual vector3df getUpVector() const;
	virtual f32 getNearValue() const;
	virtual f32 getFarValue() const;
	virtual f32 getAspectRatio() const;
	virtual f32 getFOV() const;
	virtual void setNearValue(f32 zn);
	virtual void setFarValue(f32 zf);
	virtual void setAspectRatio(f32 aspect);
	virtual void setFOV(f32 fovy);
	virtual void OnRegisterSceneNode();
	void animate(u32 ms);
	virtual void render();
	virtual const aabbox3d<f32>& getBoundingBox() const;
	virtual const SViewFrustum* getViewFrustum() const;
	virtual void setInputReceiverEnabled(bool enabled);
	virtual bool isInputReceiverEnabled() const;
	virtual void serializeAttributes(IAttributes* out, SAttributeReadWriteOptions* options=0) const;
	virtual void deserializeAttributes(IAttributes* in, SAttributeReadWriteOptions* options=0);
	virtual ESCENE_NODE_TYPE getType() const { return ESNT_CAMERA; }
	virtual vector3df getAbsolutePosition() const;
	NewtonBody * body;
	private:
	void recalculateProjectionMatrix();
	void recalculateViewArea();
	vector3df Target;
	vector3df UpVector;
	f32 Fovy;	// Field of view, in radians. 
    f32 Aspect;	// Aspect ratio. 
	f32 ZNear;	// value of the near view-plane. 
	f32 ZFar;	// Z-value of the far view-plane.
	SViewFrustum ViewArea;
	ICursorControl * cursor;
	bool InputReceiverEnabled;
	float max_velocity;
};

The CNewtonianNode class would be for any scene node that Newton can modify and so in. ICameraNewtonSceneNode is like the FPS camera scene node, only that it collides with the scene. My main issue is really designing the camera class, as its confusing to know how I should update my camera. For example, if the user presses up, how do I know how to add velocity using NewtonBodySetVelocity() and how would I calculate the right vector(I know its aligned with the target vector, but what would its magnitude be?). Next, how would this whole thing work with strafing. The cameras I have done before (in plain openGL) didn't really implement collision detection or response. They were based off the apron tutorials camera lessons. So, does anybody have some source code for a Newton based camera that I could learn from.

(Yes, I stole some of that from the base ICameraSceneNode. I understand the transformations involved, but have problems making these things work with the Newton physics engine.)

Thanks in advance, and sorry if it sounds confusing.
Post Reply