Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 13,353 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
SEARCHING HARD FOR PHYSICS TUTORIALS #63407
02/08/06 04:05
02/08/06 04:05
Joined: Nov 2005
Posts: 14
calif
G
growthweb Offline OP
Newbie
growthweb  Offline OP
Newbie
G

Joined: Nov 2005
Posts: 14
calif
Hello, Iam desperately looking for physics tutorials for the a6 engine(I dont want to use newton). Iam sure there are many newbies like me who are in need of newer physics tutorials. The only tutorials I could find after searching all over the web and many 3d gamestudio sites where theses ...

Basi Physics Scripting
Part One,two and three – Single Entity Vehicle
By Brandon Batie (aka AcidCrow)

Those tutorials are very brief and limited and for older versions. Anybody out there know of any tutorials. I would be glad to pay for any physics tutorials. Thanks in advance.

Re: SEARCHING HARD FOR PHYSICS TUTORIALS [Re: growthweb] #63408
02/09/06 01:37
02/09/06 01:37
Joined: Aug 2001
Posts: 2,320
Alberta, Canada
William Offline
Expert
William  Offline
Expert

Joined: Aug 2001
Posts: 2,320
Alberta, Canada
Here is 3 tutorials from older 3PM issues... some are over 2 years old, but I don't believe there has been any syntax changes since then. The first one is un-edited so beware of bad grammer. P.S - The post is long but i've nowhere to upload these things or the demos with them nor the time required. So please don't ask for the demos that come with them.


Step into the Real World
by William

A large boulder come tumbling down the mountain side, another boulder appears gaining even greater speed then the first, soon the two boulders collide causing a change in speed and direction for both! The boulders then slowly come to a stop in the valley below. How can this realistic behavior be added to models in 3dgamestudio you ask? Well this article will explain that and much more on applying the physics engine to your models using either the commercial or professional edition.

This article applies to either commercial or professional edition owners of A6. If you own either standard or the extra edition, fear not, since most of the future areas covered in this column will not be centered on the physics engine. Included is the download for the resources needed in this article. The final results from this article can be seen in the file “physicsdone”. Now to get started!

Our objective in this tutorial is creating an entity, setting it as a physics object, then applying a force to it, and watching it roll and come to a stop. First open up the file named “physics” in WED. Next, load up the “boulder” wdl in SED. You will notice an action up top, just leave it there for now, I will explain what to do with it later. Everything from this point on should be added in the boulder script.

First, define your string which gives an unique id to the model in brackets. This will be our boulder!

string boulder = <stone.mdl>;

Next you’ll want to create a function which creates the boulder model.

function emit_sphere()
{
temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;

In this function you create a boulder at the temp position, and set its action to boulder_move.
Now you must create the action, which holds the instructions for enabling the entity as a physics object, then moving it accordingly.

if(boulderm != null){remove(boulderm);}

Next add that only if you have commercial edition as the first line in your emit_sphere function.


action boulder_move
{
boulderm = my;
my.shadow = on;

phent_settype(my, PH_RIGID, PH_sphere);
ph_setgravity ( earthgravity );
phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);

}

phent_settype(my, PH_RIGID, PH_sphere);

This line sets the entity as a physics object, “my” is the entity that you wish to set, “ph_rigid” turns the entity into a rigid body, which sets it in the physics engine, and “ph_sphere” makes the entity use a sphere hull for collision detection while in the physics engine.

var earthgravity[3] = 0,0, -300;
entity* boulderm;

Add these lines to the very beginning of your script.

ph_setgravity ( earthgravity );

This command sets the gravity of which the object moves along the z axis, or another axis if you require. A negative value in this will make it move downward along the specified axis.

phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);

These three lines of code determine the mass of the entity, the amount of friction it receives when moving across the ground, and its elasticity. First off, I will explain how mass work’s in 3dgamestudio. In the line “phent_setmass(my,3,PH_BOX);” you first set the entity that its applying to. Then the amount of mass, which currently is at 3, you usually will want to experiment with this to find what’s best for you. If you set the amount of mass as 0, the object that this line is applying to will become a fixed object, meaning no forces will move it, also, higher mass makes it so a greater force needs to be applied to move it. The last line in this command sets the hull type, the hull type will determine how the object’s mass will react to an external force.

phent_setfriction(my,70);

You set the friction in this line. The entity that its being applied to is set “my” and the amount of friction which is 70. If you set your friction too 0, it will be like moving on ice and if its set at 100, it’ll seem like rubber to rubber. The engine doesn’t like high friction values, especially for vehicle wheels.

phent_setelasticity(my, 30, 10);

