wanted to share my working player/camera control action C++ app

Posted By: Neurosys

wanted to share my working player/camera control action C++ app - 08/30/08 00:30

I've never seen anyone compile a working example of all the major problem areas faced by beginners such as myself.
Namely, camera control, player control, animation and trigonometry. So now That I've met all my milestones with this little prototype, I'm quite pleased to share my code in its complete form more accurately illustrating(i hope) the ways these things can be successfully implemented.

I wish I'd have had something like this, well.. when I started making this. I hope it demonstrates well enough for some other people like me to overcome some of their theory obstacles.

I wrote this in C++ using the engine SDK (not making a dll).
It spawns a wmb with a terrain entity and a playermodel entity. real simple stuff. it assigns the function playercode to the player entity and tells the engine to hit it every frame (just like an action). Thats where the simple stuff ends.

The player code action/function seizes control of the camera, puts the camera behind the player a bit above it very 3rd person mmo looking. it follows the players position,height, pan,etc... if you click and hold the left mouse button, you get free cam mode orbiting the player with full 360 rotation along the x axis and about 100 degrees up and down tilt. letting go lets the camera drift smoothly back to its default position. holding down the right mouse button lets you control the players pan with mouse_force.x with the camera following along of course, holding both left and right makes the player move forward,play walk animation and still control the pan AND STILL move the cam freely as before. Of course, all that can be changed to fit the particular your application's needs for any variation of those effects.

scrolling the mouse wheel doesnt zoom by changing the camera.arc anymore (basically narrowing and widening the focal width) but rather increments and decrements the camera distance playerskill by small intervals. since the camera distance playerskill value is directly implemented into the orbit/rotation formula it fits beautifully and works perfectly to provide a realistic and smooth zooming effect without altering the focal width but by actually moving the camera along intermittent orbit tracks.
Gravity also works.

oh yea... w,s forward,back a,d turn left,right


ESC = sys_exit

Code:
#define WIN32_LEAN_AND_MEAN		
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "adll.h" // Include the engine data types, variables, and functions
#include "mtlFX.h"


//Prototypes
var rad2deg(float radians);
float deg2rad(var degrees);
var abz(var val);
//Globals

ENTITY* terrain1;
ENTITY* playermodel;
ENTITY* skycube;
BMAP* mcursor;
TEXT* tDebug;
PANEL* pDebug;




var abz(var val) {
	var retval;
	if (val >= _VAR(0)) { return val; }
	retval = _VAR(0) - val;
	return retval;
}


float deg2rad(var degrees) {
    float deg2rad = (float)(degrees / 57.29578);
	return deg2rad;
}



var rad2deg(float radians) {
    var rad2deg = _VAR(radians * 57.29578);
	return rad2deg;
}


//Function Definitions

