Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

HELP: Raycasting & Position-Checking w/ Rotated Bounding Boxes

Rez Gray
Registered User
Join date: 2 Apr 2007
Posts: 23
06-13-2008 10:28
It's that time again. When people get fed up with the old, and want to make something new.
But there's always some scripting puzzle that stalls things.
Well, this time, we're still writing the spec for the new scaling combat engine we want to build, and I'm trying to cover a couple problems I see coming down the road.

Rotatable Bounding Box:
For use in plotting collision frames for fighter and capitol ship subsystems.
We need a fast function to construct a virtual bounding box using position, scale, and rotation.
We know approx. where the box is, we know how big it is (say, 30x20x10), and we know it's rotation.

Raycasting:
The bullet system will operate by regionsaying the shot path, ships within applicable range would run the 'ray' shot and compute if the ray strikes their primary hull, or subsystem using said bounding boxes.

Position-Checking:
We'll also need a way to see if a targeted object (say, a person, game asset) are within a bounding box as well.


If anyone has code that can help w/ this. I'm willing to pay for it.
If anyone thinks they can code this up, i'm willing to pay for it.
No, this isn't a 'product wanted'. Foo on you. :P
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-13-2008 10:53
This thread might give you a good starting point: /54/f4/258108/1.html

Follow up here or there if you have more questions.
Rez Gray
Registered User
Join date: 2 Apr 2007
Posts: 23
06-13-2008 11:24
Wow. Last time I looked a bounding boxes this hadn't been done yet.

I'm still trying to wrap my head around it.
Rez Gray
Registered User
Join date: 2 Apr 2007
Posts: 23
06-13-2008 12:04
Okay. I need to point out that I failed algebra... and geography.


I have a bounding box prim set up, w/ the functions tied to a listen.
I have a seperate object, the 'ray caster', i need to calc the ray based on where this sucker is pointing and see if it intersects the bounding box.

here's my attempt at a caster. I'm not sure what kind of input the direction really needs to I used a 'spot' trick from a sensor weapon I built once. (projects a virtual line)
CODE

default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number)
{
llSay(5678,"raycast|"+(string)llGetPos()+"|"+(string)(llGetPos() + (<900,0,0> * llGetRot())));
}
}


and here's the implementation of the bounding box
CODE

// BBox Setup //
vector bscale = <5,5,5>;
integer rayh; // listenhandle
integer rayc = 5678; // ray-cast channel

// Raycast Bounding Box Check, taken from LSLwiki - Returns -1 if no intersect.
//vector Ro Origin of Ray
//vector Rd Direction of Ray
//vector Bo Origin of Box
//vector Bs Size of Box
//rotation Br Rotation of Box
//float gBRxZ Returns distance to intersection of a ray and a b
float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br){
vector oB = (Ro-Bo)/Br; vector dB = Rd/Br; vector eB = 0.5*Bs;
float mD = -1.0; float D; vector X;

if(llFabs(dB.x) > 0.000001){
D = (-eB.x - oB.x ) / dB.x;if(D >= 0.0){X = oB + D * dB;if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z) mD = D;}
D = ( eB.x - oB.x ) / dB.x;if (D >= 0.0){X = oB + D * dB;if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D;}
}

if(llFabs(dB.y) > 0.000001){
D = (-eB.y - oB.y ) / dB.y;if(D >= 0.0){X = oB + D * dB;if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D;}
D = ( eB.y - oB.y ) / dB.y;if (D >= 0.0){X = oB + D * dB;if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D;}
}

if(llFabs(dB.z) > 0.000001){
D = (-eB.z - oB.z ) / dB.z;if(D >= 0.0){X = oB + D * dB;if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y) if (mD < 0.0 || mD > D) mD = D;}
D = (-eB.z - oB.z ) / dB.z;if (D >= 0.0){X = oB + D * dB;if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y) if (mD < 0.0 || mD > D) mD = D;}
}

return mD;
}


vector gBRxX( vector Ro, vector Rd, vector Bo, vector Bs, rotation Br){
float k = gBRxZ(Ro,Rd,Bo,Bs,Br);
if( k != -1.0 ) return Ro + Rd * k;
else return ZERO_VECTOR;}


default
{

state_entry()
{ llListenRemove(rayh); rayh = llListen(rayc,"","","");}

listen(integer ch, string name, key id, string msg)
{string m = llToLower(msg);
llOwnerSay(msg);
list ml = llParseString2List(m, ["|"], []);
string ml0 = llList2String(ml, 0);
string ml1 = llList2String(ml, 1);
string ml2 = llList2String(ml, 2);
string ml3 = llList2String(ml, 3);
string ml4 = llList2String(ml, 4);

if(ml0 == "raycast")
{ llOwnerSay((string)gBRxX((vector)ml1,(vector)ml2,llGetPos(),bscale,llGetRot()));
if(gBRxXZ((vector)ml1,(vector)ml2,llGetPos(),bscale,llGetRot()))
{llSetColor(<0,1,0>,-1);}else{llSetColor(<1,0,0>,-1);}
}
}


any ideas?
Nexii Malthus
[Cubitar]Mothership
Join date: 24 Apr 2006
Posts: 400
06-14-2008 03:43
Ray Direction has to be relative to Ray Position.

<1,0,0> would point forward in the X axis for example.

So what'd you would change in your caster code is the direction. Change that to <1,0,0> * llGetRot() instead of llGetPos()+<900,0,0>*llGetRot()
_____________________

Geometric Library, for all your 3D maths needs.
https://wiki.secondlife.com/wiki/Geometric

Creator of the Vertical Life Client
Rez Gray
Registered User
Join date: 2 Apr 2007
Posts: 23
06-14-2008 15:19
Thank you Nexii. After applying that fix the text rig works perfectly.

Now I need to figure out how to see if a single point is inside a bounding box.