Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (7th_zorro, Aku_Aku, 1 invisible), 579 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
wanted to share my working player/camera control action C++ app #224422
08/30/08 00:30
08/30/08 00:30
Joined: Aug 2008
Posts: 61
Neurosys Offline OP
Junior Member
Neurosys  Offline OP
Junior Member

Joined: Aug 2008
Posts: 61
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


See more code and crap @ www.neuroticnetworks.com
Re: wanted to share my working player/camera control action C++ app [Re: Neurosys] #224662
08/31/08 17:22
08/31/08 17:22
Joined: Mar 2007
Posts: 75
Hamburg, Ger
Altimeter Offline
Junior Member
Altimeter  Offline
Junior Member

Joined: Mar 2007
Posts: 75
Hamburg, Ger
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.

Re: wanted to share my working player/camera control action C++ app [Re: Altimeter] #224833
09/01/08 13:01
09/01/08 13:01
Joined: Aug 2008
Posts: 61
Neurosys Offline OP
Junior Member
Neurosys  Offline OP
Junior Member

Joined: Aug 2008
Posts: 61
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.


See more code and crap @ www.neuroticnetworks.com
Re: wanted to share my working player/camera control action C++ app [Re: Neurosys] #226722
09/10/08 21:48
09/10/08 21:48
Joined: Sep 2008
Posts: 76
Max_Prower Offline
Junior Member
Max_Prower  Offline
Junior Member

Joined: Sep 2008
Posts: 76
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?

Re: wanted to share my working player/camera control action C++ app [Re: Max_Prower] #232039
10/18/08 23:30
10/18/08 23:30
Joined: Aug 2008
Posts: 61
Neurosys Offline OP
Junior Member
Neurosys  Offline OP
Junior Member

Joined: Aug 2008
Posts: 61
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.


See more code and crap @ www.neuroticnetworks.com
Re: wanted to share my working player/camera control action C++ app [Re: Neurosys] #239798
12/07/08 12:07
12/07/08 12:07
Joined: Jul 2008
Posts: 27
Italy, Sicilia, Palermo
Azrael85 Offline
Newbie
Azrael85  Offline
Newbie

Joined: Jul 2008
Posts: 27
Italy, Sicilia, Palermo
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?

Re: wanted to share my working player/camera control action C++ app [Re: Azrael85] #239846
12/07/08 19:02
12/07/08 19:02
Joined: Dec 2006
Posts: 434
UK,Terra, SolarSystem, Milky W...
pararealist Offline
Senior Member
pararealist  Offline
Senior Member

Joined: Dec 2006
Posts: 434
UK,Terra, SolarSystem, Milky W...
you have to declare

ENGINE_VARS * ev;

before you use it.

then you can use

ev = engine_open(NULL) ;

ect.


A8.3x Commercial, AcknexWrapper and VS 2010 Express
&#9675;pararealist now.
Re: wanted to share my working player/camera control action C++ app [Re: pararealist] #239960
12/08/08 13:58
12/08/08 13:58
Joined: Jul 2008
Posts: 27
Italy, Sicilia, Palermo
Azrael85 Offline
Newbie
Azrael85  Offline
Newbie

Joined: Jul 2008
Posts: 27
Italy, Sicilia, Palermo
thx

Re: wanted to share my working player/camera control action C++ app [Re: Azrael85] #264781
05/08/09 05:52
05/08/09 05:52
Joined: Apr 2009
Posts: 298
Southern Oceans
KiwiBoy Offline
Member
KiwiBoy  Offline
Member

Joined: Apr 2009
Posts: 298
Southern Oceans
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.


Use the 'manual' Luke, the manual is your friend. 'Self reminder' smile

My WebPage
Re: wanted to share my working player/camera control action C++ app [Re: KiwiBoy] #265659
05/13/09 14:43
05/13/09 14:43
Joined: Jan 2009
Posts: 38
Anonymous2009 Offline
Newbie
Anonymous2009  Offline
Newbie

Joined: Jan 2009
Posts: 38
C++ uses .cpp extension.

Page 1 of 2 1 2

Moderated by  TWO 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1