for Beginners: physics

Post your own tutorials, guides and demos.

Post » Wed Jul 09, 2008 2:02 pm

Assume the position. Download this .cap and fire it up.

download/file.php?id=102

What you see are 3 smiley's, close to the shape of a ball.
All 3 are instances of the same object.
Select one, and bring up the Animator (on the right with all the other organizers ).

They have 3 animation states: fall, climb and bounce.

This is as far as i prepared this position to start from.

Now what do i want to do ?

I want the smiley's to fall by Gravity.
When they are falling i want them to smile.
When they are climbing i want to them to "ohhhhh!".
When they bounce i want them to stick out there tongues.

I want them to bounce on a ground, and during the bounce to act more as plastic balls, including seizing there shapes a bit, and getting a little jump up.

Ok, thats the project ( i have not done this before, by the way )

So bring up the objects properties on the left of the layout editor.

Add the behavior "physics" to one of the balls. And lets walk trough this behavior.

1/'link'
__to remove the behavior.

2/Name
__ You can change that name. Just dont use spaces plz. A project can have many different physic behaviors. Even 1 object can have more physics behaviors. To simplify addressing a certain behavior in the events, its handy to Name them.

3/ Gravity
__is a switch. Will the object be influenced by gravity or not ?

4/ Immovable.
__is a switch. Objects with a physics behavior will only interact using that behavior with other objects if they also have a physics behavior. Simply stated: To bounce the object from a "ground' object. Also the 'ground' object needs to have the physics behavior. But since we do not want the ground to bounce, or move, or even shake 1 pixel, we can set it to 'Immovable' with this switch.

5/ No rotation.
__ switch. Allow the physic forces to rotate the object or not.

6/ Mass
__ can be tweened in the layout and in the actions. Just stands for how heavy the object is. Heavier objects do not fall faster. But they act different to forces added to the object by user, or by the physics system.

7/ Linear damping.
__ can be tweened in the layout and in the actions. Gravity is not a constant speed. When you let something fall. It starts with a zero speed. And how longer it falls, it will build up its speed. Falling faster and faster. Now lets say: When you jump from a high building. You will not accelerate for ever. There is the air that resists, your cloths, and so many other things, causing you to at one point fall with a speed close to constant.
To represent reality in a physics system we need to have something thats damping the acceleration. You can use Linear damping to archive this. You should give bigger objects a bigger damping then small objects. And feathers even a very big damping.

8/ Angular damping.
__ can be tweened in the layout and in the actions. And is to damp the accelerations in the forces that make an object rotate.

9/ Collision mask.
__ is a switch. Bounding, Ellipse, or none. None will exclude the object from any collisions / bouncing MADE BY THE BEHAVIOR only. When you trow a square at a wall, the forces that make it bounce back and rotate, are different then when you trow a ball at the same wall. To set this switch right, just ask yourself: Do i want the object to behave as a ball, or as a block. Do not mix this up with the objects collision method. They work together. This mask will alter the way the object is reacting when colliding. The objects collisions method properties define WHERE exactly (at witch pixel related to the sprite) the collision will happen.

10/ World solver. 11/World friction. 12/World x scale. 13/World Y scale.
__ oh i dont understand this yet. So plz comment if you know its function.

14/
World gravity.
__ __ can be tweened in the layout and in the actions. Stands for the acceleration of the gravity.
9,8 means: Each second the gravity will go 9,8 units/second faster .. and damped by the linear damping value.



Next post i take the behavior to the events editor, and walk step by step trough it there.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 5:07 pm

Right, so still with the same .cap. Name one of the smileys to anything you like.

Go to the Events editor.

Add an event to the empty sheet.
Start from the smiley object.

In the next step of the wizard, where you are supposed to choose a condition ..
Notice the object has 2 tabs on top.
Its own tab and a tab called Physics.
Click the Physics tab.

Wah ? no conditions for the Physics behavior ? Ah yes sir. In the present release the Physics still lack conditions. This will be something we will have to work around. And oh we will. Do not worry.

Maybe in a next release there will be conditions added.
Possible conditions could be:
On 'object' is moving.
On 'object' bounce.
On 'object' at turn point.
On 'object' slowing down
on 'object' speeding up.
Compare 'objects" X velocity
Compare 'objects" Y velocity
But for now, no conditions. We will counter this.

So add a system event from the system condition "always". To explore the actions.
Add an action to this. Start from the smiley object. Click on the physics Tab.

