Page 1 of 2

real physics on balls?

PostPosted: Mon May 08, 2006 7:29 pm
by sali11
Hi all

Im new to Game Editor, Iv been looking at the tutorials and demos.
one demo iv been looking at is the mini golf game. Im trying to learn how to apply real physics to a ball so when it is hit it gradually slows down.

When I look at all the scripts assigned to the ball in this game I cant seem to see anything that controls this. Can any one tell me which script is used to controle that aspect of the ball?

or even show a good tutorial on how to apply real physics to balls so that when they collied with each other they dont just keep bouncing off every thing for ever. thanks for any help any one can provide, or pointers on where to learn it. :wink:

PostPosted: Mon May 08, 2006 8:00 pm
by makslane
In a 'Draw Actor' event, put this script:

directional_velocity *= .9;

PostPosted: Mon May 08, 2006 8:13 pm
by Fuzzy
The secret that you seek is hidden in the tutorials, but it is wearing a disguise!

What you are looking at is a situation that is the complete opposite of jumping. When you jump, you fly up, then start falling, faster and faster, until you hit. What you want is to go slower and slower, so you need to use the complete opposite of the falling technique.

I dont know how clear I was...

You need two variables. One is the velocity or directional velocity that is built into GE, and the other is an acceleration, a rate at which you are speeding up, or slowing down.

In physics, this is sometimes called Delta, a measure of change in speed. Thats short, so use that, unless you can think of something more descriptive. Each actor(ball) is going to need its own Delta, so make it an actor variable. Use a floating point or real number.

In the collision event for the ball, set Delta to be 1.0

Delta = 1.0;

Now, each time you draw the actor(its draw event) set the directionalvelocity to its self, multiplied by the Delta.

directionalvelocity = directionalvelocity * Delta;

Right after that, make Delta a little smaller, but multiplying it by its self and a number just under 1.0.

Delta = Delta * 0.95;

Play with that 0.95 to get things balanced so the ball slows at the right rate. you might make a global variable and call it friction, and use that for the 0.95.

If you set friction to over 1.0, the actor will accelerate.

questions?

PostPosted: Tue May 09, 2006 12:57 pm
by sali11
Thank you both for your replyes.

ThreeFingerPete, every thing you said sounds very complicated. I dont suppose you have any good tutorials that demonstrate what you just said? I will have a go at it any way when I get home.

one more thing thow is i want to be able to make the que ball so that I can have a power bar to set the strenght the ball is hit, just like every pool game you have come accross. any ideas on where I might learn to apply that to a ball, once i have this sorted on it?

thanks again for your help :)

PostPosted: Tue May 09, 2006 3:18 pm
by Fuzzy
how about a working example?

use this picture.
Image

Make new project. Make an actor, and call it ball. Use that graphic for an animation.

click add event.

click key down.

press the space bar.

click repeat: enable and select disable.

click add action.

click script editor.

type this into the script editor:

directional_velocity = 10;

click add at the bottom. click immediate action. click close.

click add event.

select draw actor.

select script editor.

type this:

directional_velocity = directional_velocity * 0.95;

click add at the bottom. click immediate action. click close.

click on game mode at the top of the window. press spacebar.

thats it.

PostPosted: Tue May 09, 2006 4:13 pm
by sali11
Cool, thanks for that, as soon as i get home im gona try it out. thanks allot for your help man, :D

PostPosted: Tue May 09, 2006 5:51 pm
by Just4Fun
Very helpful example. That shows direct application in GE. Thanks for posting it...

PostPosted: Tue May 09, 2006 9:27 pm
by sali11
Hi ThreeFingerPete I got it to work! its very cool, i put a wall around it and made it collied with it so now at the moment the ball bounces off the wall when it hits it.

I was wondering if there is a way to change this code so that I can use the arrow keys up and down to increase or decreas the speed i want the ball to go, and then when I have got the speed i want, then hit the space bar to make the ball go at that speed.

maybe even have a power bar to represent the speed that the ball has been set for. that would be cool.

Iv been trying to understand how the physics on the ball works in this script.

when you put this:

directional_velocity = 10;

you gave the ball the starting speed of 10 when the space bar is hit.

then the code:

directional_velocity = directional_velocity * 0.95;

im thinking that this then slows the ball down by multiplying 0.95 to the velocity which was 10 untill it stops? that * dose represent multiply right? and if it is multiplying it by 0.95 how cum that slows it down? sorry about the noobe questions. I just want to understand the code, not just do a copy and past job.

could you explain this in noobe terms please? thanks again :)

PostPosted: Tue May 09, 2006 10:15 pm
by Fuzzy
Nono. Never apologise for a question!

You are catching on fast! Yes, * means multiply, and it causes the ball to slow down because that formula is applied each time the ball is drawn, probably 30 times per second.

So when you hit the ball, its going 10, then next draw, its going 9(it rounds down from 9.5), the following draw it hits a speed of 8(8.55 really), followed by 7, 6, 5..... until it stops.

It makes me happy to see that you want to understand. So many just want help(as in "do this for me"). Ask any questions you want.