void player_code() {
	ENTITY *my;
	my =(ENTITY *)ev->me;
	ev->player = my; // I'm the player
 	VECTOR* move_vec =_vec(0,0,0);
	VECTOR* temp_vec =_vec(0,0,0);
	var distance_to_ground;

	//Calculate Movement
	move_vec->x = (v(key_w) - v(key_s))* 0.015 * v(time_step); // move player on x axis
	move_vec->y = 0;//(v(key_a) - v(key_d)) * 0.025 *v(time_step); // move player on y axis
	my->pan-= (v(key_d) - v(key_a)) * 0.02 * v(time_step);

	//Read Forces
	if (v(mouse_left)) { 
		my->skill[0] += (v(mouse_force).x * 0.02 * v(time_step));
		my->skill[6] += (v(mouse_force).y * 0.02 * v(time_step));
		if ( my->skill[0] < _VAR(-360) ) { my->skill[0] += _VAR(360); }
		if ( my->skill[0] > _VAR(360) ) { my->skill[0] -= _VAR(360); }
		if ( my->skill[6] <= _VAR(-89) ) { my->skill[6] = _VAR(-90); }
		if ( my->skill[6] >= _VAR(9) ) { my->skill[6] = _VAR(10); }
	} else {
		if (my->skill[0] < _VAR(0)) { my->skill[0]+= 3 * v(time_step); }
		if (my->skill[0] > _VAR(0)) { my->skill[0]-= 3 * v(time_step); }
		if (my->skill[0] > _VAR(-1) && my->skill[0] < _VAR(1) ) { my->skill[0]=0; }
		if (my->skill[6] < _VAR(-61)) { my->skill[6]+= 3 * v(time_step); }
		if (my->skill[6] > _VAR(-59)) { my->skill[6]-= 3 * v(time_step); }
		if (my->skill[6] < _VAR(-61) && my->skill[6] > _VAR(-59) ) { my->skill[6]=-60; }
	}
	if (v(mouse_right)) { 
		my->pan-= v(mouse_force).x * 0.02 * v(time_step); 
		if( v(mouse_left) ){
			move_vec->x = _VAR(2) * 0.015 * v(time_step); // move player on x axis
		}
	}
	my->skill[3] -=v(mickey).z*0.0001;  //Calc Zoom Distance

	//Calculate Z Axis Gravity And Move Player
	vec_set(temp_vec,(VECTOR*)&my->x);
	temp_vec->z -= _VAR(5000); //Set to 5000 below player
	distance_to_ground = c_trace((VECTOR*)&my->x,temp_vec, _VAR(IGNORE_ME | USE_BOX));
	if (distance_to_ground != 0) {
		move_vec->z = -(distance_to_ground - _VAR(25));
		move_vec->z = maxv(_VAR(-35) * v(time_step),move_vec->z);
	}
	c_move(me,move_vec,_vec(0,0,0),GLIDE); // function to actually move player, with GLIDE collision
	
	//Animate As Needed
	if (v(key_w) == OFF && v(key_s) == OFF) {
		if(v(mouse_left) && v(mouse_right)){
  			ent_animate(my, "Walka", my->skill[5], ANM_CYCLE);
  		} else {
  			ent_animate(my, "Standa", my->skill[5], ANM_CYCLE);
  		}
	} else {
		ent_animate(my, "Walka", my->skill[5], ANM_CYCLE);
	}
	my->skill[5] += 6 * v(time_step);	
	my->skill[5] = my->skill[5]%_VAR(100);
	
	//Move The Camera
	if (abz(my->skill[0]) >= _VAR(0) && abz(my->skill[0]) < _VAR(90)) {
		vec_set((VECTOR*)&v(camera).x,vector(my->x -(my->skill[3] * cosv(my->pan + my->skill[0])),my->y -(my->skill[3] * sinv(my->pan + my->skill[0])),my->z+_VAR(my->skill[4])));
	} else if (abz(my->skill[0]) > _VAR(90) && abz(my->skill[0]) < _VAR(180)) {
		vec_set((VECTOR*)&v(camera).x,vector(my->x -(my->skill[3] * -cosv(_VAR(180)-(my->pan + my->skill[0]))),my->y -(my->skill[3] * sinv(_VAR(180)-(my->pan + my->skill[0]))),my->z+_VAR(my->skill[4])));
	} else if (abz(my->skill[0]) > _VAR(180) && abz(my->skill[0]) < _VAR(270)) {
		vec_set((VECTOR*)&v(camera).x,vector(my->x -(my->skill[3] * -cosv(_VAR(180)-(my->pan + my->skill[0]))),my->y -(my->skill[3] * sinv(_VAR(180)-(my->pan + my->skill[0]))),my->z+_VAR(my->skill[4])));
	} else if (abz(my->skill[0]) > _VAR(270) && abz(my->skill[0]) < _VAR(360)) {
		vec_set((VECTOR*)&v(camera).x,vector(my->x -(my->skill[3] * cosv(my->pan + my->skill[0])),my->y -(my->skill[3] * sinv(my->pan + my->skill[0])),my->z+_VAR(my->skill[4])));		
	}
	
	if (my->skill[6] < _VAR(0) && my->skill[6] > _VAR(-90)) {
		v(camera).z=my->z - my->skill[4] * sinv(my->tilt + my->skill[6]);
	} else if (my->skill[6] > _VAR(0) && my->skill[6] < _VAR(90)) {
		v(camera).z=my->z - my->skill[4] * sinv(_VAR(180)-(my->tilt + my->skill[6]));
	} 
	//Keep Eye On Player After Moving
	vec_set(temp_vec,(VECTOR*)&my->x); 
	vec_sub(temp_vec,(VECTOR*)&v(camera).x);
	temp_vec->z+=40;  
	vec_to_angle((ANGLE*)&v(camera).pan,temp_vec);
	
}