Yes sir ! There are a bunch of actions here.

Lets walk trough them.


1/ Add force.
Imagine a paper airplane passing you. And you give it a little slam to make it fly faster. Thats what 'Add force' is meant for. The paper plane was moving already due a force given by some one else earlier, and you add your own force to it. Now slamming that airplane makes it move according the angle that you slammed it. Slam it with your hand moving from down to up, will cause the plane to move upwards. This is why Force has 2 values, X and Y component. When you add a force, you will have to figure out the X and Y component of this force. X and Y components relate to each other as cos and sin. but that is for later.

2/
Set force.
Imagine you catch that paper airplane. And trow it yourself by your own force. In other words, take away the force that the paper plane already had, and change it to only your force = set force

3/ Add force towards a position.
Read: add a force to an object so, that when the object on the moment that you add this force and it was not moving by any other forces, it will move to the given position. Simply said: Add a force to a non moving object and as result it moves to the given position. Of course, since you "add", this new force, it will add to the forces that make the object already move, so its not sure that it will move towards the given position as result.

lets say, there is a car passing you. And you give it by hand a force towards the middle of the road (position). The car will hardly change direction, since the forces that move it already, added with your force to push it to the middle do not change much at the global situation.

4/ Set force towards position.
Take all the possible forces from an object and add a new force resulting in the object moving to the given position. Or: Stop the object, then move it towards the given position by the given force.

Or: Stop the car and push it to the given direction.


5/ Create Hinge to position.
Make the object glue to a certain position, with a given Stiffness.

Like lets say. A door hanging in its hinges. When you slam the door, it will try to keep its position to the hinges. Hinge to a position is like hinging the object to an immovable object.

6/ Create Hinge to object.
Same as 5 but to an object, lets say a door to a wall.
When you slam the door with a big force: the hinges will break. And an even bigger force will hang the wall at the door while both move into space.
Also, when the door has way more mass then the walls, the door will move the walls.

7/Set Gravity enabled.
On or off for this object.

8/Set mass.
Make the object lighter or more heavy.

9/Set linear damping.
Damp the accelerations in the moving forces.

10/ Set Force of gravity.
9,8 on earth.

11/ Add torque.

Ok, we had force.
When you hit a ball, you add a force to it.

When you balance the ball on the tip of one of your fingers, and give it a little push to rotate it, you add torque to it.

When the ball was already rotating on that moment, your torque will be added to the forces (torque) that made it rotate already.

12/ Set torque.
Thats like stopping the rotation of the ball and give it a new rotation.
Or:
Take away any torque off the object, and give it a totally new torque.

13/Set torque towards object.

Well see it as. When you sit with 2 people at a table. Lay a pencil on the table. Point it to yourself. And give it a little push so it rotates towards the other person.

Or: take all torque of an object, give it a new torque, with as result that the object will rotate from the current angle towards the given angle.

Of course, if you give it a big torque, it will take less time to reach the given angel.

14/ Add torque towards angle.
Well, suppose you hit the pencil to hard. Its about to rotate more then you intended. Risking to face back to you. And you give it a little push, to correct its rotation towards the other person.
Thats "add torque towards an angle".

15 Set velocity.
Imagine. A car toy in the middle of the room. You give it a push to make it move. Then you disappear from the room.
I enter the room, and there is no way for me to know what force you used to make the toy move.
I can not know what torque you used to make it rotate.

But i see the result. The toy is moving, rotating, accelerating, de-accelerating.

What i see, the result = the velocity.

I know when the car is going slower, when i compare its velocity on different moments by eye. When its velocity is less then it was a moment ago, its going slower. If the toy is moving away from me, and i put myself as reference to this world, the velocity will be positive. If the toy is moving towards me, it will be negative.

Velocity = speed ? Yes and no. When working with physics, objects mostly do not have a constant speed. But yes, if we make the moment that we measure very very small in time, it comes close to measuring speed. So yes velocity gives a good indication for speed. And by comparing velocity measured over moments in time, we can know if the object is slowing down or going faster.

Velocity, just like force, has a X and Y component. Maybe a little picture is on its place here.



We can set in an action the X and Y velocity components that combined make up the way, the speed, the direction.. the object is moving. Combine negative and positive X and Y components allow us to describe any direction , any speed.

So we only need to know how to set the velocity and forget about torque and force ?