Power bars are a little tricky at first, so I'll show you how to display the power as a number. You wil be able to influence it with the up/down arrow keys too.

make an actor. Call it Display.

dont assign a animation.

Over on the right side of the actor control menu, find 'text' and press it. type 0000 in the grey box. Thats four zeros, no letter O.

Click new font and true type. Now click on the box beside file and find ariel.ttf in the list. It should be close to the start. If you dont have it, pick a different one. It doesnt matter.

Change size 32 to 64. You want nice big letters. Just leave the rest alone, and click ok.

Hit ok again, and you should see your four large white zeros. Move them up to the corner of the view(white border area). Your actor may be hidden behind the edit menu, just move it if so.

now, click add event then click key down.

press the up arrow key, and add action.

go to script editor, and click that. in the script editor, type

textNumber = textNumber + 1;

make sure you use the capital N, thats important.

hit add, and select immediate action.

it takes you back to the keypress event. hit the clear button, and then press down arrow. click add action, and then script editor. in this script, you do the same, except use - instead of +

textNumber = textNumber + 1;
Hit add, immediate action, then close.

press game mode and test. try using up and down arrow keys. the numbers should change.

now we need to add that speed to the ball. Its pretty easy.

In the actor edit menu, at the top is name: and in a box, it says Display. click that and find your ball.

click edit event, and select the key press (space), and then edit action. that should take you to the script editor and show the script that make the ball start moving.

Change it to say:

directional_velocity = Display.textNumber;

and now hit add, immediate.

test it out....

Let me know when you have that working, and then I have a way for you to check what you have learned.

PostPosted: Wed May 10, 2006 9:00 am
by sali11
cool, thanks for your help, sounds quite easy to apply.

I have some programing experience in java, HTML and Flash action scripts so understanding the scripts is not too much off a problem. However my math is not so hot, so thats when I get stuck.

thanks for your help in this. Im learning allot from you. Ill try this out when I get home and then let you know how it went.

:D

PostPosted: Wed May 10, 2006 5:18 pm
by Fuzzy
ah. if you have some programming experience, then sooner or later things are going to 'click' with you. After that, its just a matter of asking questions about formulas.

PostPosted: Wed May 10, 2006 7:53 pm
by sali11
I hope so. Iv applyed the new codeing and it works, I have been testing it and found a few bugs in it, it may be due to the Game Properties.

the game frame rate is 30 fps
safe margin is 20 (dont know what this is for)

some times when the ball hits the wall, it gets stuck on it, when I hit the space bar again, it gose straight through the wall.Some times it just gets stuck and wont move no matter how many times i hit the space bar. Some times if the ball is going very fast it passes through the wall. I think I may have to change something in the collision settings of the wall.

I understand this code, straight forward stuff. I gess its gona get complicated when I try to draw a power bar in relation to the numbers right :? lol

Another thing I found is that if I press the down key first and give the speed -12 the ball ends up going in the opposit direction. I think an If statment might be used to make sure the power wont go into the - before the ball is hit. am I hot or cold?

If Display.textNumber = < 0000
Display.textNumber = 0000

or sumthing in that direction.

PostPosted: Wed May 10, 2006 8:24 pm
by Fuzzy
Thats right, an IF will help with that.

Regarding the ball sticking and/or passing through... Thats what happens when the velocity is too high.

Imagine that, unlike the real world, an actor or moving element in programming doesnt pass through each pixel on its journey. Instead, GE(in this case), looks at the speed, figures out where the ball will sit next time its drawn, and then checks that area for a collision.

So if you have a ball and a wall...
Code: Select all
O   ||


...and the ball is going 10 velocity units(pixels), and the wall is 5 pixels away, after the screen updates you will see...
Code: Select all
    ||        O


...the ball almost warps right past and never collides at all. Likewise, a near collision can result in stuck actor. You have to learn to anticipate and prepare for such a happening. Its usually pretty easy to do. Just make the walls thicker than the highest possible speed.

PostPosted: Thu May 11, 2006 3:19 am
by Fuzzy
The 20 safe margin is an area around the view, outside of sight, where events still happen. it allows for actors that are partly off screen to still effect the game. You should set your safe margin to 1/2 of the size of the largest actor animation that could be partly in view, if at all possible. Its going to have a large effect on lag, so experiment carefully.

PostPosted: Thu May 11, 2006 8:39 am
by sali11
Ok cool. If I was to make the walls thicker, the game board will look odd. is there a way of applying a mask over it like in flash so you only see the thickness, that you want the players to see?

For the IF statement to prevent any - speeds, can i apply that in the same script window for the down button or would I create a new script for it. allso what is the correct way to write scripts like that in GE?

is it like this:

IF {
Display.textNumber = < 0000;
Display.textNumber = 0000;
}

thats probably completly wrong, is that script going to do what I want it to do?

ohh and if i was to limit the speed to two digits ie 00 instead of 0000 it should still work shouldent it? well i can test that out when i get home.

thanks for all your help in teaching me all this stuff. :D