//WINMAIN!!!OMFGZ!!!
// application instance handle, // always zero // application command line // window flags
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) {
	ev = engine_open(NULL);
	if (!ev) return 1;	// acknex.dll not found
		
	//Add Content Folders Paths
	add_folder("terrains");
	add_folder("images");
	add_folder("models");
	add_folder("code");
	level_load("dh.wmb");
	engine_frame();
	
	//Initialize The Graphics Environment
	video_set(_VAR(1280),_VAR(1024),_VAR(32),_VAR(0));
	
	//Setup The Worldscape
	terrain1 = ent_for_name("terrain0x0");
	playermodel = ent_for_name("playermodel");  //These two are placed in the level by wed. This will change. Maybe. I hate wed.
	
	//Ready The Player
	playermodel->x=0;
	playermodel->y=0;
	playermodel->z=_VAR(550);
	//Init player skills
	playermodel->skill[0] = 0; //thetax
	playermodel->skill[1] = 0; //tempforcex
	playermodel->skill[2] = 0; //tempforcey
	playermodel->skill[3] = 180; //Camera Distance
	playermodel->skill[4] = 70; //Camera Height
	playermodel->skill[5] = 0; //Player Animation Percent
	playermodel->skill[6] = -60; //thetay
	playermodel->event=(EVENT)player_code;
	playermodel->emask |= DYNAMIC;
	playermodel->emask |= ENABLE_FRAME;
	
	//Wait for the player to materialize
	while(ev->player == NULL) { engine_frame(); }

	//Move Camera to Player And Look at the fucker
	vec_set((VECTOR*)&v(camera).x,_vec(-180,0,70));
	vec_rotate((VECTOR*)&v(camera).x,(ANGLE*)&v(player).pan);
	vec_add((VECTOR*)&v(camera).x,(VECTOR*)&v(player).x);
	v(camera).pan = v(player).pan;

	//Stall For A Frame (add more if ur compy sux0rz)
	engine_frame();
	
	//Make a Pretty Sky
	skycube = (ENTITY *)ent_createlayer("skycube+6.tga", _VAR(SKY | CUBE | VISIBLE), _VAR(0));

	//Bring Up The Mouse
	mcursor = bmap_create("brokenarrow.pcx");
	ev->mouse_map = mcursor;
	v(mouse_mode)=2;

	//GUI time
	tDebug = txt_create(_VAR(5),_VAR(1));
	tDebug->pos_x = _VAR(200);
	tDebug->pos_y = _VAR(200);
	tDebug->flags |= VISIBLE;
	pDebug = pan_create("digits(0,10,\"TEMPFORCEX: \",*,0,NULL); \
						digits(70,10,4,*,1,player.skill2); \
						digits(0,20,\"TEMPFORCEY: \",*,0,NULL);\
						digits(70,20,4,*,1,player.skill3); \
						digits(140,10,\"THETA X: \",*,0,NULL); \
						digits(230,10,4,*,1,player.skill1); \
						digits(310,10,\"THETA Y: \",*,0,NULL); \
						digits(410,10,4,*,1,player.skill7); \
						digits(140,20,\"Camera Distance: \",*,0,NULL); \
						digits(230,20,4,*,1,player.skill4); \
						digits(140,30,\"Camera Height: \",*,0,NULL); \
						digits(230,30,4,*,1,player.skill5); \
						digits(0,40,\"PLAYER X: \",*,0,NULL);\
						digits(70,40,4,*,1,player.x); \
						digits(0,50,\"PLAYER Y: \",*,0,NULL);\
						digits(70,50,4,*,1,player.y); \
						digits(0,60,\"PLAYER Z: \",*,0,NULL);\
						digits(70,60,4,*,1,player.z); \
						digits(140,40,\"Camera X: \",*,0,NULL);\
						digits(230,40,4,*,1,camera.x); \
						digits(140,50,\"Camera Y: \",*,0,NULL);\
						digits(230,50,4,*,1,camera.y); \
						digits(140,60,\"Camera Z: \",*,0,NULL);\
						digits(230,60,4,*,1,camera.z); \
						digits(140,120,\"AnimPercent: \",*,0,NULL);\
						digits(230,120,4,*,1,player.skill6); \
						digits(0,90,\"MOUSELEFT: \",*,0,NULL);\
						digits(70,90,4,*,1,mouse_left); \
						digits(0,100,\"MOUSERIGHT: \",*,0,NULL);\
						digits(70,100,4,*,1,mouse_right);",_VAR(1));
	pDebug->flags |= VISIBLE;
	pDebug->pos_x = _VAR(600);
	pDebug->pos_y = _VAR(200);

	//Main Loop
	while (engine_frame()) {
		//Main Loop
		v(mouse_pos).x = v(mouse_cursor).x;
		v(mouse_pos).y = v(mouse_cursor).y;
		if (v(key_esc)) { sys_exit(""); }
	}//Main Loop
	engine_close();
	return 0;
}


Hope It Helps Someone, Wish I'd Have Had It,
Neurosys
Posted By: Altimeter

Re: wanted to share my working player/camera control action C++ app - 08/31/08 17:22

nice contribution, I wished I had had something like this in the early stages of my project. It would have saved some sleepless nights of trial and error. You should put it into the wiki, where the C++ section needs some examples like this.
Posted By: Neurosys

