Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

bullet to shoot with proper angle?

Jolan Nolan
wannabe
Join date: 12 Feb 2006
Posts: 243
07-14-2006 10:56
I looked over the posts here and need a more direct answer I guess.
I have a small turret that aims up and down. My question is How do I get the bullet to fire at the same anle as the barrel? I mean I set the barrel to 0,0,0 and the llRezObject to 0,0,0 and it fires straight out at angle zero. But after I rotate it to say 45 degrees, it still fires straight out at angle zero. (I understand it's because of the set values but...)

I know I need llGetRot in there somehow but I'm not sure how to use it to set that angle.

Still learning
- Jolan
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-14-2006 11:17
/54/b2/120428/1.html

same principle. To actually fire bullets at correct angle from the turret you can change the rezzing line to:
CODE

vector velocity = llRot2Fwd( angle ) * 2.5; // 2.5 is speed multiplier, in this case makes it 2.5 m/sec
llRezObject( "bullet", position + (offset * angle), velocity, angle, 0 );
Jolan Nolan
wannabe
Join date: 12 Feb 2006
Posts: 243
07-14-2006 11:23
Well, I still don't understand the math part of it but this'll do for now, thanks.
What I need is an SL Script for Dummies :P.

- Jolan
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-14-2006 11:36
Math part is optional, really ^^

it's much easier to understand when you play with Build panel a bit, basically boils down to:

vector * rotation = your translation vector is now aligned with "local" Ruler Mode rather than "world" mode
vector / rotation = the opposite iirc, angle the object is at is cancelled out, and your translation vector is now in "world" Ruler Mode

the llRot2Fwd() and such functions allow you to retrieve direction of red, green and blue arrow in "local" Ruler Mode, as vectors... these vectors will be always 1 m long.

from this you just mix and match to get desired effect ^^;
Jolan Nolan
wannabe
Join date: 12 Feb 2006
Posts: 243
07-14-2006 11:46
Well I'm almost there... That part that says ZERO_VECTOR for the velocity actually has to have a value. The pellet (bullet) is being lobbed instead of fired so if I leave it at ZERO it still just drops and if I set it to a specific value, it only fires in that direction, which is the problem I had it at before.
Here's my script right now, including my attempt to guess it:
=============================
touch_start(integer total_number)
{
vector position = llGetPos(); // location of root object
rotation angle = llGetRot(); // rotation of root object
vector offset = <0, -0.7, 0>; // location of new cube relative to the root object
vector power = <0.0, -10.0, 0.0>; //Power setting

llRezObject( "_pellet", position + (offset * angle), power, angle, 42 );
}
}
=============================
I would like to be able to change the vector power before firing. I guess the angle value isn't needed since the pellet is round and is being tossed like a ball?

- Jolan
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-14-2006 11:53
It's included in that snippet i posted in first reply ^^

vector velocity = llRot2Fwd( angle ) * 2.5;

this retrieves the direction where the barrel is aiming. Because this vector is 1 m long (meaning your object would move with initial velocity of 1 m/sec if using it) ... it's then multiplied by 'power' of shot. 2.5 results in 2.5 m/sec, 10 would result in 10 m/sec etc.

Then the velocity is simply entered in the rez command instead of zero_vector:

llRezObject( "bullet", position + (offset * angle), velocity, angle, 0 );

so full code would be someething like:
CODE

fire_pellet( float Power) {

vector position = llGetPos(); // location of root object
rotation angle = llGetRot(); // rotation of root object
vector offset = <0, -0.7, 0>; // location of new cube relative to the root object
vector velocity = llRot2Fwd( angle ) * Power;

llRezObject( "_pellet", position + (offset * angle), velocity, angle, 42 );
}

... then you can invoke it with say, fire_pellet( 10.0 ) or fire_pellet( 0.5 ) ... depending how much power you want the bullet to have.

edit: and i guess for round bullets the correct angle isn't indeed necessary, but it's probably good to have it there in case you ever want to fire some more modern ammo shells ^^;;
Jolan Nolan
wannabe
Join date: 12 Feb 2006
Posts: 243
07-14-2006 12:06
So then 'angle' aims the shot, 'llrot2fwd' tells the shot which direction it will go and then pushes it off at a speed of 'power'?

Why do you put 'float power' up at the title of the section instead of naming it with the angle and others? Do I still put that under touch_start?

And also would it matter to put all this at the top of the page before DEFAULT as I've seen in vehicles and other stuff - Does putting it all down there recall it on command?

These should clairify it good for me.

- Jolan
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-14-2006 12:20
From: Jolan Nolan
So then 'angle' aims the shot, 'llrot2fwd' tells the shot which direction it will go and then pushes it off at a speed of 'power'?

Yes, that's pretty much it ^^ 'angle' is current rotation of the barrel item and it's used to correctly rotate the bullet object, to place the rezzed bullet in the right spot in relation to the barrel, and finally to determine in which direction the bullet should fly. You can think of it as of a bit more fancy vector with extra data that makes it more robust when you spin it around and whatnot.
From: someone
Why do you put 'float power' up at the title of the section instead of naming it with the angle and others? Do I still put that under touch_start?

This is called function, and it allows you to reuse the same bit of code with varying parameters that determine final effect... you can read about functions here:

http://secondlife.com/badgeo/wakka.php?wakka=UserDefinedFunction

to use it, you could invoke it in the touch_start event, yes:
CODE

fire_pellet( float Power) {
// copy and paste the function code from previus reply here
}

default {

touch_start( integer ContactsTotal ) {

fire_pellet( 10.0 );
}
}

note though, this particular approach uses hard-coded value for power (power is always 10.0) ... you could probably turn it into some sort of "power" variable and then allow user to modify its value either through menu or spoken commands... then use fire_pellet( power ) to fire actual shot with such finetuned power value.
Jolan Nolan
wannabe
Join date: 12 Feb 2006
Posts: 243
07-14-2006 12:30
Ah Geeze, now what the heck is 'ContactsTotal' :P
Well, it's working good so far so my basic questions are solved :D ).

Thanks ALOT for putting up with me and also replying so quickly!
One last guessing question before I go.
If I use the code from your previous post, it would probably work with a control panel (with that prim messaging stuff) used to set power, and then click the turret when ready to fire...? That sounds right. :)

*EDIT*Hmm.. I think I've got it. Set float Power = 1 above 'default' and then in the touch_start, I could put that message reciever (maybe?) to get the new power setting for the shot...

- Jolan
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
07-14-2006 13:29
From: Jolan Nolan
Ah Geeze, now what the heck is 'ContactsTotal' :P
Well, it's working good so far so my basic questions are solved :D ).

Ahh sorry, that's the default parameter that's supplied when touch_start event is triggered... it tells you how many people are touching your object, but it's normally ignored.

http://secondlife.com/badgeo/wakka.php?wakka=touch_start

From: someone
If I use the code from your previous post, it would probably work with a control panel (with that prim messaging stuff) used to set power, and then click the turret when ready to fire...? That sounds right. :)

You could use link_message event or something similar to adjust parameters and trigger the launch, aye. ^^;