Introduction
[color=#ff0000][b]Important: [/b][/color]This book is under construction. Apologies for the inconvenience. [br][br]GeoGebra has a set of basic tools to make applets with different kinds of geometrical constructions, calculations or animations in a simple way, which is ideal for most of the cases. However, if you want to create more sophisticated applets, you can use a very powerful feature known as [b]Scripting[/b].[br][br]In this GeoGebra book we will show how to create applets using [b]scripting[/b] and we might examine topics that come from physics and mathematics, but it won’t be our job to investigate these topics with a particularly high level of academic rigor. Instead, we're going to glance at those concepts and grab the parts that we need in the service of building a particular example. [br][br]In particular, I hope that the content of this book will help you, not only to understand how [b]scripting[/b] works, but also to appreciate the advantages of using this method for creating applets (worksheets) in GeoGebra.
Linear motion in two dimensions
Moving a ball in 2 dimensions
Our goal here is to implement linear motion in two dimensions to an object. We will create a ball moving in a linear path with a given speed. In order to do this, we need to know: [br][list=1][*]the location of the ball, and [br][/*][*]how fast we want it to move.[br][/*][/list][br]So let's begin by defining its location as a vector with components:[br][br][code]xlocation = 2[br]ylocation = 2[br][br][/code]We need to be able to change these values by dragging with the mouse, so we define the point O as follows:[br][br][code]O = (xlocation, ylocation)[/code][br][br]Let's create our the ball, that is, a circle with centre O and radius 1:[br][br][code]c = Circle(O, 1)[/code][br][br]Now, how fast is it going to move? We need to define the components of the vector velocity:[br][br][code]xvel = -0.03[br]yvel = 0.08[br][br][br][/code]These scripts have to be added in the [i]Setup[/i] button. See picture below.
The following applet contains the [i]Setup[/i] button with the scripts. Click on the button[i], [/i]and then drag the centre of the circle to change its position.
Of course, this circle moves only when you drag it around, but we would like GeoGebra to move it automatically. In other words, we would like to update automatically the values of its location considering its velocity. [br][br]A way to do this is by implementing the following equation:[br][br][center][i]loc[/i][i][i]ation[/i] = [i]location[/i] + [i]velocity[/i][/i][/center]How can we do this? Well, we need to add in our [i]Setup[/i] button another line:[br][br][code]Run = Slider(-5, 5, 0.01, 1, 140, false, true, false, false)[/code]
We will use this slider to update the location of the ball. Click again on the [i]Setup [/i]button to create the slider. Then open the [i]Object Properties[/i] of the slider.[code][br][/code]
Select the [i]Scripting[/i] tab and click the [i]On Update[/i] tab. There we can add the following:[br][br][code]SetValue(xlocation, xlocation + xvel)[br]SetValue(ylocation, ylocation + yvel)[br][br][br][/code]The next applet shows the result. Drag the slider or click on the [i]Play[/i] button on the left-side screen next to the slider. Notice also that the scale of the [i]Graphics View[/i] was changed.
As you can see, we have a little problem: the ball goes off screen very quickly. The only way to bring it back is by clicking on the [i]Setup[/i] button again. How can we restrict the location of the ball? For solving this problem, we need to define a region where the ball is allowed to move. For example, we want it to move only inside the square [-10, 10]x[10, 10].[br][br]Thus we need to add the following two lines to the slider:[br][br][code]If( x(O) > 10, SetValue( xlocation, -10 ), If( x(O) < -10, SetValue( xlocation, 10 ) ) )[br]If( y(O) > 10, SetValue( ylocation, -10 ), If( y(O) < -10, SetValue( ylocation, 10 ) ) )[br][/code][br]
Now the location of the ball is restricted to the square [-10, 10]x[-10, 10]. Drag the slider in the following applet to observe the behaviour of the ball or click the [i]Play[/i] button (left-side of the screen) to animate the slider.
Great! We have constructed a ball that moves in a linear path with a given velocity and only inside the region [-10, 10]x[-10, 10].[br][br]Finally, we can add some colour and format so the applet has a better presentation. The applet below shows an example where I added a polygon with inverse filling and a [i]boolean value [/i]([i]a = true[/i]) with the script [i]On Click[/i] tab:[br][br][code]StartAnimation(Run, a)[/code]
Final result
If you prefer, you can download this file to see all the scripts [url=https://www.geogebra.org/material/show/id/cv5wymhs#]Basic motion: Final result[/url]
To Recap
[b]1.[/b] We implemented linear motion in 2 dimensions with a given velocity. The commands used were:[br][list][*][url=https://wiki.geogebra.org/en/Circle_Command]Circle[/url][br][/*][*][url=https://wiki.geogebra.org/en/Slider_Command]Slider[/url][br][/*][*][url=https://wiki.geogebra.org/en/SetValue_Command]SetValue[/url][br][/*][*][url=https://wiki.geogebra.org/en/If_Command]If[/url] [br][/*][*][url=https://wiki.geogebra.org/en/StartAnimation%20Command]StartAnimation[/url][br][/*][/list][br][b]2.[/b] The[i] Setup [/i]button contains the following scripts [i]On Click[/i]:[br][br][code]#Location[br]xlocation = 2[br]ylocation = 2[br][br]#Point location[br]O = (xlocation, ylocation)[br][br]#Define ball[br]c = Circle(O, 1)[br][br]#Velocity[br]xvel = -0.03[br]yvel = 0.08[br][br]#Slider[br]Run=Slider(-5, 5, 0.01, 1, 140, false, true, false, false)[br][br][/code][br][b]3.[/b] The slider Run contains the following scripts [i]On Update[/i]:[br][br][code]#Update location[br]SetValue(xlocation, xlocation + xvel)[br]SetValue(ylocation, ylocation + yvel)[br][br]#Check edges[br]If( x(O)>10, SetValue( xlocation, -10 ), If( x(O)<-10, SetValue( xlocation, 10) ) )[br]If( y(O)>10, SetValue( ylocation, -10 ), If( y(O)<-10, SetValue( ylocation, 10) ) )[br][/code]
Exercise
Notice that in our example the location is always the same and we have a fixed velocity. Try implementing the above example with:[br][list][*]a random location (in a given region) of the ball, and[/*][*]a velocity that can be adjusted by the user.[/*][/list]You can also try to change the region where the ball is defined.