Re: wanted to share my working player/camera control action C++ app - 09/01/08 13:01

aw hell yea i forgot about the wiki! I was just gonna keep updating this thread but now i can just update a wiki entry. coolness.. I just added jumping with jump and fall/landing animations.. I'll throw that up here in a few.
Posted By: Max_Prower

Re: wanted to share my working player/camera control action C++ app - 09/10/08 21:48

Thanks for the help, guys but I'm a beginner, I don't understand all those codes and stuff. ^^; Would there be any basic scripts to just hold down a button and drag a target model in a certain direction?
Posted By: Neurosys

Re: wanted to share my working player/camera control action C++ app - 10/18/08 23:30

Originally Posted By: Max_Prower
Thanks for the help, guys but I'm a beginner, I don't understand all those codes and stuff. ^^; Would there be any basic scripts to just hold down a button and drag a target model in a certain direction?


You should examine C-Script and Lite-C for this. 3DGS comes with several templates for doing what you describe. This example is for those utilizing the engine SDK and C++ not litec, For instance you might return to the Higher langauges section once you are more familiar with litec usage and have gained an aptitude for c++ endeavors, which are for achieving slightly more complex amount of application control.
Posted By: Azrael85

Re: wanted to share my working player/camera control action C++ app - 12/07/08 12:07

i've tried to compile your code so to know and and understand the engine_vars but
i've got an error that i don't understand

1>project.obj : error LNK2001: unresolved external symbol "struct ENGINE_VARS * ev" (?ev@@3PAUENGINE_VARS@@A)
1>C:\work\project\Debug\project.exe : fatal error LNK1120: 1 unresolved externals

i've linked the sdk_engine correctly and the 3 tutorials worked fine

can you tell me what could have gone wrong?
Posted By: pararealist

Re: wanted to share my working player/camera control action C++ app - 12/07/08 19:02

you have to declare

ENGINE_VARS * ev;

before you use it.

then you can use

ev = engine_open(NULL) ;

ect.
Posted By: Azrael85

Re: wanted to share my working player/camera control action C++ app - 12/08/08 13:58

thx
Posted By: KiwiBoy

Re: wanted to share my working player/camera control action C++ app - 05/08/09 05:52

Tried it out but got this...

Error in "c;\progfiles\Gstudio7\include\windows.h' line 6519 syntax error < RECT rcPaint ..0.203 sec

...on start up.

Copy and paste directly into SED then saved as .c file.
Should I do something else to get it to run or does it need a compiler installed.
Posted By: Anonymous2009

Re: wanted to share my working player/camera control action C++ app - 05/13/09 14:43

C++ uses .cpp extension.
Posted By: Quad

Re: wanted to share my working player/camera control action C++ app - 05/13/09 14:59

@kiwiboy

this code is meant for use with engine sdk and c++ (probably with visual c++ 2008 express or code::blocks), not with lite-c
Posted By: KiwiBoy

Re: wanted to share my working player/camera control action C++ app - 05/19/09 10:14

Oh ok, thanks...
Posted By: CHaP

Re: wanted to share my working player/camera control action C++ app - 06/02/09 13:01

THX a lot!

Now I can learn more about 3dg using with c++.

Good work!
Posted By: Goodberry

Re: wanted to share my working player/camera control action C++ app - 07/12/09 02:24

you absolutely rock. Thank you so much smile
Posted By: Neurosys

Re: wanted to share my working player/camera control action C++ app - 08/02/09 20:09

@Quadraxas

just to clarify for everyone, it was using VC++ 2005, setup via the instructions in the 3dgs manual for vc++ 2005. DX8 I think. ( i run dx 10 but i think it uses the libs for 8 so i had to install them)

@everyone else

Your welcome smile glad you like it. would love to see anything you guys make with/from it. I'm getting back into 3dgs so I will post more stuff too I hope soon.
Posted By: Neurosys

Re: wanted to share my working player/camera control action C++ app - 08/04/09 17:08

w0000t w00000t 2,500 VIEWS in 11 months!!
227 views per month
56 views per week
8 Views PER DAY!!!

my forum posts now officially get more traffic than my website smile
Posted By: TheGringe

Re: wanted to share my working player/camera control action C++ app - 03/13/11 11:35

This question might be stupid, because others seem to get the code to work, but I wonder which map shall be loaded. Should I create it on my own, is it an example-map from an older GS-version (I have GS8) or have you uploaded it and I don't know it?

And does it also work without mtlFX.h? I don't have it so I commented that header out and it works (apart from the map-problem).

But though I don't get it work yet, it helped me a lot; thank you.
© 2024 lite-C Forums