No sir. After all a Physics system is meant to simulate real world forces. In the real world you push an object to make it move (add a force to it). The object will start moving depending on its mass, depending on if and how it was moving already, depending on its physics. It will build up speed, and lose speed depending on the damping (linear damping).

Velocity is cold math. Changing velocity will instantly change the objects movement. Not counting in forces that was already there. Not counting in inertia. That is impossible in the real world.

16/set angular velocity.
More of the same to make an object rotate.

Next post about physic's properties that can be used in an expression.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 6:08 pm

The Physics world is a box in the box. Its not aware about the events. And the events are not much aware of the physics world. I will post a .cap a little later to prove that statement.

To make things work, we will have to talk straight to the Physics world in our events. To make decisions we will need to retrieve info from the Physics world.

We do this by accessing certain properties of the Physics world, and use them in expressions.

So lets explore the possible properties we can use in expressions.

Still with the same .cap. With Physics added to a smiley object. Go to the events editor.

Add an event, starting from the system object, choose 'compare' as condition.
In the next wizard step, the one with the parameters ...
Click in the value 1 field.
Double click the smiley object on the bottom of the wizard ...
Click on the Physics tab, where it says "select expression"
Now you have all expressions listed for the Physics world attached to the smiley object.

And that is not that much.

1/ Get mass
In the real world the mass of an object will change with its speed. I have not tested yet if this is the case in construct too. But so yes, we have to be able to get the mass of an object.

2/ Get linear damping.
Does what it says.

3/4/ Get world X and Y scale, plz post a comment to explain those to me.

5/ Get velocity X component.
Yes! We can retrieve the result of all the forces that we added to an object. And combined with the Y velocity component, we can know the objects ...
Speed ..
Direction its moving ..
If its moving at all ..
Is it speeding up ?
Is it slowing down ?

Those are the base components for all math behind objects moved by physics.

6/ Get velocity Y component.

7 Get angular component.
To know if ...
the object is rotating ..
the speed its rotating ..
the direction that it is rotating
and more

Thats all ?

Well thats a BIG world of information. And once you start to work with them, you will know that there is not much more needed to know.

Ok back to our target.

Delete all events.
Back to the layout editor ...

The smileys should have a physics behavior.
Just check the gravity switch in the behaviors properties ...
and ...

Run all.

As You see ...
They start falling. They do not fall instantly. They start falling and build up speed as they should and would do in the real world.
And they keep falling and falling.
Its a Physics simulation world.
And i happen to like it.

So lets add a ground to make them bounce off from. Where i start next post.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 6:39 pm

Download this and fire it up.
download/file.php?id=101

Look at the layout.

I only added Box objects all around the layout.
You would expect that i made the box objects solid. But nope, not needed for physics to work.

BUT, i gave also the Box objects a physics behavior. And made them immovable in there physics properties.

And thats all to set up a closed Physics world. Glance at the events editor, no events !, no programming. (look moms no hands)

Yet, Run all ..

And .. admit that this is actual a very nice simulation.

But now, to make you understand that it indeed is a box in a box.

Go to the events editor...

Add an event from the system condition "Every milliseconds".
Give it a value 7, witch is each 7 seconds

Give that event an action, setting the smiley objects Y at 100.

So every 7 seconds the objects jump to Y = 100.

Run all.

The first time the objects fall as expected.

The second time they dont fall at all.
They are physical dead. Well all there potential energy and there velocity's came to rest while bouncing of the ground and returning to the ground.
Physical they came to a rest point.

And moving them by an event, just setting there Y's is NOT interacting with the physics world.

Plz keep that in mind !
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 7:48 pm

OK.
Allow me to repeat the targets

[quote:2fzyxecm]I want the smiley's to fall by Gravity.
When they are falling i want them to smile.
When they are climbing i want to them to "ohhhhh!".
When they bounce i want them to stick out there tongues.

I want them to bounce on a ground, and during the bounce to act more as plastic balls, including seizing there shapes a bit, and getting a little jump up.[/quote:2fzyxecm]

Everything that i want to do is related to what the objects are doing in the Y direction.

(yes i had a reason to simplify this)

But, we lack support for conditions related to the physics behavior. There is no condition that goes like "on object falling". There is no "compare xxxxx" condition to pick 1 of the instances based on one of the behaviors properties.

But.
I know that i can read out the Y velocity component in the behaviors expressions.

I just do not know yet what values this expression has in certain situations.
Lets set up a sorta kinda laboratory to watch the Y velocity change, and make conclusions.

