|
2 registered members (TipmyPip, izorro),
556
guests, and 2
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Physics code problem
#88384
08/31/06 18:55
08/31/06 18:55
|
Joined: Oct 2005
Posts: 131
CBSection31
OP
Member
|
OP
Member
Joined: Oct 2005
Posts: 131
|
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. 
|
|
|
Re: Physics code problem
[Re: demiGod]
#88389
08/31/06 22:01
08/31/06 22:01
|
Joined: Oct 2005
Posts: 131
CBSection31
OP
Member
|
OP
Member
Joined: Oct 2005
Posts: 131
|
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. 
|
|
|
Re: Physics code problem
[Re: CBSection31]
#88390
08/31/06 22:39
08/31/06 22:39
|
Joined: Mar 2006
Posts: 752 Portugal
demiGod
User
|
User
Joined: Mar 2006
Posts: 752
Portugal
|
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 
|
|
|
Re: Physics code problem
[Re: demiGod]
#88391
08/31/06 23:30
08/31/06 23:30
|
Joined: Oct 2005
Posts: 131
CBSection31
OP
Member
|
OP
Member
Joined: Oct 2005
Posts: 131
|
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?
Last edited by CBSection31; 08/31/06 23:39.
|
|
|
Re: Physics code problem
[Re: CBSection31]
#88392
09/01/06 01:01
09/01/06 01:01
|
Joined: Oct 2005
Posts: 131
CBSection31
OP
Member
|
OP
Member
Joined: Oct 2005
Posts: 131
|
Hmm, the phent_enable command worked with a different object, but not with this one...I'll keep running tests.
Last edited by CBSection31; 09/01/06 01:03.
|
|
|
|