Jump to content

Setting direction wIth character controller


Davaris
 Share

Recommended Posts

I'm learning to use the character controller and want to set the direction of a mesh to be the same direction the character controller is moving. Isubtracted last_position from current_position, to determine the direction vector and then used RotateEntity, but the mesh always faces the same direction. Is there a special function I should be using to get the direction vector that the character controller is facing?

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

I'm learning to use the character controller and want to set the direction of a mesh to be the same direction the character controller is moving. Isubtracted last_position from current_position, to determine the direction vector and then used RotateEntity, but the mesh always faces the same direction. Is there a special function I should be using to get the direction vector that the character controller is facing?

 

 

 

Example?

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

#include "engine.h"


int main(int argc, char** argv)
{
Initialize();

//Create a graphics context
Graphics(800,600);

//Create a world
if (!CreateWorld()) 
{
	MessageBoxA(0,"Error","Failed to create world.",0);
	goto exitapp;
}



//Create a camera
TEntity cam=CreateCamera();
CameraClearColor(cam,Vec4(0,0,1,1));
PositionEntity(cam,Vec3(10,15,-5));
RotateEntity(cam,Vec3(45,45,0));

//Load a model
TModel scene=LoadModel("Character_Controllers_Files/scene.gmf");
if (scene==0) 
{
	MessageBoxA(0,"Error","Failed to load model.",0);
	goto exitapp;
}

//Create a light
TLight light=CreatePointLight();
PositionEntity(light,Vec3(0,5,0));

//Create a render buffer
TBuffer buffer=CreateBuffer(800,600,BUFFER_COLOR|BUFFER_DEPTH|BUFFER_NORMAL);



EntityType(scene,2);
Collisions(1,2,true);


//	TMesh cube=CreateCube();
//	ScaleEntity(cube,Vec3(0.4, 1.8, 0.4)); 

float playerheight = 1.5;//1.8;

TController player=CreateController(playerheight,0.4,0.5, 46);
EntityType(player,1);

PositionEntity(player,Vec3(0,5,0));
SetBodyMass(player,1);

float move=0.0;
float strafe=0.0;

//DebugPhysics(1);
TVec3 camrotation=Vec3(0);
float mx=0;
float my=0;

HideMouse();
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);

SetBodyDamping(player,0.0);
SetWorldGravity(Vec3(0,-20,0));

//Load the animated mesh
TMesh mesh=LoadMesh("crawler.gmf");

TEntity head=FindChild(mesh,"Bip01 Head");


int sequence=1;
float framebegin;
float frameend;
float frame;

TVec3 currplayerpos, lastplayerpos;

//Main loop
while(!KeyHit(KEY_ESCAPE)) 
{
	if (KeyDown(KEY_LEFT)) { TurnEntity(head,Vec3(0,0.5,0));}
	if (KeyDown(KEY_RIGHT)) { TurnEntity(head,Vec3(0,-0.5,0));}
	if (KeyDown(KEY_UP)) { TurnEntity(head,Vec3(0.5,0,0));}
	if (KeyDown(KEY_DOWN)) { TurnEntity(head,Vec3(-0.5,0,0));}

	UpdateAppTime();


	float jump=0.0;
	//Player movement
	move=3*(KeyDown(KEY_W)-KeyDown(KEY_S));
	strafe=3*(KeyDown(KEY_D)-KeyDown(KEY_A));



	if (KeyHit(KEY_SPACE)) 
	{
		if (!ControllerAirborne(player)) 
		{
			jump=4.0;
		}
	}

	if (KeyDown(KEY_LSHIFT)||KeyDown(KEY_RSHIFT)) 
	{
		if (!ControllerAirborne(player)) 
		{
			move*=2.0;
			strafe*=2.0;
		}
	}

	// SETTING ANIMATION CYCLE or MODE
	// walking
	if (move != 0 || strafe != 0)// || ControllerAirborne(player))
	{
		framebegin=70.0;
		frameend=130.0;
		// running
		if (KeyDown(KEY_LSHIFT)||KeyDown(KEY_RSHIFT)) 
		{
			framebegin=131.0;
			frameend=171.0;
		}
	}
	// idle
	else
	{
		framebegin=0.0;
		frameend=69.0;
	}

	//Animate the mesh
	frame=AppTime()/100.0;
	frame=fmodf(frame,frameend-framebegin)+framebegin;
	Animate(mesh,frame,1.0,0,true);

	//mx=Curve(MouseX()-GraphicsWidth()/2,mx,6);
	//my=Curve(MouseY()-GraphicsHeight()/2,my,6);
	//MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
	//camrotation.X=camrotation.X+my/10.0;
	//camrotation.Y=camrotation.Y-mx/10.0;
	//RotateEntity(cam,camrotation);

	//Set controller input
	//UpdateController(player,0.0,move,strafe);
	UpdateController(player,camrotation.Y,move,strafe,jump, 100);

	//TVec3 playerpos=EntityPosition(player);
	//playerpos.Y+=1.75;
	//PositionEntity(cam,playerpos);




	TVec3 playerpos=EntityPosition(player);

	playerpos.Y -= 0.8;

	TVec3 camerapos=EntityPosition(cam);
	camerapos.Y=Curve(playerpos.Y+1.75,camerapos.Y,2.0);
	camerapos=Vec3(playerpos.X,camerapos.Y,playerpos.Z);
//	PositionEntity(cam,camerapos);

	// WHERE THE PLAYER IS
	PositionEntity(mesh,playerpos);

	// THE PLAYERS DIRECTION USING VECTOR SUBTRACTION
/*
	//if (move != 0 || strafe != 0)
	{
		lastplayerpos = currplayerpos;
		currplayerpos = EntityPosition(player);
		TVec3 dir = currplayerpos - lastplayerpos;
		dir = EntityRotation(player);//Normalize(dir);
		RotateEntity(mesh,dir);
	}
	//	RotateEntity(mesh,Vec3(45,45,0));
*/

	// THE PLAYERS DIRECTION USING strafe, move
	TVec3 dir = Vec3(strafe,0,move);
	dir=TFormVector(dir,0,player);
	//dir = Normalize(dir);
	RotateEntity(mesh,EntityRotation(player));


	// ISOMETRIC CAMERA THAT PUTS THE PLAYER IN THE
	// CENTER OF THE SCREEN
	float multi = 1.0;
	TVec3 isopos = Vec3(5*multi,3*multi,-5*multi);
	PositionEntity(cam,camerapos + isopos);


	//Update the world
	UpdateWorld();

	//Render the scene
	SetBuffer(buffer);
	RenderWorld();

	//Render lighting
	SetBuffer(BackBuffer());
	RenderLights(buffer);

	//Swap the front and back buffer
	Flip();
}

exitapp:
return Terminate();
}

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Have you tried Parenting the animated model to the contoller?

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Something along the lines:

 

   TController player = CreateController(playerheight,0.4,0.5, 46);
  TMesh mesh=LoadMesh("crawler.gmf");
  MoveEntity(Mesh,"To where you want it");
  EntityParent(Mesh, player);
  PositionEntity(player,Vec3(0,5,0));
  EntityType(player,1);
  SetBodyMass(player,1);

 

then you wont need to up date it.

 

off the top of my head.

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...