Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Bouncing Balls/ Vehicles

Tagart Franklin
Registered User
Join date: 2 Jun 2005
Posts: 4
06-07-2005 15:10
I'm wanting to make a ball to bounce off walls and vehicles.
So far I have a hovering ball that bounces, but moves very slowly.
I'd like the ball to get an impulse of speed each time it bounces off an object but I want the ball to bounce in a natural direction based on the angle it hits the object, movement of the object, speed etc.
If anyone could offer some thoughts, I'd be grateful.
My intention is to build a game that is a cross between bumpercars and soccer.
Thanks.
Tagart
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
06-08-2005 04:07
I feel like a dunce for not figuring this out sooner. Chris is going to kill me. :D

T'ain't perfect, but it approximates decently enough.

CODE
vector velocity = <0,0,0>; // Initialization
float rebound = 0.85; // Rebound strength

collide()
{
vector comparison = llGetVel();
if(llVecMag(comparison) > 1)
{
if(llVecMag(llVecNorm(comparison) - llVecNorm(velocity)) > 1)
llApplyImpulse(llVecNorm(comparison) * llVecMag(velocity) * rebound * llGetMass(),FALSE);
else
llApplyImpulse(-1 * llVecNorm(comparison) * llVecMag(velocity) * rebound * llGetMass(),FALSE);
}
}

default
{
state_entry()
{
llCollisionSound("",0.0);
llSetTimerEvent(0.2);
}

collision_start(integer total_number)
{
collide();
}
land_collision_start(vector pos)
{
collide();
}
timer()
{
velocity = llGetVel();
}
}


Edit: And a vehicle!

CODE
vector velocity = <0,0,0>; // Initialization
float rebound = 0.85; // Rebound strength
float impulse_const = 1; // Impulse speed
rotation rot = <0,0,0,1>; // Rotation const

collide()
{
vector comparison = llGetVel();
if(llVecMag(comparison) > 1)
{
if(llVecMag(llVecNorm(comparison) - llVecNorm(velocity)) > 1)
llApplyImpulse(llVecNorm(comparison) * llVecMag(velocity) * rebound * llGetMass(),FALSE);
else
llApplyImpulse(-1 * llVecNorm(comparison) * llVecMag(velocity) * rebound * llGetMass(),FALSE);
}
}

default
{
state_entry()
{
llSitTarget(<0,0,0.01>,<0,0,0,1>);
llSetCameraEyeOffset(<-10, 0, 5>);
llCollisionSound("",0.0);
}

collision_start(integer total_number)
{
collide();
}
land_collision_start(vector pos)
{
collide();
}

changed(integer change)
{
key agent = llAvatarOnSitTarget();
if(agent == llGetOwner())
{
llRequestPermissions(agent,PERMISSION_TAKE_CONTROLS);
llSetStatus(STATUS_PHYSICS,TRUE);
llSetTimerEvent(0.2);
}
else
{
llReleaseControls();
llUnSit(agent);
llSetStatus(STATUS_PHYSICS,FALSE);
llSetTimerEvent(0.0);
}
}

run_time_permissions(integer perm)
{
if(perm)
{
llTakeControls(CONTROL_UP | CONTROL_DOWN | CONTROL_LEFT | CONTROL_ROT_LEFT | CONTROL_RIGHT | CONTROL_ROT_RIGHT | CONTROL_FWD | CONTROL_BACK, TRUE, FALSE);
}
}

control(key id, integer level, integer edge)
{
if(level & CONTROL_UP)
{
llApplyImpulse(llGetMass() * <0,0,impulse_const>,TRUE);
}
if(level & CONTROL_DOWN)
{
llApplyImpulse(llGetMass() * <0,0,-1 * impulse_const>,TRUE);
}
if(level & CONTROL_FWD)
{
llApplyImpulse(llGetMass() * <impulse_const,0,0>,TRUE);
}
if(level & CONTROL_BACK)
{
llApplyImpulse(llGetMass() * <-1 * impulse_const,0,0>,TRUE);
}
if(level & (CONTROL_LEFT | CONTROL_ROT_LEFT))
{
rot = <0.00000, 0.00000, 0.08716, 0.99619> * rot;
}
if(level & (CONTROL_RIGHT | CONTROL_ROT_RIGHT))
{
rot = <0.00000, 0.00000, -0.08716, 0.99619> * rot;
}
}

timer()
{
llRotLookAt(rot,0.3,0.3);
velocity = llGetVel();
}
}
_____________________
---
Heart Wishbringer
The One & Only "Heart"
Join date: 22 Nov 2004
Posts: 284
hmm :)
06-08-2005 05:38
I need Jeffrey to help me.......... IM me Jeffrey :)
_____________________
Myspace: www.myspace.com/heartshinegirl

Documentary Series about Heart & Joe:
http://www.itv.com/Soaps/WebLives/SecondLifers/default.html

Our Story: Google Rhonda Lillie & Paul Hawkins

We appeared on TV, in Newspapers, and Magazines all around the UK. All because of a dream to be together in real life, after meeting in Second Life.
Tagart Franklin
Registered User
Join date: 2 Jun 2005
Posts: 4
Many thanks!
06-08-2005 08:03
This did exactly what I was hoping for.
Thanks Jeffery.

I added:
llSetBuoyancy(1.0);
llSetHoverHeight(2, 0, 1.0);

after state entry to get eliminate friction from the ground.
Now the ball moves continuously as I wanted.
Without setting a hover height, the vehicle didn't move for me, but now it's great.
Thanks,
Tagart
Tagart Franklin
Registered User
Join date: 2 Jun 2005
Posts: 4
adding breaks
06-09-2005 08:49
I found that in an enclosed arena, after a few bumps, the bumper car started moving too fast to handle, so I replace the "reverse gear" with brakes. It adds a lot of control to the vehicle. It's quite a bit of fun.
To add breaks, I replaced the back control of the vehicle with:

if(level & CONTROL_BACK)
{
llApplyImpulse(-llGetMass()*llGetVel() * .3,FALSE);
}
Enjoy,
Rick