How do I make a camera rotate with an entity as it's center? :)

Posted By: Max_Prower

How do I make a camera rotate with an entity as it's center? :) - 03/16/09 21:42

What I mean is. I've got my basic movement, gravity and everything sorted out. Now, what I would like to create is a camera.

The camera that I am trying to create is basically a basic third-person shooter camera. One that keeps the player entity as it's center and turns with it when I move the mouse. (I made it so that the character's pan changes with mouse_force.x)

So basically, how can I make my camera center around a model?

Also, how can I make it so that the camera doesn't go through walls? Say, if I went around a corner by moving sideways, that when the camera lost sight of the player; it would zoom to the player's view again?

I apologise if there is a blatantly obvious solution; but I did a brief bit of searching, and I couldn't find anything.
Posted By: Jaxas

Re: How do I make a camera rotate with an entity as it's center? :) - 03/17/09 17:27

You can find code in forum, but what the heck, here's the basic that you'll need:

camera.x = player.x-distance* cos(camera.pan);
camera.y = player.y-distance* sin(camera.pan);

Experiment with other camera values your self wink
Posted By: Sonne

Re: How do I make a camera rotate with an entity as it's center? :) - 03/17/09 17:43

camera.x = me.x + fcos(my.pan,-1 * distance);
camera.y = me.y + fsin(my.pan,-1 * distance);
vec_diff(temp.x,me.x,camera.x);
vec_to_angle(camera.pan,temp.x);

I found that in one of the tutorials on the wiki. Works for me. I too have to resolve the camera going into walls which I believe was also addressed in those tutorials but I haven't looked into that portion yet.
Wiki Tutorials
Posted By: Max_Prower

Re: How do I make a camera rotate with an entity as it's center? :) - 03/18/09 12:46

Trig... My mortal enemy. Thanks for the help, you guys! ^^

One thing though, I'm curious. Where (school here doesn't teach it for some reason) can I learn trigonometry? And how does Trigonometry apply in Gamestudio?
Posted By: DJBMASTER

Re: How do I make a camera rotate with an entity as it's center? :) - 03/18/09 13:23

Trigonometry and vector/matrix algebra are the principles that 3D graphics are built up off. Without an understanding of them its pretty hard to fully appreciate what is going on. If you're not that comfortable with maths or just have gaps in your knowledge then i recommend a maths book.

I have used this book and it helps alot...

3D maths primer for 3D Graphics/Games.

http://www.amazon.com/Primer-Graphics-Development-Wordware-Library/dp/1556229119
Posted By: Max_Prower

Re: How do I make a camera rotate with an entity as it's center? :) - 03/18/09 21:34

I'm not an adult and don't have my own money to be able to spend on the net. Are there any free resources? smile

And I manged to find a solution. I used the camera script from David Lancaster's Kindom Hearts tutorial. Here is what I used (I tinkered with the values a little).

Code:
vec_set(camera.x,vector(my.x + fcos(my.pan,-466),my.y + fsin(my.pan,-466),my.z +
160)); 
vec_diff(temp.x,my.x,camera.x); //make the camera look towards the player
vec_to_angle(camera.pan,temp.x);//place the camera behind the player
}

© 2023 lite-C Forums