Brainache
Member
|
Camera Collision
Hey there,
Is anything inplace for handling camera collision when using the setSpherical method for creating a 3rd person camera?
Or is this something that would need to be done usint hit testing,etc?
|
|
12-22-2008 04:37 PM |
|
Esenthel
Administrator
|
Re: Camera Collision
yes this should be handled manually (it's pretty easy though)
I encourage trying to set a Ball above players head, then use Physics.move method to move the ball to a target location where the camera should be, 'move' method will move the camera as far as it can go.
if you'll have problems with it ill make a tutorial
|
|
12-22-2008 04:54 PM |
|
Brainache
Member
|
Re: Camera Collision
Makes perfect sense... I'll work on the approach.. thanks for the advice.
|
|
12-22-2008 05:36 PM |
|
Kaldrick
Member
|
Re: Camera Collision
Yet, tutorial would be great. ;P
|
|
12-22-2008 06:00 PM |
|
Esenthel
Administrator
|
Re: Camera Collision
ok ill make one
|
|
12-22-2008 06:02 PM |
|
Brainache
Member
|
Re: Camera Collision
Well.. I've taken a stab at this, and I feel like I'm close.. but I'm not quite on it...
Here is the code I have:
Code:
Vec dir;
SinCos(dir.z,dir.x,Players[0].angle.x+PI_2); // set direction according to player angle
dir.normalize();
Vec start_pos = Players[0].ctrl.actor.pos()+Vec(0,0.7,0);
Vec end_pos = start_pos + (dir * 10);
ball.pos = start_pos;
//ball2.pos(start_pos + (dir * 2) );
Physics.move(ball, end_pos);
Cam.setSpherical(Players[0].ctrl.actor.pos()+Vec(0,0.7,0),Players[0].angle.x,Players[0].angle.y,0,Cam.dist*ScaleMul(Ms.wheel*-0.2)).updateVelocities().set(); // update camera according to player angles and mouse wheel
( note this isnt setting the camera - im just trying to get the ball collision to work)
Question 1 - The SinCos function (taken from one of the turorials) is only basing the dir vector from the left-right angle, not up-down.. how can I change that?
2 - The ball is not showing at all ( using Physics.draw() )... If I replace using a Ball with using an Actor created from a Ball - it renders in the correct place.. but then I have to use Physics.move(ball2.box(), end_pos).... and no collision occurs...
Thanks for the help!
|
|
12-22-2008 06:12 PM |
|
Esenthel
Administrator
|
Re: Camera Collision
Physics.move(ball, end_pos); doesn't operate on "start", "end" parameters, but "start", "move" (move = end-start, delta, difference)
ok I'll create a tutorial about it
|
|
12-22-2008 06:23 PM |
|
Esenthel
Administrator
|
Re: Camera Collision
Ok,
here's the tutorial you wished for, it will be also available in the next SDK
Code:
/******************************************************************************/
#include "stdafx.h"
#include "../../../../../data/enum/_enums.h"
/******************************************************************************
Here we'll present how to detect camera collisions
/******************************************************************************/
struct Player : Game::Chr
{
virtual Bool update();
};
/******************************************************************************/
Camera desired_camera; // create a helper desired camera
/******************************************************************************/
Game::ObjMemx<Game::Static> Statics;
Game::ObjMemx<Game::Item > Items ;
Game::ObjMemx< Player> Players;
/******************************************************************************/
Bool Player::update()
{
if(action)
{
if(Kb.b(KB_W) || Kb.b(KB_S) || Kb.b(KB_A) || Kb.b(KB_D) || Kb.b(KB_Q) || Kb.b(KB_E))actionBreak();
}
if(!action)
{
// turn & move
input.anglei.x=Kb.b(KB_Q)-Kb.b(KB_E);
input.anglei.y=Kb.b(KB_T)-Kb.b(KB_G);
input.diri .x=Kb.b(KB_D)-Kb.b(KB_A);
input.diri .z=Kb.b(KB_W)-Kb.b(KB_S);
input.diri .y=Kb.b(KB_SPACE)-Kb.b(KB_LSHIFT);
// dodge, crouch, walk, jump
input.dodge = Kb.bd(KB_D)-Kb.bd(KB_A);
input.crouch= Kb.b (KB_LSHIFT);
input.walk = Kb.b (KB_LCTRL );
input.jump =(Kb.bp(KB_SPACE ) ? 3.5 : 0);
// ready stance change
ready^=Kb.bp(KB_R);
}
return __super::update();
}
/******************************************************************************/
void InitPre()
{
App.name="Camera Collisions";
App.flag=APP_MS_EXCLUSIVE|APP_FULL_TOGGLE;
IOPath="../data/";
PakAdd("engine.pak");
D.full(true).sync(true).shdMapSize(1024).ambPower(0.3).hpRt(true);
}
/******************************************************************************/
Bool Init()
{
Physics.create();
Sky .set ();
Sun .set (*Gfxs("gfx/sky/sun.gfx")).power=1-D.ambPower();
Sun.rays.mode=SUN_RAYS_HIGH;
// create the world
Game::World.init()
.setType(Statics,OBJ_STATIC)
.setType(Players,OBJ_CHR ) // please note that here we'll use 'Players' memory container for 'OBJ_CHR' objects
.setItem(Items ,OBJ_ITEM ) // please note that here is called 'setItem' instead of 'setType', this is used for enabling built-in character<->item relations such as picking up and dropping items
.New("world/sample" );
return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Main()
{
if(Kb.bp(KB_ESC))return false;
Game::World.update(Cam.at);
// setup the camera
if(Players.elms()) // if we have at least one player
{
// first setup the desired camera as in the previous tutorials
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*ScaleMul(Ms.wheel*-0.1)); // update camera distance according to mouse wheel
desired_camera.at =Players[0].pos();
desired_camera.setSpherical(); // set as spherical from current values, this will set the cameras 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)
// nowe we'll move the ball in the direction where the camera should be
Physics.move(ball, desired_camera.matrix.pos-ball.pos); // use physics move 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
}
else // when no player on the scene
{
CamHandle(0.1,100,CAMH_ZOOM|(Ms.b(1)?CAMH_MOVE:CAMH_ROT)); // default camera handling actions
}
return true;
}
/******************************************************************************/
void Render()
{
Game::World.draw();
}
void Draw()
{
Renderer(Render);
D.text(0,0.9,"Try to move the camera around");
D.text(0,0.8,"and see how it gets blocked by the terrain");
}
/******************************************************************************/
|
|
12-22-2008 07:57 PM |
|
Brainache
Member
|
Re: Camera Collision
Perfect! Thats exactly what I was looking for ( and others it seams!)
Thanks for the great support!
|
|
12-22-2008 08:08 PM |
|
lucifer1101
Member
|
Re: Camera Collision
Thank you heaps for that Esenthel, im new to this but to get that to work you will need to change where the enums is located right, or did you just have it in a specific folder, if so which one?
|
|
12-22-2008 11:25 PM |
|
Esenthel
Administrator
|
Re: Camera Collision
Hi, no problem
this tutorial should be located where the other game tutorials are located in "game basics" folder, there it should compile ok
|
|
12-22-2008 11:27 PM |
|
lucifer1101
Member
|
Re: Camera Collision
thankyou for your reply..
I had already edited the path for where i put it, but htaught that others should know...
|
|
12-23-2008 12:13 AM |
|