Jump to content

Camera collision


ghoflvhxj
 Share

Recommended Posts

Hello guys.

I'm just tried to make 3rd person view.

and, i did it.

But there is a serious flaw.

It is the collisions of camera.;; I just want to make camera which have collisions.

Can you help me please? ㅠㅠ

Here is a picture and my code.

#include "engine.h"
int main(int argc, char** argv)
{
Initialize(1);

//Create a graphics context
Graphics(800,600);
SetAppTitle("Project A");
RegisterAbstractPath("C:\Program Files\Leadwerks Engine SDK");

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

CreateFramework();

//옵션
SetBloom(1);
SetHDR(1);
SetSSAO(1);
SetGodRays(1);
AFilter() ;
TFilter() ;

float move=0.0;
float strafe=0.0;
TVec3 camrotation=Vec3(0);
TVec3 playerrotation=Vec3(0);
float mx=0;
float my=0;
int sequence=3; //애니메이션 단계
float framebegin,frameend,frame; //애니메이션 시작,끝 프레임

//맵 로드
TEntity map = LoadScene("abstract::test.sbx");

//플레이어 생성, 충돌, 무게등등
TController player=CreateController(1.8,0.3,0.5,45.45);
PositionEntity(player,Vec3(-5,4,22));
EntityType(player,1);
SetBodyDamping(player,0.0);
SetWorldGravity(Vec3(0,-20,0));
SetBodyMass(player,1);

//중심 생성
TPivot pivot = CreatePivot(player);

//카메라 생성
TEntity cam= CreateCamera(pivot);
EntityType(cam,1,true);
CameraClearColor(cam,Vec4(255,255,255,255));
PositionEntity(cam,Vec3(0,2,-3));

//모델 로드,회전
TMesh test = LoadMesh("abstract::crawler.gmf");
RotateEntity(test,Vec3(0,180,0));

//마우스 위치
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
DebugPhysics(0);

//Main loop
while(!KeyHit(KEY_ESCAPE))
{
	//백터 값들.
	TVec3 testrotation = EntityRotation(test);
	TVec3 playerpos=EntityPosition(player);
	playerrotation=EntityRotation(player);
	//카메라 시선
	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;

	//플레이어 움직임
	move=(KeyDown(KEY_W)-KeyDown(KEY_S))*4.5;
	strafe=(KeyDown(KEY_D)-KeyDown(KEY_A))*4.5;

	//점프
	float jump=0.0;
	if (KeyHit(KEY_SPACE)) {
		if (!ControllerAirborne(player)) {
			jump=6.0;
		}
	}

	//카메라 x축 제한
	if(camrotation.X>23)
		camrotation.X=23;
	else if(camrotation.X<-32)
		camrotation.X=-32;

	//컨트롤러 업데이트
	UpdateController(player,camrotation.Y,move,strafe,jump,40);

	//모델 위치,회전
	PositionEntity(test,Vec3(playerpos.X,playerpos.Y,playerpos.Z));
	RotateEntity(test,Vec3(testrotation.X,testrotation.Y,testrotation.Z));

	//모델 애니메이션
	if(KeyDown(KEY_W) || KeyDown(KEY_D) || KeyDown(KEY_A) || KeyDown(KEY_S) || KeyDown(KEY_SPACE) || ControllerAirborne(player))
	{
		RotateEntity(test,Vec3(playerrotation.X,camrotation.Y+180,playerrotation.Z));
	}
	if(KeyDown(KEY_W)||KeyDown(KEY_S)||KeyDown(KEY_A)||KeyDown(KEY_D))
	{
		sequence=1;
	}
	else
	{
		sequence=3;
	}

	switch(sequence)
	{
		case 1:
		 framebegin=131,frameend=171;
		break;
		case 2:
		 framebegin=70,frameend=130;
		break;
		case 3:
		 framebegin=0,frameend=68;
		break;
	}
	frame=AppTime()/29.0;
	frame=fmodf(frame,frameend-framebegin)+framebegin;
	Animate(test,frame,1.0,0,true);

	//와이어프레임
	if(KeyDown(KEY_Q))
	{
		DebugPhysics(1);
	}

	UpdateFramework();

	RotateEntity(pivot,camrotation,1);

	RenderFramework();

	SetBlend(BLEND_ALPHA);

	SetBlend(BLEND_NONE);

	Flip(0);
}
exitapp:
return Terminate();
}

a.JPG?type=w3

Link to comment
Share on other sites

I have a camz variable that is controlled by the MouseZ function. It affects how far from the PlayerController the camera is.

 

