, but now i'm having some problems, with the cpu load.
Code:
/******************************************************************************/
#include "stdafx.h"
/******************************************************************************/
Flt angle ; // car wheel angle
Actor car; // ground actor
MeshBase mshb;
Wheel wheel[4]; // wheels
Mesh autoMesh,ruedaMesh;
Material rueda0,rueda1;
Camera desired_camera;
/******************************************************************************/
#include "../../../../../data/enum/_enums.h"
/******************************************************************************/
/******************************************************************************/
Game::ObjMemx<Game::Static> Statics; // container for static objects
Game::ObjMemx<Game::Item > Items; // container for item objects
Game::ObjMemx<Game::Chr > Chrs; // container for character objects
/******************************************************************************/
void InitPre()
{
App.name("Juego Autos");
App.flag=APP_MS_EXCLUSIVE|APP_FULL_TOGGLE;
IOPath("../data");
Paks.add("../data/engine.pak");
D.full(true).sync(true).hpRt(true).hdr(true);
Cam.dist =10;
Cam.yaw =-PI_4;
Cam.pitch=-0.5;
Cam.at.set(16,0,16);
}
Bool Init()
{
Cam.dist=10;
Physics.create();
//Investigar sobre el motion blur porque todos los que pongo estan mal.
Game::World.init(); // initialize world, optionally you can change default parameters here
// once the world is initialized, we need to tell the world 'which class handles which type'
// this is done by assigning memory containers to certain Object Types defined in Game Enums (which were used in the World Editor)
Game::World.setObjType(Statics,OBJ_STATIC) // set 'Statics' memory container for 'OBJ_STATIC' objects
.setObjItem(Items ,OBJ_ITEM ) // set 'Items' memory container for 'OBJ_ITEM' objects
.setObjType(Chrs ,OBJ_PLAYER); // set 'Chrs' ' memory container for 'OBJ_PLAYER' objects
// now when the engine is set up properly we can start a 'new game' with a builded world
Game::World.New("world/ciudad.world"); // create the world by giving path to builded world
autoMesh.load("obj/auto/ferrari.mesh");
autoMesh.scale(Vec(2.3,2.3,2.3));
ruedaMesh.create(2);
ruedaMesh.load("obj/auto/rueda.mesh");
rueda0.load("Mtrl/rueda/cubierta2.mtrl");
rueda1.load("Mtrl/rueda/llanta2.mtrl");
ruedaMesh.part(1).setMaterial(&rueda1);
ruedaMesh.part(0).setMaterial(&rueda0);
//ruedaMesh.scale(Vec(0.8,0.8,0.8));
/*ground .create(Box_U(100,1,100),0);
ground .pos (ground.pos()-Vec(0,3,0));*/
Sun.image=Images("gfx/sky/sun.gfx");
Sky.atmospheric();
D.bumpMode(BUMP_FLAT);
//mshb.create(Torus(1,0.3),VTX_NRM);
Game::World.init(); // initialize world, optionally you can change default parameters here
// once the world is initialized, we need to tell the world 'which class handles which type'
// this is done by assigning memory containers to certain Object Types defined in Game Enums (which were used in the World Editor)
Game::World.setObjType(Statics,OBJ_STATIC) // set 'Statics' memory container for 'OBJ_STATIC' objects
.setObjItem(Items ,OBJ_ITEM ) // set 'Items' memory container for 'OBJ_ITEM' objects
.setObjType(Chrs ,OBJ_PLAYER); // set 'Chrs' ' memory container for 'OBJ_PLAYER' objects
// now when the engine is set up properly we can start a 'new game' with a builded world
Game::World.New("world/pista.world"); // create the world by giving path to builded world
// when the world is loaded it doesn't actually load all the terrain and objects into memory
// it loads only information about them
// you need to tell the world which terrain and objects you need to use at the moment
// to do that call:
Game::World.update(Cam.at); // which updates world to use only terrain and objects at given position, here camera position is used
// create car
Cam.setPosDir(Vec(1,3,1));
// create car wheels
car.mass(1500);
car.create (Box(2,1,4.5),2);//2,1,4 // create actor
car.massCenterL(car.pos()-Vec(0,0.8,0)); // lower mass center
car.pos(Vec(1,3,1));
Wheel::Param wp; // wheel parameters
wp.mass=20;
wheel[0].create(car,Matrix().setPos(Vec( 2,2, 2.2)),wp); // left -front
wheel[1].create(car,Matrix().setPos(Vec(0,2, 2.2)),wp); // right-front
wheel[2].create(car,Matrix().setPos(Vec( 0,2,-0.5)),wp); // left -rear
wheel[3].create(car,Matrix().setPos(Vec(2,2,-0.5)),wp); // right-rear
return true;
}
void Shut()
{
}
/******************************************************************************/
/******************************************************************************/
Bool Update()
{
Flt accel,brake;
if(Kb.bp(KB_ESC))return false;
CamHandle(0.1f,200,CAMH_ZOOM|(Ms.b(1)?CAMH_MOVE:CAMH_ROT));
/*
Control de la camara
INICIO
*/
// first setup the desired camera as in the previous tutorials
//desired_camera.yaw =LerpAngle(Cam.yaw,-car.matrix().angles().y,Time.d()*5);
desired_camera.yaw -=Ms.ds.x; // update camera yaw angle according to mouse smooth delta x
desired_camera.pitch+=Ms.ds.y; // update camera pitch angle according to mouse smooth delta y
Clamp(desired_camera.pitch,-PI_2,PI_4); // clamp to possible camera pitch angle
desired_camera.dist=Max(1.0f,desired_camera.dist*ScaleFactor(Ms.wheel()*-0.2)); // update camera distance according to mouse wheel
desired_camera.at =car.pos();
desired_camera.setSpherical(); // set as spherical from current values, this will set the camera's matrix (desired_camera.matrix)
// now what we'll do is cast a small sized Ball from starting position to target camera destination
// we'll stop the ball at first contact point, and set camera at that place
// create a helper ball which will be used for collision detection
Ball ball(0.1f, desired_camera.at); // we place it at starting point (where the camera is looking at)
// now we'll move the ball in the direction where the camera should be
Physics.move(ball, desired_camera.matrix.pos-ball.pos); // use physics movement to move the ball as far as it can go without any collisions
// now the ball.pos is located at either maximum movement distance or at nearest collision point
// having ball's position we can now set the final camera position
Cam.setPosDir(ball.pos, desired_camera.matrix.z, desired_camera.matrix.y); // we'll use 'desired_camera.matrix' directions which were set in 'setSpherical' camera method
Cam.updateVelocities().set(); // update camera velocities and activate it
//Fin del control de la camara
Physics.startSimulation().stopSimulation();
if(Kb.bp(KB_ENTER))car.pos(Vec(1,3,1)); // reset car position on ENTER key
// adjust car controls
{
AdjustValTime(angle,(Joypad[0].dir_a[0].x)*PI_4,0.01); // adjust wheel angle
accel=((Joypad[0].b(1)*2)-Joypad[0].b(2))*1200, // acceleration
brake= (Joypad[0].b(5)*5) *3200; // brakes
wheel[0].angle(angle).accel(accel); // set angle, acceleration and brakes to left -front wheel
wheel[1].angle(angle).accel(accel); // set angle, acceleration and brakes to right-front wheel
//4x4
wheel[2].brake(brake);
wheel[3].brake(brake);
}
/*if(accel>0){
accel=accel*0.8;
}*/
// update wheels
REPAO(wheel).update();
Game::World.update(Cam.at);
return true;
}
void Render()
{
Game::World.draw(); // draw world (this is done outside of 'switch(Renderer())' because world automatically detects active rendering mode)
// <------- HERE
switch(Renderer()){
case RM_PREPARE:
autoMesh.draw(car.matrix());
/*
Dibujo las ruedas
*/
ruedaMesh.draw(wheel[0].matrix());
ruedaMesh.draw(wheel[1].matrix());
ruedaMesh.draw(wheel[2].matrix());
ruedaMesh.draw(wheel[3].matrix());
break;
case RM_SHD_MAP:
ruedaMesh.drawShadow(wheel[0].matrix());
ruedaMesh.drawShadow(wheel[1].matrix());
ruedaMesh.drawShadow(wheel[2].matrix());
ruedaMesh.drawShadow(wheel[3].matrix());
autoMesh.drawShadow(car.matrix());
break;
}
}
void Draw()
{
D.clear();
Renderer(Render);
D.text(0,0.7,S+"Speed " + 3.6*car.vel());
//Physics.draw ();
}
/******************************************************************************/