Physics code problem

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:
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.
Posted By: demiGod

Re: Physics code problem - 08/31/06 20:32

It seems that ball disable process itīs not correct or somewhere in the code the ball are being activated when shouldnīt be. Although phent_settype (my, 0, ph_sphere); should deactivate the ball, i think you should try phent_enable(my, 0).
Posted By: CBSection31

Re: Physics code problem - 08/31/06 21:12

For some reason, whenever I try phent_enable(my, 0), it doesn't deactivate the physics on an entity. :\ I've had that problem from day one. I'll play around with it for a bit and see if I can get it to work. Thanks for your help!
Posted By: CBSection31

Re: Physics code problem - 08/31/06 21:31

Okay, I ran a quick test and everything works properly repeatedly if I turn off the part of the code that has the header // CODE FOR "HOLDING" THE BALL. So something within that piece of code is causing the ball to go haywire if it's in motion. Any ideas? Is there an alternate way of having the ball follow the player around, centered at the screen?
Posted By: demiGod

Re: Physics code problem - 08/31/06 21:35

I think you should change the structure of your code, for example:

a) you are registering the ball entity and applying itīs properties inside a loop. You should assign those commands in the beginning of the function before the while loop;

b) you are using phent_settype too much times. You only need to use it one time when register the ball;

c) for changing states of the ball just use phent_enable accordingly to the conditions you want, enabling or desabling the entity temporarly.

d) Also check what you are using in the basketball_event function.
Posted By: CBSection31

Re: Physics code problem - 08/31/06 22:01

Okay, based on your advice I am going to re-write the code from scratch. I ran into a problem, however. This basic piece of code is not working:

Code:
action basketball
{
phent_settype (my, ph_rigid, ph_sphere);
ph_setgravity (vector (0, 0, -386)); // play with this value
phent_setgroup (my, 3);
phent_setmass (my, 0.624, ph_sphere); // play with this value
phent_setfriction (my, 100);
phent_setelasticity (my, 25, 0);
phent_setmaxspeed( my, 500, 500);
wait (3);
while (player == null){wait (1);}
while (1)
{
if (vec_dist (player.x, my.x) < 1000)
{
phent_enable(my,0);
my.skill22=1;
wait (1);
}



From what I understand, this should mean that the ball's physics turn off when the player is near. Yet this doesn't happen...the ball remains activated. Am I using phent_enable improperly?

I'm sorry for all the questions--I know it can't be fun teaching the basics to someone. I really do appreciate your help.
Posted By: demiGod

Re: Physics code problem - 08/31/06 22:39

Code:

action basketball
{
phent_settype (my, ph_rigid, ph_sphere);
// you should place this code on the main function, but it works
ph_setgravity (vector (0, 0, -386)); // play with this value

phent_setgroup (my, 3);
phent_setmass (my, 0.624, ph_sphere); // play with this value
phent_setfriction (my, 100);
phent_setelasticity (my, 25, 0);
// disable it for now, you must read this function on the manual
//phent_setmaxspeed( my, 500, 500);

//wait (3); //what for?
while (player == null){wait (1);}
while (1)
{
if (vec_dist (player.x, my.x) < 1000)
{
phent_enable(my,0);
my.skill22=1;
//wait (1); //what for?
}
wait(1);
}
}




I ran a test and it works fine now. When the ball reaches 1000 quants to the player, it stops and stays disabled temporarly
Posted By: CBSection31

Re: Physics code problem - 08/31/06 23:30

Okay, I am completely baffled now. I copied and pasted the code from your above post and placed it into the script in place of everything I had under the basketball action, and it STILL does not work...what happens is, the ball can still be pushed by the player by walking into it. It doesn't roll like it did before, but it can still be pushed. I'm wondering if there's some system specific problem causing phent_enable to not work properly?
Posted By: CBSection31

Re: Physics code problem - 09/01/06 01:01

Hmm, the phent_enable command worked with a different object, but not with this one...I'll keep running tests.
© 2023 lite-C Forums