camz = MouseZ();
PositionEntity (camera,Vec3(0,0,camz);

 

To initiate camera collisions, I position a second pivot about 0.2m from the pivot used to rotate the camera. This means that my raycast won't be affected by the mesh.

 

pivot2 = CreatePivot(pivot);
PositionEntity(pivot2,Vec3(0.0f,0.0f,0.2f));
       PointEntity(pivot2,camera)

 

Then I do a EntityPick command from that second pivot to the camera. If there is a collition, I put a third pivot at the point of the collision. If camz is less than the distance to that 3rd pivot, I ignore the collision. Otherwise, the camz variable is overridden by the collision.

 

    if (EntityPick(&raycast,pivot2,10.0f,1)) {
       PositionEntity(pivot3,Vec3(raycast.X,raycast.Y,raycast.Z));
       PositionEntity(camera, Vec3(0,0,Min(camz, EntityDistance(controller,pivot3)-1.0f)));
   }
   else {
       PositionEntity(camera, Vec3(0,0,camz));
   }

Link to comment
Share on other sites

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

CreateFramework();//Include up to the world
//TEntity cam= CreateCamera(pivot);
//Can so
TEntity cam =GetLayerCamera(GetFrameworkLayer(0));
EntityParent(cam,pivot);

AMD3600+/2GB DDR3 SDRAM / GeForce 8600 GTS

I3 530/2GB DDR3/GF GT240 DDR5

Link to comment
Share on other sites

Basically instead of giving your camera collisions you are effectively giving your camera "sensors" instead.

 

Using 'ray casts' or 'entity picks' you can fire out sensors to check if there is an object between the player and the camera. If the ray cast or pick hits something then you have move the camera closer by a distance. You have to calculate this distance from values you obtain from the raycast.

 

Neway thats the jist of it though its a bit more complex than that because you have to check to a point behind the camera or else ur camera will cull a wall or something.

STS - Scarlet Thread Studios

AKA: Engineer Ken

 

Fact: Game Development is hard... very bloody hard.. If you are not prepared to accept that.. Please give up now!

Link to comment
Share on other sites

Just a moment ago, i tested.

However, Unfortunately, the result failed...

What is the flaw in my code?

..........
TPivot pivot = CreatePivot(player);
TPivot pivot2;
TPivot pivot3= CreatePivot();
TPick raycast;
float camz;
..........
while(!KeyHit(KEY_ESCAPE))
{
               .....
               camz=MouseZ();
	PositionEntity(cam,Vec3(0,2,camz));

               pivot2 = CreatePivot(pivot);
	PositionEntity(pivot2,Vec3(0.0f,0.0f,0.2f));
	PointEntity(pivot2,cam);


	if (EntityPick(&raycast,pivot2,10.0f,1)) 
	{
		PositionEntity(pivot3,Vec3(raycast.X,raycast.Y,raycast.Z));
		PositionEntity(cam, Vec3(0,0,Min(camz, EntityDistance(player,pivot3)-1.0f)));
	}
	else 
	{
		PositionEntity(cam, Vec3(0,0,camz));
	}
               UpdateController(player,camrotation.Y,move,strafe,jump,40);
               .....

Link to comment
Share on other sites

If an entity is another entity child objects

Please use:

TFormPoint(point,src,dst)

Conversion position

 

http://www.leadwerks.com/werkspace/files/file/273-lupins-le2-3d-game-engine-starter-kit-for-delphi-and-freepascal/

delphi2C++:

#include "engine.h"

float AngleDiff(float angle1, float angle2)
{
angle1 =fmodf(angle1,360);
if (angle1<0)
	angle1=angle1+360;

angle2 =fmodf(angle2,360);
if (angle2<0)
	angle2=angle2+360;

if (angle1 > angle2)
{
	if(angle1 - angle2 < 360 + angle2 - angle1)
		return (angle1 - angle2) * -1;
	else
		return 360 + angle2 - angle1;
}
if (angle2 > angle1)
{
	if (angle2 - angle1 < 360 + angle1 - angle2)
		return angle2 - angle1;
	else
		return (360 + angle1 - angle2) * -1;
}
return 0;
}
int main(int argc, char** argv)
{
TPick *pick =new TPick;
float move = 0;
float strafe = 0;
float jump = 0;
TVec3 playerpos;
TVec3 meshpos;
TVec3 campos;
int gx = 0;
int gy = 0;
int mz = 0;
float dx = 0;
float dy = 0;
float camerapitch = 0;
float camerayaw = 0;
float dist = 0;

float framebegin = 0;
float frameend = 0;
float frame = 0;

float rd;
float rc;

Initialize();
RegisterAbstractPath("F:\\3D\\leadwerks v2.4");
	Graphics(800,600);

CreateFramework();

TEntity scene = LoadScene("abstract::train.sbx");
TMesh crawler = LoadMesh("abstract::crawler.gmf");

TEntity camera =GetLayerCamera(GetFrameworkLayer(0));
PositionEntity(camera,EntityPosition(crawler));

AmbientLight(Vec3(1.0f, 1.0f, 1.0f));

TController player = CreateController();
EntityType(player,3);
EntityType(player,1);
SetBodyDamping(player,0.0);
SetWorldGravity(Vec3(0,-20,0));
SetBodyMass(player,1);

Collisions(2,3,1);

gx = GraphicsWidth() / 2;
gy = GraphicsHeight() / 2;

move = 0.0f;
strafe = 0.0f;
camerapitch = 0.0f;
camerayaw = 0.0f;
dist = 2.0f;

HideMouse();
MoveMouse(gx, gy);

mz = MouseZ();
// Main loop
while (!KeyHit(KEY_ESCAPE))
{
    DebugPhysics(KeyDown(KEY_Q));

    if (MouseZ() != mz)
    {
        if (mz < MouseZ())
        {
			dist = dist - ((MouseZ() - mz) / 5.0f);
			if (dist < 1)
				 dist = 1;
        }
        else
        {
			dist = dist + ((mz - MouseZ()) / 5.0f);
			if (dist > 5)
				dist = 5;
        }
        mz = MouseZ();
    }
    dx = Curve((MouseX() - gx) / 5.0f, dx, 5.0f / AppSpeed());
    dy = Curve((MouseY() - gy) / 5.0f, dy, 5.0f / AppSpeed());
    MoveMouse(gx, gy);
    camerapitch = camerapitch + dy;
    camerayaw = camerayaw - dx;
    Min(camerapitch, 90.0f);
	Max(camerapitch,  -90.0f);
    RotateEntity(camera,Vec3(camerapitch, camerayaw, 0.0f), true);

    move = (KeyDown(KEY_S) || MouseDown(MOUSE_MIDDLE)) - (KeyDown(KEY_W) || MouseDown(MOUSE_LEFT));
    strafe = KeyDown(KEY_A) - KeyDown(KEY_D);

    if ((move != 0) || (strafe != 0))
    {
		rd = fmodf( EntityRotation(crawler).Y, 360);
        rc = fmodf(EntityRotation(camera).Y + 180, 360);
		if (abs(fmodf(rd - rc,360)) <= 12.0f)
			RotateEntity(crawler,Vec3(0.0f, rc, 0.0f));
        else
        {
			if (AngleDiff(rc, rd) < 0)
				TurnEntity( crawler,Vec3(0.0f, 6.0f * AppSpeed(), 0.0f));
			else
				TurnEntity(crawler,Vec3(0.0f,  -6.0f * AppSpeed(), 0.0f));

			if (move != 0.0f)
				move = move / 5.0f;
			if (strafe != 0)
				strafe = strafe / 5.0f;
        }
    }
    jump = 0.0f;
    if ((move != 0))
    {
        framebegin=131,frameend=171;
    }
    else if ((strafe != 0))
    {
        framebegin=70,frameend=130;
    }
    else
    {
        framebegin=0,frameend=68;
    }
    frame =AppTime()/29.0f;
       frame =fmodf(frame,frameend-framebegin)+framebegin;
       Animate(crawler,frame,1.0,0,true);
    UpdateController(player,EntityRotation (crawler).Y, move * 3.0f, strafe * 2.0f, jump, 40.0f);

    playerpos = EntityPosition(player);
    meshpos = EntityPosition(crawler);
    meshpos.Y = Curve(playerpos.Y, meshpos.Y, 2.0f);
    meshpos = Vec3(playerpos.X, meshpos.Y, playerpos.Z);
    PositionEntity(crawler,meshpos);

    campos = TFormPoint(Vec3(0, 1, 0), crawler,0);
	PositionEntity (camera,campos);
	MoveEntity(camera,Vec3(0, 0,  -dist));

	//HideEntity(player);
	HideEntity(crawler);
	if (LinePick(pick,campos, EntityPosition(camera), 0.5f))
        PositionEntity(camera,Vec3(pick->X,pick->Y,pick->Z));
    MoveEntity(camera,Vec3(0.0f, 0.0f, 0.2f));
	// ShowEntity(player);
    ShowEntity(crawler);
	UpdateFramework();
	RenderFramework();
    Flip();
}
return Terminate();
}

AMD3600+/2GB DDR3 SDRAM / GeForce 8600 GTS

I3 530/2GB DDR3/GF GT240 DDR5

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...