Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Announcements and Showcase
fps gameplay

cyril
Registered User
Quote
2012-03-03 11:07:25

Hi !

I want to code in AS3 my own fps gameplay but I have some problems :

I understand Camera has no rotation (this is little strange...).
So, for looking around I use setCameraTarget().
I use a circle radius, for example :

cx = stage.width/2
cy = stage.height/2
targetx = cx - Math.sin(angle)*radius
targetz = cy + Math.cos(angle)*radius
simpleAPI.setCameraTarget(cam,targetx,targety,targetz)

It works fine but now I don't understand for the correct translations.
My code :

if (leftKey ) {camera_position.x++}
if (rightKey ) {camera_position.x--}
if (upKey ) {camera_position.z--}
if (downKey ) {camera_position.z++}
setSceneNodePosition(cam,camera_position.x,camera_position.y,camera_position.z)

OK it works but only if my target is in front of me. When I turn my look, the camera rotation is the same, so translations are false. (if I turn my look at 180° upKey/dowkKey are reversed)

My question : What about coding for good translations ?
My method is good ?

Thanks a lot,
Cyril


niko
Moderator
Quote
2012-03-03 14:54:00

It should work like this: Take the target point of the camera and substract it from the camera position, and you get the vector the camera looks into:
dx = cameratarget.X - camera_position.X;
dy = cameratarget.Y - camera_position.Y;
dz = cameratarget.Z - camera_position.Z;

To make sure the vector has the correct length, make it so long as how much you want to move, like this:
var lengthToMove:Number =2.0f;
var lengthOfVector:Number = Math.sqrt(dx*dx + dy*dy + dz*dz);
dx = (dx / lengthOfVector) * lengthToMove;
dy = (dy / lengthOfVector) * lengthToMove;
dz = (dz / lengthOfVector) * lengthToMove;

Now, you could move the position of the camera by that vector, and that's it:
camera_position.x += dx;
camera_position.y += dy;
camera_position.z += dz;

hope this helps!
Update: Ah, I see, that's only what you need to the up and down key. (down it's minus then). For left and right, it gets a bit more complicated in 3D, I guess. You need to use the cross product with the upvector or something similar then, to find the correct left and right direction.


cyril
Registered User
Quote
2012-03-03 21:40:55

Yes, Thank you very much, you help me.

But the camera target move too ? How ?
In fact, camera fps is a 3rd person camera with an unvisible target object, isn't it ?


niko
Moderator
Quote
2012-03-15 07:38:10

Yes, that's right. The target is in that case always locked to the object you are looking to. In case of a FPS camera, you need to move the target the same way as you move the camera position.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inter?ational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact