NewtonWOWstyle

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

NewtonWOWstyle

Post by xtayxkjy »

I have been in irrlicht forum for three month,this is my first topic.
Using irrlicht 1.42, IrrNewt_SDK_0_4,
Open NewtonWOWstyle/display/bin/demo.exe
This is a real WOW style except for mouse move hasn`t been completed.
You can use button"W" "S" "A" "D" "Q" "E" to move and rotate.Mouse wheel can room your camera to your object smoothly.
Button"C" "G" "L" "F" can be used for other functions.
Source code is in NewtonWOWstyle/Source/demo, The project uses Visual C++ 2005,
:D Good luck to you!
http://www.filexoom.com/showfile-5794/n ... wstyle.zip
Image
Image
Image
Image
Image
The following is my real project.
Image
Image
Image
Image

real robot done only 10%
Last edited by xtayxkjy on Thu Jan 01, 2009 6:59 am, edited 10 times in total.
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Re: NewtonWOWstyle

Post by xtayxkjy »

Code: Select all

#include "IrrNewt.hpp"
#include <string>
#include <direct.h>
#include <sstream>
#include <vector>
#include <iostream>
#include "irrlicht.h"
#include <math.h>
using namespace irr;

using namespace core; 
using namespace scene; 
using namespace video; 
using namespace gui;
//------------------------------------------------------------
bool up=false,down=false,left=false,right=false,can_move=true;
bool HorizontalLeft = false,HorizontalRight = false;
bool high_rotation=false;
bool MOUSE_WHEEL_Event =false;
//---------------------------------------------------
const bool solve_model_bug=false;

const float rigell_gravity_y_force=-1000*9.8f;
const float rigell_rotation_speed=0.3f;
const float rigell_high_rotation_speed=1.15f;

const float rigell_speed=10;

const float sphere_gravity_y_force=-20.0f;
const float sphere_force = 1300;
const float sphere_size=6.2f;

const float fire_cube_size=10;
const int fire_cube_particle_system_scale=2;

//pin constants
const float pin_mass=1.0f;
const float pin_gravity_y_force=-50*pin_mass;

const bool set_pin_linear_damping=true;
const float pin_linear_damping=0.4f;

const bool set_pin_level_friction=false;
const float pin_level_static_friction=0.9f;
const float pin_level_kinematic_friction=0.1f;

const f32 CameraStartPosX_Default =-200,CameraStartPosY_Default = 100;
const f32 CameraEndPosX_Default = 200,CameraEndPosY_Default = 0;

const f32 CameraLength_X_Default = CameraStartPosX_Default-CameraEndPosX_Default;
const f32 CameraLength_Y_Default = CameraStartPosY_Default-CameraEndPosY_Default;
const f32 CameraLengthSquare_X_Default = pow(CameraLength_X_Default,2);
const f32 CameraLengthSquare_Y_Default = pow(CameraLength_Y_Default,2);

const f32 CameraLength_S_Default = sqrt(CameraLengthSquare_X_Default+CameraLengthSquare_Y_Default);
const f32 resolution_x = 1024,resolution_y = 768;
const bool fullscreen_mode = true;
bool mouseDownL = false; 
bool mouseDownM = false; 
bool mouseDownR = false;
float CameraStartPosX = CameraStartPosX_Default,CameraStartPosY = CameraStartPosY_Default;

f32 lastWheelMovement=0; 
f32 zoom = 0;
//GENERAL VARIABLES

IrrlichtDevice* device;
video::IVideoDriver* driver;
scene::ISceneManager* smgr;
scene::ICameraSceneNode* camera;
scene::IAnimatedMeshSceneNode* rigell_node;
scene::ISceneNode* sphere_node;

irr::SIrrlichtCreationParameters device_settings;
std::vector<irr::newton::IBody*> pins;
irr::newton::ICollisionManager* ICManager;
irr::newton::ICharacterController* rigell_p_node;
irr::newton::ICharacterController* sphere_p_node;
irr::newton::IBody* level_p_node;
irr::newton::IWorld* p_world;
irr::newton::IMaterial* fire_material;
irr::newton::IMaterial* character_material;
irr::newton::IMaterial* pin_material;
irr::newton::IMaterial* ball_material;
core::vector3df CameraEndPos;
core::vector3df  CameraStartPos;
position2di MouseclickPosition;
//a material wich refuse all contacts. used for temporany bodies (used for calculations)
irr::newton::IMaterial* temp_fire_material;
int debug_info=0;

//TEMP
irr::core::array< irr::core::line3d<f32> > all_lines;

//------------------------------------------------------
//append a number to a string
template<class Num> std::string NumString(Num num) {
	std::ostringstream stringa;
	stringa<<num;
	return stringa.str();
}

//convert a const char* to a wchar_t*
wchar_t* CharWChar_T(const char* s,int max_char_size) {
	wchar_t* w_string=new wchar_t[max_char_size];
	mbstowcs(w_string,s,max_char_size);
	return w_string;
}

//----------------------------------------------------------------

//game functions
//rigell animations
int last_anim=0;

void AnimateMs3d(scene::IAnimatedMeshSceneNode* node,int inizio, int fine,int totale){
	if (inizio==0)
		node->setFrameLoop(((s32)(1.984f*totale)), ((s32)(fine*41.65f)));
	if (inizio!=0)
		node->setFrameLoop( (s32)(inizio*41.65f),  (s32)(fine*41.65f) );
}

void Stand() {
	if(last_anim!=1) {
		last_anim=1;
		rigell_node->setFrameLoop(1,1);
		rigell_node->setAnimationSpeed(100);
		rigell_node->setLoopMode(false);
	}
}

void Run() {
	if(last_anim!=2) {
		last_anim=2;
		rigell_node->setMD2Animation("run");/*setFrameLoop(33, 350)/*, 94)*/;
		rigell_node->setAnimationSpeed(100);
		rigell_node->setLoopMode(true);
	}
}

//sow a tex in the center of the screen for some seconds
void ShowText(wchar_t* text) {
	static scene::ITextSceneNode* text_node=NULL;
	static irr::u32 last_time=device->getTimer()->getTime();

	const static irr::u32 life_time=2500;

	if(text!=NULL) {

		if(text_node!=NULL) {
			text_node->remove();
			text_node=NULL;
		}

		text_node=smgr->addTextSceneNode(
			device->getGUIEnvironment()->getBuiltInFont(),
			text,
			video::SColor(255, 255, 255, 255),
			0,
			core::vector3df(
			0,
			0,
			1)
			);

		text_node->setScale(core::vector3df(100,100,100));
		text_node->updateAbsolutePosition();
		camera->addChild(text_node);
		last_time=device->getTimer()->getTime();
	}//if(text_node!=NULL) {

	if((device->getTimer()->getTime()-last_time)>=life_time&&
		text_node!=NULL) {

			text_node->remove();
			text_node=NULL;
	}

}

//create a box
void CreatePin(core::vector3df pos) {

	scene::ISceneNode* pin_node=smgr->addCubeSceneNode(20);
	pin_node->setMaterialTexture(0,
		driver->getTexture("../../media/wheel_texture.jpg"));
	pin_node->setMaterialFlag(video::EMF_LIGHTING,false);
	pin_node->setPosition(pos);

	pin_node->setScale(core::vector3df(0.5f,1.5f,0.5f));

	//physics
	newton::SBodyFromNode boxData;
	boxData.Node=pin_node;
	boxData.Mass=pin_mass;
	irr::newton::IBody* pin_body=p_world->createBody(boxData);

	pin_body->addForceContinuous(core::vector3df(0,
		pin_gravity_y_force,0));

	pin_body->setContinuousCollisionMode(true);
	pin_body->setMaterial(pin_material);

	if(set_pin_linear_damping)
		pin_body->setLinearDamping(pin_linear_damping);

	pins.push_back(pin_body);
}

//--------------MATERIAL COLLISION CALLBACK-----------------
//callback between character and blazing sphere

class Material_Character_Fire_class:public irr::newton::IMaterialCollisionCallback{
public:
	virtual int ContactBegin(
		irr::newton::IMaterialPair* material,
		irr::newton::IBody* body0,
		irr::newton::IBody* body1
		) {

			/*
			irr::f32 value=-(rigell_speed*100); 
			rigell_node->updateAbsolutePosition();

			core::vector3df force=rigell_p_node->FRIgetDirectionPositionY(core::vector3df(
			value,
			0,
			0));

			//rigell_p_node->setRotation(last_rot);

			//get sphere body
			newton::IBody* sphere_body;
			if(body0==rigell_p_node) sphere_body=body1;
			else sphere_body=body0;


			//improve dimension of sphere body by creating a new bigger sphere
			scene::ISceneNode* temp_sphere=smgr->addSphereSceneNode(fire_cube_size*2.8);
			temp_sphere->setPosition(sphere_body->getPosition());
			temp_sphere->setMaterialFlag(video::EMF_BACK_FACE_CULLING,false);

			newton::SBodyFromNode newSphereData;
			newSphereData.Node=temp_sphere;
			sphere_body=p_world->createBody(newSphereData);

			sphere_body->setMaterial(temp_fire_material);

			//create line
			core::line3d<f32> line;
			line.start=rigell_p_node->getPosition();
			line.end=rigell_p_node->getPosition()+force;
			line.start.Y=line.end.Y=sphere_body->getPosition().Y;

			//ceck collision between line and sphere
			irr::newton::SIntersectionPoint point;
			bool result=p_world->getCollisionManager()->getCollisionPoint(sphere_body,line,point);

			//if collide move in the opposite direction
			if(result) {
			core::vector3df line_vector=line.getVector();
			line_vector.invert();

			force=rigell_p_node->FRIgetDirectionPositionY(
			core::vector3df(
			-value,
			0,
			0));
			}


			rigell_p_node->addForce(force);
			*/

			//		rigell_p_node->setVelocity(core::vector3df(0,0,0));
			//		rigell_p_node->setForce(core::vector3df(0,0,0));


			//		irr::newton::ICharacterController* rigell_p_node;
			core::vector3df last_direction=rigell_p_node->getLastDirectionParameter();
			irr::f32 last_time_elapsed=rigell_p_node->getLastTimeElapsed();
			//last_direction/=last_time_elapsed;

			last_direction+=rigell_node->getPosition();
			last_direction.invert();
			last_direction=last_direction.normalize();
			last_direction*=150.0f;//acceleration
			last_direction*=40.0f;//rigell_mass
			last_direction.Y=0;

			//debug
			std::string c=NumString(last_direction.X);
			c+=",";
			c+=NumString(last_direction.Y);
			c+=",";
			c+=NumString(last_direction.Z);
			c+=";\n";
			device->getLogger()->log(c.c_str());
			//debug end


			rigell_p_node->addForce(last_direction);

			rigell_p_node->cantSetVelocityWhileMove();
			//rigell_p_node->cantForceWhileMove();


			//remove the temporany sphere
			//sphere_body->remove();

			ShowText(L"Life -");

			return 1;
	}

	virtual int ContactProcess(
		irr::newton::IMaterialPairAndContact* material) {
			return 0;//refuse contact
	}
}Material_Character_Fire;


//create a blazing sphere
void CreateFire(core::vector3df pos) {
	scene::ISceneNode* cube=smgr->addSphereSceneNode(fire_cube_size);
	cube->setPosition(pos);

	scene::IParticleSystemSceneNode* ps = smgr->addParticleSystemSceneNode(
		false);
	ps->setParticleSize(core::dimension2d<f32>(
		/*20.0f, 10.0f*/ 22.5f,12.5f));

	scene::IParticleAffector* paf =ps->createFadeOutParticleAffector();
	ps->addAffector(paf);
	paf->drop();

	ps->setMaterialFlag(video::EMF_LIGHTING, false);
	ps->setMaterialTexture(0,driver->getTexture("../../media/fire.jpg"));
	ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);

	scene::IParticleEmitter* em = ps->createBoxEmitter(
		core::aabbox3d<f32>(-7,0,-7,7,1,7), //box
		/*core::vector3df(0.0f,0.03f,0.0f)*/
		core::vector3df(0.0f,0.03f,0.0f),//direction
		80,//minParticlesPerSecond
		100,//maxParticlesPerSecond
		video::SColor(0,255,255,255),//minStartColor
		video::SColor(0,255,255,255),//maxStartColor
		800,//life time min
		2000, //life time max
		0 //max angle degrees
		);

	ps->setEmitter(em);
	em->drop();

	ps->setScale(core::vector3df(
		fire_cube_particle_system_scale,
		fire_cube_particle_system_scale,
		fire_cube_particle_system_scale));

	//add the particle system as child of the cube
	cube->addChild(ps);

	//physics
	newton::SBodyFromNode fireData;
	fireData.Node=cube;
	fireData.Mass=2.0f;
	irr::newton::IBody* fire_body=p_world->createBody(fireData);

	fire_body->setMaterial(fire_material);
	fire_body->addForceContinuous(core::vector3df(0,-5,0));
}
//--------------------EVENT RECEIVER BEGIN-------------------------------
enum E_GAME_PHASE{
	EGP_NORMAL,
	EGP_SPHERE_GETTED
};

E_GAME_PHASE game_phase=EGP_NORMAL;

class MyEventReceiver:public IEventReceiver {
public:
	virtual bool OnEvent(const SEvent & event) {
		can_move=true;
		if(event.EventType == EET_KEY_INPUT_EVENT) {
			if(event.KeyInput.PressedDown == true) {

				if(event.KeyInput.Key==KEY_KEY_A) {
					left=true;
					high_rotation=true;
				}
				if(event.KeyInput.Key==KEY_KEY_D) {
					right=true;
					high_rotation=true;
				}
				if(event.KeyInput.Key==KEY_KEY_Q) {
					HorizontalLeft = true;
				}
				if(event.KeyInput.Key==KEY_KEY_E) {
					HorizontalRight = true;
				}


				if(event.KeyInput.Key==KEY_KEY_W) {
					Run();
					up=true;
				}
				if(event.KeyInput.Key==KEY_KEY_S) {
					Run();
					down=true;
				}
				if(event.KeyInput.Key==KEY_SPACE) {
					debug_info++;
					if(debug_info>2) debug_info=0;
				}

			}//if(pressed_down==true)
			
			else if(event.KeyInput.PressedDown == false) { //pressed down false
				if(event.KeyInput.Key==KEY_KEY_F) {
					CreateFire(camera->getPosition());
				}

				if(event.KeyInput.Key==KEY_KEY_W) {
					Stand();

					up=false;
					//rigell_p_node->setVelocityDirection(0,'u');
				}
				if(event.KeyInput.Key==KEY_KEY_S){
					Stand();

					down=false;
					//rigell_p_node->setVelocityDirection(0,'u');
				}

				if(event.KeyInput.Key==KEY_KEY_A) {
					left=false;
					high_rotation=false;
				}
				if(event.KeyInput.Key==KEY_KEY_D) {
					right=false;
					high_rotation=false;
				}
				if(event.KeyInput.Key==KEY_KEY_Q) {
					HorizontalLeft=false;
				}
				if(event.KeyInput.Key==KEY_KEY_E) {
					HorizontalRight=false;
				}

				if(event.KeyInput.Key==KEY_ESCAPE) {
					device->closeDevice();
				}

				if(event.KeyInput.Key==KEY_KEY_C)
					p_world->getUtils()->launchCube();


				//get the ball!!!
				if(event.KeyInput.Key==KEY_KEY_G) {
					const float distance_sphere_rigell=60.0f;

					if(game_phase!=EGP_SPHERE_GETTED) {

						if(rigell_node->getPosition().getDistanceFrom(sphere_node->getPosition())<=distance_sphere_rigell) {

							//get the ball
							game_phase=EGP_SPHERE_GETTED;

							//refuse contact between character and ball
							ball_material->setCollidable(character_material,false);

							//annul gravity force
							sphere_p_node->addForceContinuous(core::vector3df(0,sphere_gravity_y_force,0));

							//freeze
							sphere_p_node->setFreeze(true);

							sphere_node->setPosition(rigell_node->getPosition());
							sphere_node->updateAbsolutePosition();
							sphere_p_node->setNodeMatrixToBody();
						}//if(rigell_node->getPosition().getDistanceFrom(

						else{
							ShowText(
								L"You are too far for getting the sphere");
						}//else{
					}//if(game_phase!=EGPSPHERE_GETTED) {

					else{
						ShowText(
							L"You already have the sphere");
					}//else
				}//if(event.KeyInput.Key==KEY_KEY_G) {

				if(event.KeyInput.Key==KEY_KEY_M) {
					core::vector3df rot=sphere_node->getRotation();
					rot.Y=rigell_node->getRotation().Y;
					sphere_p_node->setRotation(rot);

					//sphere_p_node->addTorque(core::vector3df(20000,0,0));
				}

				//lauch the ball
				if(event.KeyInput.Key==KEY_KEY_L) {
					if(game_phase==EGP_SPHERE_GETTED) {
						game_phase=EGP_NORMAL;

						//unfreeze the sphere
						sphere_p_node->setFreeze(false);

						//accept contact between character and ball
						ball_material->setCollidable(character_material,true);

						//stop it (?)
						sphere_p_node->setVelocity(core::vector3df(0,0,0));

						//restore gravity force
						sphere_p_node->addForceContinuous(core::vector3df(0,sphere_gravity_y_force,0));

						core::vector3df pos = rigell_p_node->FRIgetDirectionAbsolutePositionY(core::vector3df(30,0,0));
						sphere_p_node->setPosition(pos);

						//sphere_node->setPosition(pos);
						//sphere_node->updateAbsolutePosition();

						//to fall the sphere

						irr::newton::SIntersectionPoint int_point;
						core::line3d<f32> line(
							sphere_node->getPosition(),
							sphere_node->getPosition());
						line.end.Y-=999999.0f;

						int_point=p_world->getCollisionManager()->getCollisionFirstPointEx(
							line);

						if(int_point.body!=NULL) {
							int_point.point.Y+=(sphere_p_node->getShapeSize().Y/2)+0.01;
							sphere_p_node->setPosition(int_point.point);
						}

						sphere_node->updateAbsolutePosition();
						//sphere_p_node->posRotScaleAsNode();

						//sphere_p_node->adjustRotationForDirectionFunctions();

						//set y rotation like character y rotation
						//otherwise addForceDirection move the sphere
						//considerating y rotation as 0

						rigell_node->updateAbsolutePosition();

						core::vector3df rot = sphere_node->getRotation();
						rot.Y = rigell_node->getRotation().Y;
						sphere_p_node->setRotation(rot);

						sphere_node->updateAbsolutePosition();

						core::vector3df force = rigell_p_node->FRIgetDirectionPositionY(
							core::vector3df(sphere_force,0,0));
						sphere_p_node->addForce(force);

					}

					else{
						ShowText(
							L"First get the sphere");

					}//else{
				}//if(event.KeyInput.Key==KEY_KEY_L) {
			}//else if(event.KeyInput.PressedDown == false) {

		}//if(event.EventType == EET_KEY_INPUT_EVENT) {
		
		if (event.EventType == EET_MOUSE_INPUT_EVENT)
		{
			// left mouse button state check 
			if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)  
			{
				MouseclickPosition = device->getCursorControl()->getPosition();
				
				mouseDownL = true; 
		
			}
			if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)  
			{
				mouseDownL = false; 
			}
			if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)   mouseDownR = true; 
			if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)      mouseDownR = false; 
			//鼠标滚轮移动事件
			if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
			{

				lastWheelMovement = event.MouseInput.Wheel; 
				MOUSE_WHEEL_Event = true;
			}//if(event.EventType == EGET_SCROLL_BAR_CHANGED)
		}	

		return false;
	}
}my_event_receiver;

//-----------------END OF EVENT RECEIVER--------------------------

void GetDeviceSettings() {
	//	printf("Please select the driver you want for this example:\n"\
	//		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5"\
	//		"\n (otherKey) exit\n");
	/*
	char i;
	std::cin>>i;

	switch(i)
	{
	case 'a': device_settings.DriverType = video::EDT_DIRECT3D9;break;
	case 'b': device_settings.DriverType = video::EDT_DIRECT3D8;break;
	case 'c': device_settings.DriverType = video::EDT_OPENGL;   break;
	default: exit(0);
	}

	printf("\n\nPlease press y if you want to active FSAA,"
	"otherwise press another key\n");

	std::cin>>i;
	switch(i)
	{
	case 'y': device_settings.AntiAlias = true;break;
	default: device_settings.AntiAlias = false;break;
	}
	*/
	device_settings.DriverType = video::EDT_DIRECT3D9;
	device_settings.AntiAlias = true;
	device_settings.Bits=16;
	device_settings.Fullscreen=fullscreen_mode;
	device_settings.WindowSize=core::dimension2d<s32>(resolution_x,resolution_y);
}

//MAIN FUNCTION

int main(int argc, char* argv) {


	
	GetDeviceSettings();
	device=createDeviceEx(device_settings);


	device->setEventReceiver(&my_event_receiver);

	smgr=device->getSceneManager();
	driver=device->getVideoDriver();

	driver->setTextureCreationFlag(video::ETCF_OPTIMIZED_FOR_QUALITY,true);

	scene::IMesh* world_mesh = smgr->getMesh("../../media/map1.MY3D")->getMesh(0);



	//world_mesh=smgr->getMeshManipulator()->createMeshWithTangents(world_mesh);
	//smgr->getMeshManipulator()->makePlanarTextureMapping(world_mesh,0.015f);
	//smgr->getMeshManipulator()->flipSurfaces(world_mesh);

	scene::ISceneNode* world_node=smgr->addOctTreeSceneNode(world_mesh);
	scene::ITriangleSelector* selector = 0;
	selector = smgr->createOctTreeTriangleSelector(world_mesh, world_node, 128);
	world_node->setTriangleSelector(selector);

	world_node->setScale(core::vector3df(10,10,10));
	world_node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,false);

	world_node->setMaterialType(video::EMT_LIGHTMAP_ADD);

	world_node->setPosition(core::vector3df(0,0,0));

	world_node->getMaterial(0).SpecularColor.set(0,0,0,0);

	//load a model
	scene::IAnimatedMesh* rigell_mesh = smgr->getMesh("../../media/rigell.md2");
	rigell_node =smgr->addAnimatedMeshSceneNode(rigell_mesh);
	rigell_node->setMaterialFlag(video::EMF_LIGHTING,false);
	rigell_node->setMaterialTexture(0,driver->getTexture("../../media/rigell.jpg"));
	rigell_node->setScale(core::vector3df(2,2,2));
	rigell_node->setPosition(core::vector3df(
		0,
		100,
		0));

	rigell_node->setRotation(core::vector3df(
		0,
		-90,
		0));


	//try to scale the model and see that IrrNewt automatically
	//calculate the correct size of the shape
	//rigell_node->setScale(core::vector3df(8,2,8));

	Stand();

	//create a sphere
	sphere_node = smgr->addSphereSceneNode(
		sphere_size);
	sphere_node->setMaterialFlag(video::EMF_LIGHTING,false);
	sphere_node->setMaterialTexture(
		0,driver->getTexture("../../media/water.jpg"));
	sphere_node->setPosition(core::vector3df(
		7.03517f,
		61.707f,
		270.943f));

	//load an FPS camera
	//#define IcameraNode
#ifdef IcameraNode
	camera=smgr->addCameraSceneNodeFPS();


#endif
	/*	gui::IGUIEnvironment* env = device->getGUIEnvironment();

	// 状态栏
	gui::IGUIStaticText* statusText;
	wchar_t tmp[255];
	static s32 lastfps = 0;
	s32 nowfps = driver->getFPS();
	swprintf(tmp, 255, L"Persever fps:%3d ", driver->getFPS());
	statusText->setText(tmp);
	*/
	// disable mouse cursor
	device->getCursorControl()->setVisible(true);

	//INI PHYSICS

	p_world=irr::newton::createPhysicsWorld(device);

	//rigell
	irr::newton::SBodyFromNode character_data;
	character_data.Type=newton::EBT_PRIMITIVE_CHAMFER_CYLINDER;
	character_data.Node=rigell_node;
	character_data.Mesh=smgr->getMesh("../../media/rigell.md2")->getMesh(0);			
	character_data.BodyOffsetFromNode.setScale(core::vector3df(1.0f,1.0f,2.0f));

	rigell_p_node=
		p_world->createCharacterController(
		p_world->createBody(
		character_data
		));

	rigell_p_node->setMass(40.0f);
	rigell_p_node->calculateMomentOfInertia();
	rigell_p_node->setRotationUpdate(false);
	rigell_p_node->setContinuousCollisionMode(true);


	//rotation constraint. avoid that the body rotate on x and z
	irr::newton::SJointUpVector upVectorData;
	upVectorData.ParentBody=rigell_p_node;
	upVectorData.PinDir.set(0.0f,1.0f,0.0f);
	irr::newton::IJointUpVector* rigell_p_node_up_vector_joint=
		p_world->createJoint(upVectorData);

	//rotation constraint. avoid that the body rotate on y
	upVectorData.PinDir.set(1.0f,0.0f,1.0f);
	rigell_p_node_up_vector_joint=
		p_world->createJoint(upVectorData);

	//create the bowling ball
	newton::SBodyFromNode sphereData;
	sphereData.Node = sphere_node;
	sphereData.Type = newton::EBT_PRIMITIVE_ELLIPSOID;

	sphere_p_node = p_world->createCharacterController(p_world->createBody(sphereData));
	sphere_p_node->setContinuousCollisionMode(true);
	sphere_p_node->setPosition(rigell_node->getPosition());

	//level
	level_p_node=
		p_world->createBodyAuto(
		world_node,
		world_mesh
		);

	if(solve_model_bug) {

		smgr->getMeshManipulator()->flipSurfaces(world_mesh);

		p_world->createBodyAuto(
			world_node,
			world_mesh
			);

	}
	//	camera=smgr->addCameraSceneNode(0,CameraStartPos,CameraEndPos);
	//	smgr->addCameraSceneNodeFPS(rigell_node);



	//materials!!
	character_material=p_world->createMaterial();
	pin_material=p_world->createMaterial();
	ball_material=p_world->createMaterial();
	temp_fire_material=p_world->createMaterial();
	irr::newton::IMaterial* level_material=p_world->createMaterial();
	fire_material=p_world->createMaterial();

	rigell_p_node->setMaterial(character_material);
	sphere_p_node->setMaterial(ball_material);
	level_p_node->setMaterial(level_material);

	if(set_pin_level_friction) {
		pin_material->setFriction(
			level_material,
			pin_level_static_friction,
			pin_level_kinematic_friction);
	}

	fire_material->setCollisionCallback(character_material,&Material_Character_Fire);

	//refuse all contacts, temp material
	temp_fire_material->setCollidable(level_material,false);
	temp_fire_material->setCollidable(character_material,false);
	temp_fire_material->setCollidable(pin_material,false);
	temp_fire_material->setCollidable(fire_material,false);
	temp_fire_material->setCollidable(level_material,false);

	//character material and fire
	character_material->setElasticity(fire_material,0.0f);
	character_material->setFriction(fire_material,0.0f,0.0f);
	character_material->setSoftness(fire_material,0.0f);
	character_material->setCollisionCallback(fire_material,&Material_Character_Fire);

	//character material and level

	//rigell_p_node->setLinearDamping(0.0f);
	//rigell_p_node->setAngularDamping(core::vector3df(0.0f,0,0));

	character_material->setFriction(level_material,0.37f,0.10f);
	character_material->setSoftness(level_material,0.0f);
	character_material->setElasticity(level_material,0.0f);

	//end of materials

	//create the pins
	CreatePin(core::vector3df(
		-27.8529f,
		61.7861f,
		677.379f));
	CreatePin(core::vector3df(
		-18.8529f,
		61.7861f,
		667.379f));

	//gravity forces
	rigell_p_node->addForceContinuous(core::vector3df(0,rigell_gravity_y_force,0));
	sphere_p_node->addForceContinuous(core::vector3df(0,sphere_gravity_y_force,0));


	SEvent event;
	//simulate the pression of the 'g' key
	//to get the sphere
	event.EventType=EET_KEY_INPUT_EVENT;
	event.KeyInput.PressedDown=false;
	event.KeyInput.Key=KEY_KEY_G;
	device->postEventFromUser(event);

	camera=smgr->addCameraSceneNode();

	while(device->run()) {

		if(device->isWindowActive()) {


			//update the physics
			//it's important that you call this function every game loop
			p_world->update();

			if(game_phase==EGP_SPHERE_GETTED) {
				sphere_p_node->setPosition(rigell_node->getPosition());

			}
			//moving
			core::vector3df velocity=core::vector3df();
			core::vector3df rigell_rotation=rigell_node->getRotation();
			if(HorizontalLeft) 
				velocity+=rigell_p_node->FRIgetDirectionPositionY(core::vector3df(0,0,rigell_speed));
			if(HorizontalRight) 
				velocity+=rigell_p_node->FRIgetDirectionPositionY(core::vector3df(0,0,-rigell_speed));

			if(right&&high_rotation) 
				rigell_rotation.Y+=rigell_high_rotation_speed;
			if(left&&high_rotation) 
				rigell_rotation.Y-=rigell_high_rotation_speed;

			rigell_node->setRotation(rigell_rotation);



			if(up)
				velocity+=rigell_p_node->FRIgetDirectionPositionY(core::vector3df(rigell_speed,0,0));	
			if(down)
				velocity+=rigell_p_node->FRIgetDirectionPositionY(core::vector3df(-rigell_speed,0,0));	

			
			if (MOUSE_WHEEL_Event){
				if(lastWheelMovement > 0){ 
					zoom-= 40;
					lastWheelMovement = 0;

				} 
				if(lastWheelMovement < 0){ 
					zoom+= 40;
					lastWheelMovement = 0; 

				} 
			}
			if(mouseDownR)
			{
				zoom-=5;
			}
			if(debug_info==1) {
				rigell_p_node->drawDebugInfo();
				sphere_p_node->drawDebugInfo();
			}
			else if(debug_info==2) p_world->drawAllDebugInfos();

			s32 ds = zoom;
			s32 dx = ds*CameraLength_X_Default/CameraLength_S_Default;
			s32 dy = ds*CameraLength_Y_Default/CameraLength_S_Default;
			CameraStartPosX =CameraStartPosX_Default+ dx;
			CameraStartPosY = CameraStartPosY_Default+ dy;
			CameraEndPos = rigell_p_node->FRIgetDirectionAbsolutePositionY(
				core::vector3df(CameraEndPosX_Default,CameraEndPosY_Default,0));
			CameraStartPos=rigell_p_node->FRIgetDirectionAbsolutePositionY(
				core::vector3df(CameraStartPosX,CameraStartPosY,0));
			camera->setPosition(CameraStartPos);
			camera->setTarget(CameraEndPos);	

			if(mouseDownL)
			{
				core::line3d<f32> ray = 
					smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(MouseclickPosition, camera);
				vector3df desiredPosition; 
				triangle3df outTriangle; 
				if(smgr->getSceneCollisionManager()->getCollisionPoint(ray, selector, desiredPosition, outTriangle)) 
				{ 
	//				printf("\n desiredPosition:%f,%f,%f \n",desiredPosition.X,desiredPosition.Y,desiredPosition.Z);
	//				printf("\nX坐标:%d,  Y坐标:%d\n",MouseclickPosition.X,MouseclickPosition.Y);
	//				rigell_p_node->setPosition(core::vector3df(desiredPosition.X,100,desiredPosition.Z));
	//				rigell_p_node->rotateFromMouse(event,1000);
	//				rigell_p_node->jump(-100,100);
	//				velocity=rigell_p_node->FRIgetDirectionAbsolutePositionY(core::vector3df(-desiredPosition.X,0,0));
				} 
			}
			rigell_p_node->setVelocity(velocity);
			driver->beginScene(true,true,video::SColor(0,0,0,0));
			smgr->drawAll();
			driver->endScene();

		}//isWindowActive
	}

	device->drop();
	return 0;
}
Last edited by xtayxkjy on Tue Dec 09, 2008 8:46 am, edited 3 times in total.
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Re: NewtonWOWstyle

Post by xtayxkjy »

I forgot to put lib and include folders in ZIP file.

Now,you can find it.
Last edited by xtayxkjy on Tue Dec 09, 2008 8:55 am, edited 1 time in total.
Ion
Posts: 5
Joined: Tue Dec 09, 2008 6:32 am
Location: Leduc, Alberta, Canada
Contact:

Post by Ion »

This is really nice! It clarified a few IrrNewt things for me, and I might consider using it! But you may have posted it in the wrong section. I don't know the sections well enough to justify that, though.
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Post by xtayxkjy »

Where should I put it?
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Post by xtayxkjy »

Some comments of the code is using chinese,I forgot to replace them with English.
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Post by xtayxkjy »

Left button can`t use right now.
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

Hello xtayxkjy and thanks for the contribution, I haven't seen many Irrnewt examples around here and its always nice to have more. If I may suggest a few things that I think will improve the post: -

1) Put a clear description in the original post describing what the program does.

2) Move one of the nice little screenshots into the origonal post too, I think people like pictures ( I know I do :D )

Well done.
xtayxkjy
Posts: 13
Joined: Sun Nov 16, 2008 8:41 am
Location: China

Post by xtayxkjy »

Hi.Frank Dodd,Thank you very much!
I will try your advise.
Post Reply