Posted By: CBSection31
Physics code problem - 08/31/06 18:55
Hi everyone,
I'm trying to create a ball that the player can pick up and either throw or release. I'm using physics for the ball except for when the player picks it up. The code works properly if the ball is at a complete stop. But if the ball is rolling or has bounced off a wall, then when the player tries to pick it up, the ball goes flying in some random direction.
I'm new to the physics engine, so I apologize if this code looks really bad. The problem may be something really simple that I'm missing. I'm hoping that you can help me fix this code so that it will work regardless of the state of the ball.
What should happen is this: If the player is near the ball, physics are on and the ball can be rolled around on the ground. If the player right clicks on the ball, he "picks it up", physics turn off and the ball is always centered on the screen, no matter where the player walks. If the player then right clicks again, physics are reactivated and the ball drops. Instead, if the player left clicks, physics are reactivated and the ball is thrown. This process needs to work over and over again.
Code:
Whew...if any of you made it this far, then thank you! I will be very grateful for any help you can provide.
I'm trying to create a ball that the player can pick up and either throw or release. I'm using physics for the ball except for when the player picks it up. The code works properly if the ball is at a complete stop. But if the ball is rolling or has bounced off a wall, then when the player tries to pick it up, the ball goes flying in some random direction.
I'm new to the physics engine, so I apologize if this code looks really bad. The problem may be something really simple that I'm missing. I'm hoping that you can help me fix this code so that it will work regardless of the state of the ball.
What should happen is this: If the player is near the ball, physics are on and the ball can be rolled around on the ground. If the player right clicks on the ball, he "picks it up", physics turn off and the ball is always centered on the screen, no matter where the player walks. If the player then right clicks again, physics are reactivated and the ball drops. Instead, if the player left clicks, physics are reactivated and the ball is thrown. This process needs to work over and over again.
Code:
action basketball
{
var my_position;
var distance_to_obj;
var obj_active=0;
var my_pos1;
my.polygon = on;
my.enable_touch = on;
my.enable_release = on;
my.enable_click = on;
my.event = basketball_event;
while (player == null){wait (1);}
while (1)
{
// ACTIVATES PHYSICS IF THE PLAYER IS NEAR
if (vec_dist (player.x, my.x) < 1000) && (my.skill22==0)
{
phent_settype (my, ph_rigid, ph_sphere);
ph_setgravity (vector (0, 0, -386));
phent_setgroup (my, 3);
phent_setmass (my, 0.624, ph_sphere);
phent_setfriction (my, 100);
phent_setelasticity (my, 25, 0);
phent_setmaxspeed( my, 100, 100);
my.skill22=1;
wait (1);
}
// DEACTIVATES PHYSICS IF THE PLAYER IS FAR
if (vec_dist (player.x, my.x) > 1000) && (my.skill22==1)
{
phent_settype (my, 0, ph_sphere);
my.skill22=0;
wait (1);
}
// IF YOU PICK UP THE BALL, PHYSICS TURN OFF TEMPORARILY
if ((mouse_right==on) && (bball_touched==1) && (obj_active==0))
{
phent_clearvelocity(my); //Is this line needed?
phent_settype (my, 0, ph_sphere);
distance_to_obj = vec_dist (player.x, my.x);
obj_active=1;
wait (10);
}
// CODE FOR "HOLDING" THE BALL
if (obj_active==1)
{
vec_set (my_position.x, vector(distance_to_obj, 0, 0));
vec_rotate(my_position.x, vector (camera.pan, camera.tilt, 0));
vec_add (my_position.x, camera.x);
temp.x = (my_position.x - my.x) * time;
temp.y = (my_position.y - my.y) * time;
temp.z = (my_position.z - my.z) * time;
c_move (my, temp.x, nullvector, ignore_passable|glide);
}
// CODE FOR "THROWING" THE BALL - FIND A WAY TO ALTER ANGLE OF THROW BASED ON CAMERA TILT!!!
if ((obj_active==1) && (mouse_left==on))
{
phent_settype (my, ph_rigid, ph_sphere);
temp.X = cos(player.PAN);
temp.Y = sin(player.PAN);
temp.Z = 10000*cos(0);
MY_POS1.X = player.X + temp.Z*temp.X;
MY_POS1.Y = player.Y + temp.Z*temp.Y;
MY_POS1.Z = player.Z + 10000*sin(0);
phent_addcentralforce(my,MY_POS1);
obj_active=0;
}
// BALL IS RELEASED
if ((mouse_right==on) && (obj_active==1))
{
phent_settype (my, ph_rigid, ph_sphere);
obj_active=0;
holding_object=0;
}
Whew...if any of you made it this far, then thank you! I will be very grateful for any help you can provide.