But, since we deal with instances, and those instances fall from a different hight. I have this suspicion that the Y component will be different for each object.

Yet we have no pick condition to isolate an object and retrieve its expressions for the Physics behavior.

Oh do not worry, the solution is pretty simple. Look at this .cap

download/file.php?id=103

In the layout you see 3 text boxes. 1 for each object to write there Y velocity components in.

I know that i made the objects from the left to the right. Due this, a "For each loop" will walk trough the instances from the left to the right. We have the excellent pick condition "compare private variable". So all we need to do is loop trough the instances with the "for each object" loop, set a private variable to the Y velocity component. And now we can pick instances based on that private variable. In other words: we convert the flow condition, that we could make by comparing to an expression, to a usable pick condition, by storing the expression in a private variable and pick that object based on the private variable. Elegant ? no ?

But, i am not that far yet. I still do not know what this Y component looks like. What values is it holding in witch situations ?

So i made this lab to see what it all is about. Look at the events.

Using the For Each loop, i walk trough the instances (in event nr 3) from the left to the right. If the loop counter = 1, then the first instance is picked. And i fill its Y velocity component into the first text box (event nr 4). Same for the other 2 objects.

Then because i dont like to hit "run all" over and over, i made a little loop. (recalling the layout made me crash). So in event nr 7 i use the same for each loop every 7 seconds. I place each object at different heights. And i give it a down force to bring them out of rest.

Now i can watch the objects. And figure out what exactly that Y component is standing for, and what values it actually is holding.

Now when i ran this Cap for the first time, the Y component had floating numbers, and to be honest that did not tell me much. Especially because very small numbers show as a bunch of numbers with an e at the end. Its impossible to recognize a zero, or any number for that matter. To overcome this, i rounded the numbers, as you can see in the actions that write in the text boxes.

Now.
Run the Cap. And watch yourself. Make conclusions.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 8:30 pm

Conclusions:

When the Y velocity component = positive, then the object is falling.
When the Y velocity component = negative, then the object is climbing.

When the Y velocity is big, then it has more speed.
Smaller numbers indicate a lower speed.

When the Y Velocity is zero, then the object is in rest.

Well zero ? I rounded the numbers. Meaning, zero in this readings mean, a number between 0.5 and -0.5, since i rounded the numbers.

Take the round out the actions, and you will see that even when the object is in rest there is a small Y velocity component left. Will it ever be zero ? i dont know.

So lets test if an object with a 0.5 Y velocity still moves, can we tolerate that error ?

So set up a new direct X game. Add any object to the layout. Give it a Physics behavior. Dont switch the gravity on. Add an always in the events. Add an action to that, setting the Y velocity component to 0.5. And ? yes it moves. 0.5 is a to big an error. We will have to get around that.
And we will.

But lets start with what we learned already.

Positive numbers means to object goes up. Negative means it goes down.
Translated to events and actions that address this to the animations, go like this.

download/file.php?id=104

I left the events that make it all run over and over. Ignore the every 7 seconds event.
And i should have deleted the text boxes, sorry ? anyway.

The heart of it are event 3, 4 and 5.

In event 3, i make the For each loop. To store the Y velocity component in a private variable.

This done, i can pick the objects based on that private variable.

So in event 4, i pick every instance who's private variable 'Y_component' is negative. And in the action, that only acts on the objects picked, i bring up the right animation (in this case just a 1 frame animation).

And in event 5, i pick the instances who's 'Y_component' is positive. And those i let play the other animation in the action.

3 lines, to do something in fact very very complicated.

Run all.

Yes this works perfect. But i do notice also that i will have to think about the rest moments. Because the Y component comes close to zero in rest, but seems to have always a little number left, positive or negative. Based on how i see those animations change at the rest points.

Still we have like 1/3th of the mission accomplished. And that makes me happy enough for this moment.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Wed Jul 09, 2008 10:11 pm

The last cap in this topic is this one.

download/file.php?id=106

But i did't manage to make all my targets.
[quote:3upt614e]including seizing there shapes a bit[/quote:3upt614e]

At the moment i am a little frustrated, because, it looks like ....

Change the seize of an object in physics, will mess up its physics system.

Still hope i helped some one with writing all this.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Sat Jul 12, 2008 12:15 pm

I should not have left it at that point.

Its of course simple to counter that with a little animation.

download/file.php?id=114
B
3
S
2
G
4
Posts: 322
Reputation: 2,119


Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 1 guest