In this line, you set the amount of “bounce” and its minimum speed to achieve this. First off you set what entity it’s applied too, in this case the “my” entity. Next you set how bouncy you want your object to be, if set at 0 your object will seem like it’s made of metal, if set to 100 it will be like a rubber ball. The last digit in this command determines the speed that the entity needs to have achieved before it bounces.

Currently Your script should look like this…

var earthgravity[3] = 0,0, -300;
entity* boulderm;

action obelisk
{
wait(1);
}

action boulder_move
{
boulderm = my;
my.shadow = on;

phent_settype(my, PH_RIGID, PH_poly);
ph_setgravity ( earthgravity );
phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);
}

string boulder = <stone.mdl>;

function emit_sphere()
{
if(boulderm != null){remove(boulderm);}
temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;

Do a test run and see what happens when you hit your left mouse button… Great you now created and applied an entity to the physics engine, and watched it fall to the ground using the values that you’ve set for the different commands. Now that you’ve got it falling to the ground and maybe rolling a bit, you will probably want to see a force applied to it, and watch it roll and collide with the wall, or other physics objects if you have the professional edition.

temp.x=cos(camera.pan)*300000;
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;
phent_addcentralforce ( my, temp );

These lines take the cameras pan and tilt angles, uses them to get an angle, which then gives what direction to apply a force on the physics entity that you created. First add them to your action after this line “phent_setelasticity(my, 30, 10);”.

temp.x=cos(camera.pan)*300000;
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;

This group of lines uses trigonometry instructions, cos, and sin. It’s rather simple really, its similar to typing cos/sin(number) and then times by 300000. This gets an angle relative to the view direction, which is used for the direction of the force. The 300000 is how powerful the force is in that direction. If you had experimented with particles before, temp.x, temp.y, temp.z for the force direction and speed is like vel_x, vel_y and vel_z is for a particles direction and speed.
phent_addcentralforce ( my, temp );

This line adds the force to the “my” entity. There is many different ways to add force, there explained in the manual quite well. Phent_ addcentralforce applies a force to the “my” entity directly at its center of mass. It uses temp for its direction and power.

function delete()
{
var timer;
while(1)
{
timer += 1;
if(timer > 1000){ remove(me); return;}
wait(1);
}
}

delete(); // to be added to your action

These lines are quite simple, add the “delete();” line to the very end of your action. This will delete the entity that you’ve created after a certain amount of time. Note that the variable timer is local, so other entities don’t delete all at the same time.

If you own the professional edition, change your previous obelisk function to this…

action obelisk
{
phent_settype(my, PH_RIGID, PH_poly);
ph_setgravity ( earthgravity );
phent_setmass(my, 120, PH_BOX);
phent_setfriction(my,7);
phent_setelasticity(my, 10, 10);
}

Congratulations Your Done!

Now that you’ve finished, go run your level, hit the left mouse button to create the boulder. If you own the commercial engine you can only have one object set at a time. So you’ll notice the last boulder created disabling from the physics engine if you create a new one. If you own professional have fun knocking over the obelisks.

The Final Commented Code:

var earthgravity[3] = 0,0, -300; // The amount of Gravity Force
entity* boulderm;

action obelisk //If You Own Commercial Edition, keep the action like this
{
wait(1);
}

action obelisk //If You Own Professional Edition, keep the action like this
{
phent_settype(my, PH_RIGID, PH_poly); // sets the object within the physics engine
ph_setgravity ( earthgravity );
phent_setmass(my, 120, PH_BOX);
phent_setfriction(my,7);
phent_setelasticity(my, 10, 10);
}

function delete() // this function deletes the entity set in the physics engine once timer is over 1000
{
var timer;

while(1)
{
timer += 1;
if(timer > 1000){ remove(me); return;}
wait(1);
}
}

action boulder_move // The action applied to the created boulder
{
boulderm = my;
my.shadow = on; //turns on the shadow

phent_settype(my, PH_RIGID, PH_sphere); //sets the object in the physics engine
ph_setgravity ( earthgravity ); // sets its gravity to “earthgravity”
phent_setmass(my, 3, PH_BOX); // sets its mass, and collision method
phent_setfriction(my,70); // sets the amount of friction
phent_setelasticity(my, 30, 10); // sets the amount of bounce, and the minimum speed

temp.x=cos(camera.pan)*300000; // finds the angle from the camera for direction
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;
phent_addcentralforce(my,temp); // adds a force on the my entity, using temp for direction and speed

delete(); // calls delete function

}

string boulder = <stone.mdl>;

function emit_sphere() // if left mouse button hit, will call this and create a boulder
{

if(boulderm != null){remove(boulderm);} // If you have commercial version put this in

temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;





Game Dynamics
By William

Ever notice how doors tend to move? Most doors contain a hinge connecting it to a wall. The hinge moves a door along a swivel when a force is applied. 3dgamestudio is capable of realistic hinge movement as well! In this article, you will learn how to use this to your advantage, and create a realistic moving door.

First, I’ll explain how hinges work. A hinge hampers the movement of the object that it’s connected to. In 3DGS, a hinge is called a constraint, since it limits a physics entity’s movement. There are many types of constraints available in 3DGS. This month will be focused completely on the hinge constraint. You can see how a door connected to a hinge typically moves (Figure 1). This is the type of movement we want to create. Start by opening up the “physic” level. Now load up the “hinge” and “physic” script in SED. Replace the line “include <hingedone.wdl>;” with “include <hinge.wdl>;”. Now you’re ready to code!

entity* hinge;

action hingej
{
my.passable = on;
hinge = me;
wait(1);
}

This will be the action which is applied to the back piece of the hinge. You’ll notice that it’s a pointer. It will be later used as a position on which the door connects, using a hinge constraint. Set it to the back hinge model which I have selected (Figure 2).

action door
{
phent_settype(my,PH_RIGID,PH_box); //enables entity into the physics engine
phent_setgroup(my,2);
phent_setmass(my,100,PH_box);
phent_setfriction(my,10);
phent_setelasticity(my,2 ,1);
}

The action you see above is going to be our door action, but there is still much more to add. In brief, you enable it in the physics engine. You then set its mass, friction, and the amount of bounce it contains. Remember, if you own the commercial edition, you can only have one object at a time set in the physics engine! In 3PM’s January issue, I explained in-depth how this works.

var h_ID;

var p2[3];
var p3[3];
var p4[3];
var p5[3];
var p6[3];

Add these variables to the top of your script.

vec_set(p2,vector(0,0,1)); //Sets your parameter vectors
vec_set(p3,vector(0,0,0));
vec_set(p4,vector(-100,100,0));
vec_set(p6,vector(0,0,0));

h_ID = phcon_add(ph_hinge, my, hinge); // enable this object as a hinge constraint

phcon_setparams1(h_ID,hinge.x, p2, nullvector); phcon_setparams2(h_ID,p4,nullvector,nullvector);

Type these lines of code into the bottom of your “door” action. In the first few lines, you are defining the parameters of your desired constraint.

h_ID = phcon_add(ph_hinge, my, 0);

This line adds a hinge constraint on the entity “my”. As of now, the added hinge constraints parameters still aren’t set.

phcon_setparams1(h_ID,hinge.x, p2, nullvector); phcon_setparams2(h_ID,p4,nullvector,nullvector);

These lines are very important. In 3DGS, any constraint, whether it be a wheel, hinge, slider, or ball, uses a couple of parameters to tell the engine how to limit its movement. There are 6 different parameters. If the constraint you are using doesn’t use them all, set the others as nullvector. In our case, we are using a hinge constraint. A hinge constraint uses 3 different parameters: 1, 2, and 4.

The first parameter tells the engine what entity to connect the constraint to. We currently have it set to the hinge model. So now our door model will be anchored to the hinge model. The second parameter sets what axis the model will move around when using the hinge constraint. In our case, we set the z-axis, so now the door model is anchored to the hinge model. It will only move around the z-axis when acted upon. The fourth parameter limits how much the hinge constraint will be allowed to rotate around the specified axis. If you were to have vec_set(p4,vector(-360,360,0)); instead of what we already have, the door model will be allowed to rotate completely around the z-axis without limits. In summary, the door model is anchored to the hinge model, and is limited to -100, 100 degree’s of rotation along the z-axis. Take a look at the diagram in (Figure3). Now build your level. You’ll notice that it moves a little unevenly, and fast. To correct this, we will have to add some more code to our “door” action.

var max[3];

Add that variable to the top of your script.

vec_set(max,vector(20,10,0));
phcon_setmotor (h_ID, max, nullvector , nullvector);

This code should be typed right on the bottom of your “door” action. This sets a motor to your hinge constraint! In other words, it sets the maximum amount of velocity your hinge constraint can reach, and the amount of force or torque needed to achieve it. In our case, we wanted to slow down how fast the door moves when the player touches it, so we lowered the maximum amount of velocity it is allowed to achieve, using the set motor command.

That concludes this month’s Game Dynamics section! Remember that a hinge has many more functions than moving a simple door. When used for the joint between a trailer and a vehicle in 3DGS, it produces some amazing results!




Game Dynamics
By William

Bam! You just hit the punching bag with full force, causing it to spin out of control. Boom! You nail it again, feeling your arms pulsate as you hit the punching bag. How can a realistic punching bag be implemented in 3dgs? Very easily! If you own the commercial or professional edition of A6, it can be done in a few simple steps. This article will teach you how to get the punching bag moving. Let’s get started!

Open up the “physics” level in WED. Next, load up the “punch” and “physics” script in SED. Replace the line “include <punchdone.wdl>;” with “include <punch.wdl>;”. Now you’re ready to code!


entity* joint; // pointers which will be used
entity* right_hand;
entity* left_hand;

var earthgravity[3] = 0,0, -1500; // The Punching Bag gravity(Modify if you want)
var b_ID; // The variable that our ball constraint will be stored in
var s_h; // Stands for “select hit” and will be used in determining which mouse button was pressed

Add these 3 pointers, and the 3 variables to the top of your script. They will be used in the code during the article.

action bjoint //"chain"
{
my.passable = on;
joint = me;
wait(1);
}

This will be the action which is applied to the top piece of the punching bag – it will act as the chain. You will notice that it’s a pointer. It will be later used as a position on which the punching bag connects, using a ball constraint. Set it to the chain model which I have selected (Figure 1).

action punching_bag //punching bag action
{
phent_settype(my,PH_RIGID,PH_box); //enables entity into the physics engine
ph_setgravity(earthgravity);
phent_setgroup(my,2);
phent_setmass(my,1000,PH_box);
phent_setfriction(my,100);
phent_setelasticity(my,0 ,0);

b_ID = phcon_add(ph_ball, my, joint); // enable this object as a ball constraint

phcon_setparams1(b_ID,joint.x, nullvector, nullvector);
phcon_setparams2(b_ID,nullvector,nullvector,nullvector);
}

That is the action that is applied to the punching bag. The first 6 or so lines were explained in the January issue of 3PM. Next, you add a ph_ball constraint to your punching bag. You want the punching bag to connect to the chain; thus, you used “joint.x” as the anchor point. You can see this in (Figure 2). There are no other parameters to set in a ph_ball constraint.

Build your level and run it. You will notice that you now have a realistic punching bag. What good is a punching bag if there isn’t a pair of fists to hit it?

First, we need the engine to create our fist models. We will do that with this function. Go back into your “physics” script, and enter move_hand(); in the main function. That way, the hands are created at the start of the game.

function move_hand() //create the hands
{
ent_create("brhand.mdl", player.x, hit_funl);
ent_create("blhand.mdl", player.x, hit_funr);
}

Now that the fists are created, we need to position them in line with the guard model. To do this, we will need them to update in a while loop. Since there are 2 fists, we will need 2 functions – one for each fist.

function hit_funl() //movement of left hand
{
left_hand = me;
while(1)
{
temp.x = 20;
temp.y = 16;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;

wait(1);
}
}

function hit_funr() //movement of right hand
{
right_hand = me;
while(1)
{
temp.x = 25;
temp.y = -10;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;

wait(1);
}
}

Now, build your level, and run it. You will notice a pair of fists following you. Why? This is because you set some temp values which are used to determine how the fist’s position is compared to the players. Then, you rotated the temp values by the pan, tilt, and roll of the player using the vec_rotate command. You need to do this; otherwise, the fists will move according to the world coordinates, rather than the player’s coordinates. Next, you added the player’s position to the temp vector. Temp is now the player’s position, plus the added numbers. Finally, you made sure the fists were always turned according to the player.

What good are fists if you can’t punch with them? We’ll make it so that the fists move according to the left and right mouse buttons. The left fist will move if the left mouse button is pressed, and the right fist will move if the right mouse button is pressed.

if(mouse_left == 1)
{
temp.x = 40;
temp.y = 16;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;
}

Enter this after the line “my.pan = player.pan” in the “hit_funl” function.

if(mouse_right == 1)
{
temp.x = 40;
temp.y = -10;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;
}

Enter this after the line “my.pan = player.pan” in the “hit_funr” function.

What do these lines of code do? They are very similar to your previous lines; the only major difference is that you are moving the hand model upwards on the x axis. Also, they check whether the left or right mouse button was hit.

Way to go! You’ve finished the article. Now, go ahead and use this knowledge to create your own boxing game, and much more.


Check out Silas. www.kartsilas.com

Hear my band Finding Fire - www.myspace.com/findingfire

Daily dev updates - http://kartsilas.blogspot.com/
Re: SEARCHING HARD FOR PHYSICS TUTORIALS [Re: William] #63409
02/09/06 06:16
02/09/06 06:16
Joined: Nov 2005
Posts: 14
calif
G
growthweb Offline OP
Newbie
growthweb  Offline OP
Newbie
G

Joined: Nov 2005
Posts: 14
calif
Thank you so very much! Great tutorial. I love physics and this is a great start.

Re: SEARCHING HARD FOR PHYSICS TUTORIALS [Re: William] #63410
02/06/08 08:31
02/06/08 08:31
Joined: Sep 2007
Posts: 158
Guangzhou China
bomber Offline
Member
bomber  Offline
Member

Joined: Sep 2007
Posts: 158
Guangzhou China
Quote:

Here is 3 tutorials from older 3PM issues... some are over 2 years old, but I don't believe there has been any syntax changes since then. The first one is un-edited so beware of bad grammer. P.S - The post is long but i've nowhere to upload these things or the demos with them nor the time required. So please don't ask for the demos that come with them.


Step into the Real World
by William

A large boulder come tumbling down the mountain side, another boulder appears gaining even greater speed then the first, soon the two boulders collide causing a change in speed and direction for both! The boulders then slowly come to a stop in the valley below. How can this realistic behavior be added to models in 3dgamestudio you ask? Well this article will explain that and much more on applying the physics engine to your models using either the commercial or professional edition.

This article applies to either commercial or professional edition owners of A6. If you own either standard or the extra edition, fear not, since most of the future areas covered in this column will not be centered on the physics engine. Included is the download for the resources needed in this article. The final results from this article can be seen in the file “physicsdone”. Now to get started!

Our objective in this tutorial is creating an entity, setting it as a physics object, then applying a force to it, and watching it roll and come to a stop. First open up the file named “physics” in WED. Next, load up the “boulder” wdl in SED. You will notice an action up top, just leave it there for now, I will explain what to do with it later. Everything from this point on should be added in the boulder script.

First, define your string which gives an unique id to the model in brackets. This will be our boulder!

string boulder = <stone.mdl>;

Next you’ll want to create a function which creates the boulder model.

function emit_sphere()
{
temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;

In this function you create a boulder at the temp position, and set its action to boulder_move.
Now you must create the action, which holds the instructions for enabling the entity as a physics object, then moving it accordingly.

if(boulderm != null){remove(boulderm);}

Next add that only if you have commercial edition as the first line in your emit_sphere function.


action boulder_move
{
boulderm = my;
my.shadow = on;

phent_settype(my, PH_RIGID, PH_sphere);
ph_setgravity ( earthgravity );
phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);

}

phent_settype(my, PH_RIGID, PH_sphere);

This line sets the entity as a physics object, “my” is the entity that you wish to set, “ph_rigid” turns the entity into a rigid body, which sets it in the physics engine, and “ph_sphere” makes the entity use a sphere hull for collision detection while in the physics engine.

var earthgravity[3] = 0,0, -300;
entity* boulderm;

Add these lines to the very beginning of your script.

ph_setgravity ( earthgravity );

This command sets the gravity of which the object moves along the z axis, or another axis if you require. A negative value in this will make it move downward along the specified axis.

phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);

These three lines of code determine the mass of the entity, the amount of friction it receives when moving across the ground, and its elasticity. First off, I will explain how mass work’s in 3dgamestudio. In the line “phent_setmass(my,3,PH_BOX);” you first set the entity that its applying to. Then the amount of mass, which currently is at 3, you usually will want to experiment with this to find what’s best for you. If you set the amount of mass as 0, the object that this line is applying to will become a fixed object, meaning no forces will move it, also, higher mass makes it so a greater force needs to be applied to move it. The last line in this command sets the hull type, the hull type will determine how the object’s mass will react to an external force.

phent_setfriction(my,70);

You set the friction in this line. The entity that its being applied to is set “my” and the amount of friction which is 70. If you set your friction too 0, it will be like moving on ice and if its set at 100, it’ll seem like rubber to rubber. The engine doesn’t like high friction values, especially for vehicle wheels.

phent_setelasticity(my, 30, 10);

In this line, you set the amount of “bounce” and its minimum speed to achieve this. First off you set what entity it’s applied too, in this case the “my” entity. Next you set how bouncy you want your object to be, if set at 0 your object will seem like it’s made of metal, if set to 100 it will be like a rubber ball. The last digit in this command determines the speed that the entity needs to have achieved before it bounces.

Currently Your script should look like this…

var earthgravity[3] = 0,0, -300;
entity* boulderm;

action obelisk
{
wait(1);
}

action boulder_move
{
boulderm = my;
my.shadow = on;

phent_settype(my, PH_RIGID, PH_poly);
ph_setgravity ( earthgravity );
phent_setmass(my, 3, PH_BOX);
phent_setfriction(my,70);
phent_setelasticity(my, 30, 10);
}

string boulder = <stone.mdl>;

function emit_sphere()
{
if(boulderm != null){remove(boulderm);}
temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;

Do a test run and see what happens when you hit your left mouse button… Great you now created and applied an entity to the physics engine, and watched it fall to the ground using the values that you’ve set for the different commands. Now that you’ve got it falling to the ground and maybe rolling a bit, you will probably want to see a force applied to it, and watch it roll and collide with the wall, or other physics objects if you have the professional edition.

temp.x=cos(camera.pan)*300000;
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;
phent_addcentralforce ( my, temp );

These lines take the cameras pan and tilt angles, uses them to get an angle, which then gives what direction to apply a force on the physics entity that you created. First add them to your action after this line “phent_setelasticity(my, 30, 10);”.

temp.x=cos(camera.pan)*300000;
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;

This group of lines uses trigonometry instructions, cos, and sin. It’s rather simple really, its similar to typing cos/sin(number) and then times by 300000. This gets an angle relative to the view direction, which is used for the direction of the force. The 300000 is how powerful the force is in that direction. If you had experimented with particles before, temp.x, temp.y, temp.z for the force direction and speed is like vel_x, vel_y and vel_z is for a particles direction and speed.
phent_addcentralforce ( my, temp );

This line adds the force to the “my” entity. There is many different ways to add force, there explained in the manual quite well. Phent_ addcentralforce applies a force to the “my” entity directly at its center of mass. It uses temp for its direction and power.

function delete()
{
var timer;
while(1)
{
timer += 1;
if(timer > 1000){ remove(me); return;}
wait(1);
}
}

delete(); // to be added to your action

These lines are quite simple, add the “delete();” line to the very end of your action. This will delete the entity that you’ve created after a certain amount of time. Note that the variable timer is local, so other entities don’t delete all at the same time.

If you own the professional edition, change your previous obelisk function to this…

action obelisk
{
phent_settype(my, PH_RIGID, PH_poly);
ph_setgravity ( earthgravity );
phent_setmass(my, 120, PH_BOX);
phent_setfriction(my,7);
phent_setelasticity(my, 10, 10);
}

Congratulations Your Done!

Now that you’ve finished, go run your level, hit the left mouse button to create the boulder. If you own the commercial engine you can only have one object set at a time. So you’ll notice the last boulder created disabling from the physics engine if you create a new one. If you own professional have fun knocking over the obelisks.

The Final Commented Code:

var earthgravity[3] = 0,0, -300; // The amount of Gravity Force
entity* boulderm;

action obelisk //If You Own Commercial Edition, keep the action like this
{
wait(1);
}

action obelisk //If You Own Professional Edition, keep the action like this
{
phent_settype(my, PH_RIGID, PH_poly); // sets the object within the physics engine
ph_setgravity ( earthgravity );
phent_setmass(my, 120, PH_BOX);
phent_setfriction(my,7);
phent_setelasticity(my, 10, 10);
}

function delete() // this function deletes the entity set in the physics engine once timer is over 1000
{
var timer;

while(1)
{
timer += 1;
if(timer > 1000){ remove(me); return;}
wait(1);
}
}

action boulder_move // The action applied to the created boulder
{
boulderm = my;
my.shadow = on; //turns on the shadow

phent_settype(my, PH_RIGID, PH_sphere); //sets the object in the physics engine
ph_setgravity ( earthgravity ); // sets its gravity to “earthgravity”
phent_setmass(my, 3, PH_BOX); // sets its mass, and collision method
phent_setfriction(my,70); // sets the amount of friction
phent_setelasticity(my, 30, 10); // sets the amount of bounce, and the minimum speed

temp.x=cos(camera.pan)*300000; // finds the angle from the camera for direction
temp.y=sin(camera.pan)*300000;
temp.z=sin(camera.tilt)*300000;
phent_addcentralforce(my,temp); // adds a force on the my entity, using temp for direction and speed

delete(); // calls delete function

}

string boulder = <stone.mdl>;

function emit_sphere() // if left mouse button hit, will call this and create a boulder
{

if(boulderm != null){remove(boulderm);} // If you have commercial version put this in

temp.x=camera.x;
temp.y=camera.y;
temp.z=camera.z;
ent_create(boulder,temp,boulder_move);
}

on_mouse_left = emit_sphere;





Game Dynamics
By William

Ever notice how doors tend to move? Most doors contain a hinge connecting it to a wall. The hinge moves a door along a swivel when a force is applied. 3dgamestudio is capable of realistic hinge movement as well! In this article, you will learn how to use this to your advantage, and create a realistic moving door.

First, I’ll explain how hinges work. A hinge hampers the movement of the object that it’s connected to. In 3DGS, a hinge is called a constraint, since it limits a physics entity’s movement. There are many types of constraints available in 3DGS. This month will be focused completely on the hinge constraint. You can see how a door connected to a hinge typically moves (Figure 1). This is the type of movement we want to create. Start by opening up the “physic” level. Now load up the “hinge” and “physic” script in SED. Replace the line “include <hingedone.wdl>;” with “include <hinge.wdl>;”. Now you’re ready to code!

entity* hinge;

action hingej
{
my.passable = on;
hinge = me;
wait(1);
}

This will be the action which is applied to the back piece of the hinge. You’ll notice that it’s a pointer. It will be later used as a position on which the door connects, using a hinge constraint. Set it to the back hinge model which I have selected (Figure 2).

action door
{
phent_settype(my,PH_RIGID,PH_box); //enables entity into the physics engine
phent_setgroup(my,2);
phent_setmass(my,100,PH_box);
phent_setfriction(my,10);
phent_setelasticity(my,2 ,1);
}

The action you see above is going to be our door action, but there is still much more to add. In brief, you enable it in the physics engine. You then set its mass, friction, and the amount of bounce it contains. Remember, if you own the commercial edition, you can only have one object at a time set in the physics engine! In 3PM’s January issue, I explained in-depth how this works.

var h_ID;

var p2[3];
var p3[3];
var p4[3];
var p5[3];
var p6[3];

Add these variables to the top of your script.

vec_set(p2,vector(0,0,1)); //Sets your parameter vectors
vec_set(p3,vector(0,0,0));
vec_set(p4,vector(-100,100,0));
vec_set(p6,vector(0,0,0));

h_ID = phcon_add(ph_hinge, my, hinge); // enable this object as a hinge constraint

phcon_setparams1(h_ID,hinge.x, p2, nullvector); phcon_setparams2(h_ID,p4,nullvector,nullvector);

Type these lines of code into the bottom of your “door” action. In the first few lines, you are defining the parameters of your desired constraint.

h_ID = phcon_add(ph_hinge, my, 0);

This line adds a hinge constraint on the entity “my”. As of now, the added hinge constraints parameters still aren’t set.

phcon_setparams1(h_ID,hinge.x, p2, nullvector); phcon_setparams2(h_ID,p4,nullvector,nullvector);

These lines are very important. In 3DGS, any constraint, whether it be a wheel, hinge, slider, or ball, uses a couple of parameters to tell the engine how to limit its movement. There are 6 different parameters. If the constraint you are using doesn’t use them all, set the others as nullvector. In our case, we are using a hinge constraint. A hinge constraint uses 3 different parameters: 1, 2, and 4.

The first parameter tells the engine what entity to connect the constraint to. We currently have it set to the hinge model. So now our door model will be anchored to the hinge model. The second parameter sets what axis the model will move around when using the hinge constraint. In our case, we set the z-axis, so now the door model is anchored to the hinge model. It will only move around the z-axis when acted upon. The fourth parameter limits how much the hinge constraint will be allowed to rotate around the specified axis. If you were to have vec_set(p4,vector(-360,360,0)); instead of what we already have, the door model will be allowed to rotate completely around the z-axis without limits. In summary, the door model is anchored to the hinge model, and is limited to -100, 100 degree’s of rotation along the z-axis. Take a look at the diagram in (Figure3). Now build your level. You’ll notice that it moves a little unevenly, and fast. To correct this, we will have to add some more code to our “door” action.

var max[3];

Add that variable to the top of your script.

vec_set(max,vector(20,10,0));
phcon_setmotor (h_ID, max, nullvector , nullvector);

This code should be typed right on the bottom of your “door” action. This sets a motor to your hinge constraint! In other words, it sets the maximum amount of velocity your hinge constraint can reach, and the amount of force or torque needed to achieve it. In our case, we wanted to slow down how fast the door moves when the player touches it, so we lowered the maximum amount of velocity it is allowed to achieve, using the set motor command.

That concludes this month’s Game Dynamics section! Remember that a hinge has many more functions than moving a simple door. When used for the joint between a trailer and a vehicle in 3DGS, it produces some amazing results!




Game Dynamics
By William

Bam! You just hit the punching bag with full force, causing it to spin out of control. Boom! You nail it again, feeling your arms pulsate as you hit the punching bag. How can a realistic punching bag be implemented in 3dgs? Very easily! If you own the commercial or professional edition of A6, it can be done in a few simple steps. This article will teach you how to get the punching bag moving. Let’s get started!

Open up the “physics” level in WED. Next, load up the “punch” and “physics” script in SED. Replace the line “include <punchdone.wdl>;” with “include <punch.wdl>;”. Now you’re ready to code!


entity* joint; // pointers which will be used
entity* right_hand;
entity* left_hand;

var earthgravity[3] = 0,0, -1500; // The Punching Bag gravity(Modify if you want)
var b_ID; // The variable that our ball constraint will be stored in
var s_h; // Stands for “select hit” and will be used in determining which mouse button was pressed

Add these 3 pointers, and the 3 variables to the top of your script. They will be used in the code during the article.

action bjoint //"chain"
{
my.passable = on;
joint = me;
wait(1);
}

This will be the action which is applied to the top piece of the punching bag – it will act as the chain. You will notice that it’s a pointer. It will be later used as a position on which the punching bag connects, using a ball constraint. Set it to the chain model which I have selected (Figure 1).

action punching_bag //punching bag action
{
phent_settype(my,PH_RIGID,PH_box); //enables entity into the physics engine
ph_setgravity(earthgravity);
phent_setgroup(my,2);
phent_setmass(my,1000,PH_box);
phent_setfriction(my,100);
phent_setelasticity(my,0 ,0);

b_ID = phcon_add(ph_ball, my, joint); // enable this object as a ball constraint

phcon_setparams1(b_ID,joint.x, nullvector, nullvector);
phcon_setparams2(b_ID,nullvector,nullvector,nullvector);
}

That is the action that is applied to the punching bag. The first 6 or so lines were explained in the January issue of 3PM. Next, you add a ph_ball constraint to your punching bag. You want the punching bag to connect to the chain; thus, you used “joint.x” as the anchor point. You can see this in (Figure 2). There are no other parameters to set in a ph_ball constraint.

Build your level and run it. You will notice that you now have a realistic punching bag. What good is a punching bag if there isn’t a pair of fists to hit it?

First, we need the engine to create our fist models. We will do that with this function. Go back into your “physics” script, and enter move_hand(); in the main function. That way, the hands are created at the start of the game.

function move_hand() //create the hands
{
ent_create("brhand.mdl", player.x, hit_funl);
ent_create("blhand.mdl", player.x, hit_funr);
}

Now that the fists are created, we need to position them in line with the guard model. To do this, we will need them to update in a while loop. Since there are 2 fists, we will need 2 functions – one for each fist.

function hit_funl() //movement of left hand
{
left_hand = me;
while(1)
{
temp.x = 20;
temp.y = 16;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;

wait(1);
}
}

function hit_funr() //movement of right hand
{
right_hand = me;
while(1)
{
temp.x = 25;
temp.y = -10;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;

wait(1);
}
}

Now, build your level, and run it. You will notice a pair of fists following you. Why? This is because you set some temp values which are used to determine how the fist’s position is compared to the players. Then, you rotated the temp values by the pan, tilt, and roll of the player using the vec_rotate command. You need to do this; otherwise, the fists will move according to the world coordinates, rather than the player’s coordinates. Next, you added the player’s position to the temp vector. Temp is now the player’s position, plus the added numbers. Finally, you made sure the fists were always turned according to the player.

What good are fists if you can’t punch with them? We’ll make it so that the fists move according to the left and right mouse buttons. The left fist will move if the left mouse button is pressed, and the right fist will move if the right mouse button is pressed.

if(mouse_left == 1)
{
temp.x = 40;
temp.y = 16;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;
}

Enter this after the line “my.pan = player.pan” in the “hit_funl” function.

if(mouse_right == 1)
{
temp.x = 40;
temp.y = -10;
temp.z = 35;
vec_rotate(temp,player.pan);
vec_add(temp,player.x);
vec_set(my.x, temp);
my.pan = player.pan;
}

Enter this after the line “my.pan = player.pan” in the “hit_funr” function.

What do these lines of code do? They are very similar to your previous lines; the only major difference is that you are moving the hand model upwards on the x axis. Also, they check whether the left or right mouse button was hit.

Way to go! You’ve finished the article. Now, go ahead and use this knowledge to create your own boxing game, and much more.



very detailed


"I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know."
————Donald Rumfeld
Re: SEARCHING HARD FOR PHYSICS TUTORIALS [Re: bomber] #63411
02/10/08 19:39
02/10/08 19:39
Joined: Aug 2006
Posts: 96
Netherlands
P
Polypfreak1987 Offline
Junior Member
Polypfreak1987  Offline
Junior Member
P

Joined: Aug 2006
Posts: 96
Netherlands
Thank you for the tutorial. But I have 2 models. 1 cross and 1 gondela.

The cross rotates around his own axis with "pan".
The gondela is attached to the cross, but the gondela need to rotates around his own axis with "G-Force" (so, it also have an orientation). The door-script helped me very far.

The only problem is now that the gondela rotates also if the cross is not moving. How can I fix this problem?


Moderated by  HeelX, Spirit 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1