Newton`s Lab - Jeffgold.net

Download Report

Transcript Newton`s Lab - Jeffgold.net

Newton’s Lab
Games and Simulations
O-O Programming in Java
The Walker School
The Walker School – Games and Simulations - 2010
Space, The Final Frontier
You can create new
bodies.
What kinds of bodies are
in our solar system?
Why don’t we see a class
for each type?
The Walker School – Games and Simulations - 2010
Solar System
• The main types of bodies in our solar system
contains planets, planetoids, moons, asteroids
and comets. While each of these varies in size,
mass, composition and velocity, they are also
similar in many ways. Because each object does
have a size, mass, and velocity, we can use
abstraction in programming to create a Body
Class that handles each of these items as
variables.
The Walker School – Games and Simulations - 2010
3 Basic Methods
Right-click on Space world at the top?
What happens when you activate one of these methods?
The Walker School – Games and Simulations - 2010
Public Methods
Right – click on a body.
What public methods does it
have?
The Walker School – Games and Simulations - 2010
Inspecting Planet Properties
Right click on a
planet and inspect
its properties.
What is the mass?
The Walker School – Games and Simulations - 2010
Space Public Methods
What do the
numbers mean in
each case?
The Walker School – Games and Simulations - 2010
Helper Classes
Also known as
Abstract classes.
They do not have
constructors, so you
can’t create instances
of them.
2 Support Classes
SmoothMover
Vector
The Walker School – Games and Simulations - 2010
Helper (or Support) Classes
http://www.greenfoot.org/programming/classes.html
Some Helper Classes
•Counter
•Explosion
•Mover
•Plotter
•Rotator
•Slider
•Vector
•Wander
The Walker School – Games and Simulations - 2010
Classes Inherited from SmoothMover
How are these 2 classes
different?
What classes are inherited from Vector?
Why is it not under Actor?
The Walker School – Games and Simulations - 2010
Overloading
Can have the same name, as
long as their parameters are
different. This means that the
methods (or constructors)
have different signatures.
Has 2 constructers “Body” with different parameters.
The Walker School – Games and Simulations - 2010
Vectors
dy
dx
Polar Representation = length and direction
Cartesian Representation = dx and dy
The Walker School – Games and Simulations - 2010
Vector Class Variables
Open SmoothMover and Vector.
What methods do they have?
The Walker School – Games and Simulations - 2010
Method Inheritance
Open SmoothMover and Vector.
Which methods can you call from
the object menu?
Which can’t you call?
The Walker School – Games and Simulations - 2010
Default Constructors
Does not have any parameters.
Makes it easy to create bodies
interactively without having to
specify details. This is a default
constructor.
Is this an example of overloading?
The Walker School – Games and Simulations - 2010
this.
Only possible in constructors. Used to
execute another constructor.
What happens when you
remove this. in front of the
word mass? Does it compile?
The Walker School – Games and Simulations - 2010
Constants
Means the value never changes.
Means it’s shared between all actors of this class.
These variables are constants. These
variables do not change in a program.
The Walker School – Games and Simulations - 2010
Add Movement
The Walker School – Games and Simulations - 2010
Body Behavior
Create multiple bodies.
How do they behave?
The Walker School – Games and Simulations - 2010
Add Movement (Left)
How would you make the body move left?
The Walker School – Games and Simulations - 2010
Solution: Left Movement
Change the + to a -
The Walker School – Games and Simulations - 2010
Java Packages
Programmers typically use packages to organize classes
belonging to the same category or providing similar
functionality.
import java.awt.Color;
Imported using dot notation.
The Walker School – Games and Simulations - 2010
Importing Java Color Library
The Walker School – Games and Simulations - 2010
Default Color of Body
Sets the RBG values
Calls the Color class.
What is the legal range for colors?
The Walker School – Games and Simulations - 2010
Adding Gravitational Force
The Walker School – Games and Simulations - 2010
Movement
The Walker School – Games and Simulations - 2010
Apply Force To Do List
Apply forces from other bodies:
get all other bodies in space;
for each of those bodies:{
apply gravity from that body to our own;
}
The Walker School – Games and Simulations - 2010
Apply Force – Part I
Methods only intended to be called
from within the class can be labeled
private. When methods are intended
to be called from outside the class,
they should be labeled public.
The Walker School – Games and Simulations - 2010
Greenfoot World Methods
Which methods give us access to
objects within the world?
The Walker School – Games and Simulations - 2010
Getting Objects
• Java.util.List getObjects(java.lang.Class cls)
– Gives a list of all objects in the world of a particular class.
• getObjects(Body.class)
– Calls the objects in the world.
• getObjects(null)
– Keyword null is a special expression that means nothing, or no object.
• getWorld().getObjects(Body.class)
– Can be used from an actor to get all objects of class Body.
The Walker School – Games and Simulations - 2010
Java.util.list
What methods can be used to add
items to a list?
What methods can be used to remove
items to a list?
What methods can be used to found
out how many items are in a list?
The Walker School – Games and Simulations - 2010
Lists are not Classes, but Interfaces
generic type – need to put the type of list in the brackets,
such as String.
The Walker School – Games and Simulations - 2010
Apply Force – Part II
The Walker School – Games and Simulations - 2010
For Loops
Helps to step through the items in a collection, or list.
For (ElementType variable : collection) {
statements;
}
The Walker School – Games and Simulations - 2010
Apply Force – Part III
Refers to the current object.
Use an if-statement so that gravity is applied
to all objects (bodies) in the list except the
current object.
The Walker School – Games and Simulations - 2010
Applying Gravity
The Walker School – Games and Simulations - 2010
Newton’s Law
Newton's Law of Universal Gravitation states that every
massive particle in the universe attracts every other massive
particle with a force which is directly proportional to the
product of their masses and inversely proportional to the
square of the distance between them.
The Walker School – Games and Simulations - 2010
Apply Gravity To Do List
Why do these numbers need to be doubles?
Apply gravity from an object {
get the location of the objects in space;
calculate the distance between each of the objects;
create a new vector using this data to determine direction;
calculate the distance between the two bodies using the
Pythagoras theorem;
use Newton's formula to calculate the strength of the force
calculate the acceleration;
add the force;
}
The Walker School – Games and Simulations - 2010
Pythagorean Theorem
Look up the class Math in the Java API. How many parameters
does the sqrt method have?
The Walker School – Games and Simulations - 2010
Square Root
Now, find the method that can be used to find the maximum of
two integers. What is it called?
The Walker School – Games and Simulations - 2010
Acceleration
Acceleration = Force / Mass
The Walker School – Games and Simulations - 2010
Appling Gravity Method
The Walker School – Games and Simulations - 2010
Trying it Out
Try out the three initializing methods from
Space object again. What do you observe?
The Walker School – Games and Simulations - 2010
Experimenting with Gravity
Change the gravity constant at the top of the body
class. What happens?
Change it by ½, change it by doubling it; change it by 1,
change it by a decimal.
The Walker School – Games and Simulations - 2010
Experimenting with Mass & Movement
Experiment with changes to the mass and/or initial
movement of the bodies defined in the Body class.
What happens?
size mass
vector
movement
r
g
b
x
When experimenting it’s important to change one
variable at a time.
The Walker School – Games and Simulations - 2010
y
Programming / Research Challenge
• Create some new set-ups of stars and planets
and see how they interact. Can you come up
with a system that is stable? With what you
have programmed, could you create a model
of our solar system? Is our solar systems
stable? Explain your reasoning.
The Walker School – Games and Simulations - 2010
Obstacles, Gravity and Music
The Walker School – Games and Simulations - 2010
Improvements to Lab v. 3
Improvements
•new Obstacle class
•modified Body class
•increased Gravity
•fixed obstacles
•more planets
The Walker School – Games and Simulations - 2010
Add Obstacle Class
Directions
1. Right click on Actor
2. Add new subClass
3. Name it “Obstacle”
4. Choose the orange block from
“Objects”
The Walker School – Games and Simulations - 2010
Create Class Stub
What are the basic methods we need to add to our stub?
The Walker School – Games and Simulations - 2010
Create a Constructor
What parameters do we want to pass into the constructor?
The Walker School – Games and Simulations - 2010
Add Parameters to Constructor
1. We want to know when a body hits the obstacle.
2. When it does we want to play a sound.
The Walker School – Games and Simulations - 2010
Add Play Sound Method
Use Audacity to create a sound that you want
to play when the planet hits the obstacle.
What folder in the project, does this sound go?
The Walker School – Games and Simulations - 2010
Add an Act Method Stub
The Walker School – Games and Simulations - 2010
Adding Obstacles
What type of data structure is this?
The Walker School – Games and Simulations - 2010
Act To Do List
public void act {
call the intersecting object method from Actor
use a conditional statement to determine if the
object has collided with the obstacle
if touched,
set the appropriate image
play sound
if not touched,
set the appropriate image
}
The Walker School – Games and Simulations - 2010
Functioning Act Method
The Walker School – Games and Simulations - 2010
Increase the Gravity Constant
Gravity has been added to make the “bodies”
move faster. What functionality has been
added to slow them down?
The Walker School – Games and Simulations - 2010
Slowing Fast Bodies
The Walker School – Games and Simulations - 2010
Create Random Bodies Stub
What parameter(s) do we want to pass in order
to create a number of bodies?
Don’t forget to call this method in the act
method.
The Walker School – Games and Simulations - 2010
Random Bodies To Do List
write a method stub{
create a loop
make a body of random size
make a body of random mass (size * #?)
give the body a random direction
give the body a random speed
give the body a random location (within the world)
give the body a random color (r, g, b)
create a new instance of the body (pass the parameters)
}
The Walker School – Games and Simulations - 2010
Create Random Bodies Method
The Walker School – Games and Simulations - 2010
Bouncing Bodies To Do List
write a method stub{
get the body’s location
determine if the body is at the edge of the world
cast the location to decimals (for movement)
change the direction of movement, either in
vertical or horizontal plane
decelerate the body
}
The Walker School – Games and Simulations - 2010
Reflecting Bodies at World Edge
Don’t forget to add the bounceAtEdge() method call in
act() method.
The Walker School – Games and Simulations - 2010
Activities – Part I
• Change the number of bodies that are
created by default in this scenario.
• Create a different arrangement of obstacles
in your scenario.
• Use different sounds (sound files) for your
obstacles.
The Walker School – Games and Simulations - 2010
Changing the Number of Bodies
Under Space class change the
number of random bodies
created.
The Walker School – Games and Simulations - 2010
Different Obstacle Arrangement
Creates a random location along they y-axis.
The Walker School – Games and Simulations - 2010
Fixed Obstacle Arrangement
The Walker School – Games and Simulations - 2010
Different Sounds
Changes the set of sounds from piano to trumpet sounds.
These 1-minute notes were recorded by band members in
Audacity then stored in the project’s sound folder. We added
the prefix of “tp” so we didn’t have to change any of the names
in the soundFiles [] list.
The Walker School – Games and Simulations - 2010
Activities – Part II
• Use different images for your obstacles.
• Make planets change color every time
the bounce off the edge of the universe.
• Make planets change color every time
they hit an obstacle.
• Make a different kind of obstacle that
gets switched on and off by being hit.
When on, it continuously blinks and
produces a sound at fixed intervals.
The Walker School – Games and Simulations - 2010
Change the Obstacle Image
Directions
1. Right-click on Obstacle class
2. Choose Set-Image
3. Choose the new image
from the Greenfoot image
library.
Make your own image in
Photoshop and add it to the
image folder for the project.
The Walker School – Games and Simulations - 2010
Programming Challenge
• Comets and asteroids move through the solar
system. Create an image for one of these
using Photoshop and add it to the world as an
obstacle. To increase the difficulty of the
challenge, have the object appear periodically
at different speeds and if a ball hits this
object, have it make a special sound.
The Walker School – Games and Simulations - 2010
Activities – Part III
• Add some keyboard control. For
example, pressing the right arrow key
could add a small force to the right to all
Body objects.
• Allow adding more planets. A mouse
click into the universe while it is running
should create a new planet at that
location.
The Walker School – Games and Simulations - 2010