Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Need Camera Help

Feynt Mistral
Registered User
Join date: 24 Sep 2005
Posts: 551
03-26-2006 01:28
I've been working on a sort of Resident Evil style camera attachment for the past few days, here's what I've got so far:

CODE

vector cameraPos;
float maxCameraDist = 15;
integer cameraOn = TRUE;
integer scanning = FALSE;
integer noWalls = FALSE;

cameraToggle(key agent) {
if (cameraOn) {
release_camera_control(agent);
llSetTimerEvent(0);
llSensorRemove();
} else {
take_camera_control(agent);
llSetTimerEvent(1);
}
}

take_camera_control(key agent) {
llOwnerSay("take_camera_control"); // say function name for debugging
llOwnerSay( (string)agent);
llRequestPermissions(agent, PERMISSION_CONTROL_CAMERA);
}

release_camera_control(key agent) {
llOwnerSay("release_camera_control"); // say function name for debugging
llSetCameraParams([CAMERA_ACTIVE, 0]); // 1 is active, 0 is inactive
llReleaseCamera(agent);
cameraOn = FALSE;
}

positionCamera() {
if (cameraOn) {
llSetCameraParams([
CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
CAMERA_BEHINDNESS_ANGLE, 0.0, // (0 to 180) degrees
CAMERA_BEHINDNESS_LAG, 0.0, // (0 to 3) seconds
CAMERA_DISTANCE, 0.0, // ( 0.5 to 10) meters
CAMERA_FOCUS, cameraPos, // region relative position
CAMERA_FOCUS_LAG, 0.0 , // (0 to 3) seconds
CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
// CAMERA_PITCH, 80.0, // (-45 to 80) degrees
CAMERA_POSITION, cameraPos, // region relative position
CAMERA_POSITION_LAG, 0.0, // (0 to 3) seconds
CAMERA_POSITION_LOCKED, TRUE, // (TRUE or FALSE)
CAMERA_POSITION_THRESHOLD, 0.0, // (0 to 4) meters
CAMERA_FOCUS_OFFSET, ZERO_VECTOR // <-10,-10,-10> to <10,10,10> meters
]);
llSetTimerEvent(1);
}
}

default {
state_entry () {
llSensor("", NULL_KEY, PASSIVE | ACTIVE | SCRIPTED, maxCameraDist / 1.5, PI);
cameraToggle(llGetOwner());
}

timer() {
if (llVecDist(llGetPos(), cameraPos) > maxCameraDist && !scanning) {
// llOwnerSay("Distance from camera: " + (string)llVecDist(llGetPos(), cameraPos));
llSensorRepeat("", NULL_KEY, PASSIVE | ACTIVE | SCRIPTED, maxCameraDist / 1.5, PI, 0.5);
scanning = TRUE;
llSetTimerEvent(0);
}
}

sensor(integer num) {
scanning = FALSE;

vector myPos = llGetPos();
// llOwnerSay("myPos: " + (string)myPos);
integer p;
for (p = 0; p < num; p++) {
vector scannedPos = llDetectedPos(p);
// llOwnerSay("scannedPos(" + (string)p + "): " + (string)scannedPos);
if (llVecDist(scannedPos, myPos) < maxCameraDist && llVecDist(scannedPos, myPos) < llVecDist(cameraPos, myPos)) {
if (scannedPos.z > myPos.z+1) {
cameraPos = scannedPos;
}
}
}
// llOwnerSay("cameraPos: " + (string)cameraPos);
if (cameraPos == ZERO_VECTOR) {
cameraPos = myPos + <4,4,4>;
}
llSensorRemove();
vector rotPos = cameraPos + <2, 0, 0>;
rotation lookAtAv = llRotBetween(<1,0,0>, llVecNorm(llRot2Axis(llGetRot())));
vector newPos = cameraPos - ((cameraPos - rotPos) * lookAtAv);
positionCamera();
}

touch_start(integer total_number) {
cameraToggle(llDetectedKey(0));
}

run_time_permissions(integer perm) {
if ((perm & PERMISSION_CONTROL_CAMERA) == PERMISSION_CONTROL_CAMERA) {
llOwnerSay("Camera permissions have been taken");
cameraOn = TRUE;
positionCamera();
}
}
}


To decypher it, the camera scans the area around the avatar for a prim higher than the avatar and locks the camera there. It tracks the avatar's movement, but won't move from that spot. When the avatar gets too far away, it scans for another prim.

My problems are two fold:
  1. When you're farther away than the maxCameraDist and no other suitable prims are in sight, it SHOULD put your camera at llGetPos() + <4,4,4> until it finds something suitable, but doesn't.
  2. It's suppose to put the camera 2m out from the center of the prim it scanned, towards the avatar, but it doesn't.


I was going to use this as a camera example in a TeaZers class so it's essentially open source, but I'd like some help getting it working properly.
_____________________
I dream of a better tomorrow in SL!
You should too. Visit, vote, voice opinions.
Support CSG! Tell LL how much it would mean to subtract one prim from another!
Prim Animation! Stop by and say something about it, show your support!
Azurei Ash
The Sticky!
Join date: 12 Jan 2006
Posts: 38
03-29-2006 03:56
1. When you're farther away than the maxCameraDist and no other suitable prims are in sight, it SHOULD put your camera at llGetPos() + <4,4,4> until it finds something suitable, but doesn't.

CODE

if (cameraPos == ZERO_VECTOR) {
cameraPos = myPos + <4,4,4>;
}


I don't see any reason why cameraPos would be the ZERO_VECTOR at that point, cameraPos would still be what it was last set to.

Also remember that sometimes the sensor event will not even fire, you may get the no_sensor event instead.

2. It's suppose to put the camera 2m out from the center of the prim it scanned, towards the avatar, but it doesn't.

CODE

vector rotPos = cameraPos + <2, 0, 0>;
rotation lookAtAv = llRotBetween(<1,0,0>, llVecNorm(llRot2Axis(llGetRot())));
vector newPos = cameraPos - ((cameraPos - rotPos) * lookAtAv);
positionCamera();


Here it looks like you are calculating a new camera position, but then you never use that new position.

Also, say you have a vector A and a vector B. Taking B-A will give you a vector from A to B (since A plus B-A equals B). Normalize that vector (the one from A to B, use llVecNorm) and it will make it a unit vector. So then if you add that unit vector to A, it will give you a vector 1 meter toward B, from A. So if you want 2m toward B, add 2 times the unit vector.

For you, B would be your position and A would be the camera position.

Just trying to point you in the right direction, don't think I could actually correct your script without pulling my hair out or just rewriting it from scratch. XD

~Azurei Ash
Kyrah Abattoir
cruelty delight
Join date: 4 Jun 2004
Posts: 2,786
03-29-2006 04:27
check my last script, might be an easier way to do it:

/15/e6/96677/1.html
_____________________

tired of XStreetSL? try those!
apez http://tinyurl.com/yfm9d5b
metalife http://tinyurl.com/yzm3yvw
metaverse exchange http://tinyurl.com/yzh7j4a
slapt http://tinyurl.com/yfqah9u