FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Teaching
   Theory and Practice
(Moderators: REAS, arielm)
   Tutorial: Simple Motion - Simple OOP
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Tutorial: Simple Motion - Simple OOP  (Read 10645 times)
arielm


WWW
Tutorial: Simple Motion - Simple OOP
« on: Oct 8th, 2003, 3:31pm »

http://www.processing.co.il/tutorials/motion_oop/
 
OOP stands for Object-Oriented Programming, a central feature present in Java, Processing's core language. But what is the relation between motion and OOP?
 
In short, this relation is not obvious when controlling the motion of a single element on screen, but it becomes clearer when we want to handle an arbitrary number of elements that share a common behavior: example given.
 
---
 
- If you have some questions regarding this tutorial, please post a reply to this thread.
 
- If you go over the tutorial and find it too hard (or too trivial), I'd like to hear your feedback.
 
- Finally, if someone feels like helping to raise the quality of the english used, it will be just great!
« Last Edit: Oct 27th, 2003, 9:53pm by arielm »  

Ariel Malka | www.chronotext.org
REAS


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #1 on: Nov 10th, 2003, 7:50am »

I'm not sure how other's feel, but teaching OOP without constructors seems problematic. It is poor form (a bad habit that could cause trouble in the future) to directly access data from an object and your examples teach this immediately without qualification. That being said, this is a very helpful tutorial and I'm going to use it as the base for a lesson tomorrow.
 
+ Casey
 
arielm


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #2 on: Nov 10th, 2003, 1:35pm »

yeah, it was a tactical decision not to treat constructors at this point: i've seen some java tutorials on OOP and they'all mention constructors and inheritance from the very beginning, but i doubt about their effectiveness for "beginners"...
 
my goal was not to prepare the people for long-life-oo, but rather put them on tracks without too much cognitive overload and using ecological thinking whenever it was possible.
 
i agree that ideally, this tutorial should definitely have a chapter-2 that treats what needs to be yet treated, preferably "while they're hot".
 
 
looking forward to receiving some feedback on the lesson you're going to give on OOP!
 

Ariel Malka | www.chronotext.org
mKoser

WWW Email
Re: Tutorial: Simple Motion - Simple OOP
« Reply #3 on: Nov 10th, 2003, 5:11pm »

i think it is important to explain constructors as early as possible, meerely beacuse similar functionality can be found in both ActionScript and Lingo... but, eh, yeah, it depends on what the goals of the class is - i just think if you are even going to mention OOP, it would make sense to start using constructors right away...
 
ie. children are born with yellow hair, black hair, brown hair ... but when they grow older they might all turn grey... the constrcutor would be usefull to explain the color of the hair as they are born.  
 
... eh, yeah - my 2 cents.
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
madmerv
Guest
Email
Re: Tutorial: Simple Motion - Simple OOP -- FAQ
« Reply #4 on: Nov 26th, 2003, 5:35pm »

Just to give a little vocab to this:
 
OOP (object orient programming)
 
This means Polymorphism, Inheritance and the use of Classes, Properties, Constructors and Methods.
 
It also means "Child Objects", "Parent Objects", and the distinction between public and private methods and properties.
 
Finally, the use of the term "Class Heirarchy" when in reference to Inheritance.
 
What is polymorphism?
 
Polymorphism is a theoretical concept of computer science which allows abstracted data types to take on new forms; such as the example in the programming language lingo, where every variable can be set to every other variable with few exceptions.
 
http://whatis.techtarget.com/definition/0,,sid9_gci212803,00.html
 
What is a class?
 
An object definition, similar to a typedef or struct in C.
 
EXAMPLE:
class Blah {
    int property;
 
     void constructor() {
     }
 
     int method() {
     }
}
 
What is an object?
 
An instance of a class.  
Syntactically, <object>.<property|method>
 
What is a method?
 
A function contained within a class.
 
What is a property?
 
A variable contained within a class definition.
 
What is a constructor?
 
A special function called when the object is created (in java, with a new <object>() expression) -- constructor functions sometimes take a series of parameters which tweak the creation of an object.
 
What is Inheritance and Class Heirarchy?
 
Inheritance is the ability of one object to mask its methods and properties to another "child" object.  The "parent" object is the precursor Class and its properties and methods are transferred down to the "child" object, which can then augment the parent class.
 
The existence of parent and child objects is also referred to as "Class Heirarchy"
 
What is a function "overload"?
 
An alternative mapping of the function to an alternative set of parameters.
 
What is "public" versus "private"?
 
Think of these terms as being synonymous to "internal" and "external" -- public methods and properties can be referenced by other objects, while private properties and methods can only be referenced within the class itself.
 
VOCABULARY WRAP UP:
 
Polymorphism
Class
Object
Inheritance
Class Heirarchy
Methods
Properties
Constructors
Child
Parent
Public
Private
 
Good luck!
« Last Edit: Nov 26th, 2003, 5:41pm by madmerv »  
kevinP

Email
Re: Tutorial: Simple Motion - Simple OOP
« Reply #5 on: Jan 16th, 2004, 1:58pm »

on Nov 10th, 2003, 1:35pm, arielm wrote:
yeah, it was a tactical decision not to treat constructors at this point: i've seen some java tutorials on OOP and they'all mention constructors and inheritance from the very beginning, but i doubt about their effectiveness for "beginners"...

 
Hi Ariel,
 
Thanks for the tutorial. I'm a beginner, so my comments. I think it's useful to always think of objects as things you can "contruct" and then communicate with.
 
BTW, a _really_ nice learning environment for OO (using java) is (in my beginner's opinion) BlueJ. It's not as much fun as processing, but maybe better for teaching OO: http://www.bluej.org/what/what.html
 
Back to your tutorial. I thought it would be good for me to try converting it to use a constructor. Here is what I came up with (a couple questions follow):
 
Code:

// BOUNCE 2C, OBJECT ORIENTED, N BALLS
// Tutorial by Ariel Malka | http://www.chronotext.org
 
// Modified 16 January 2004 by K Pfeiffer
// Added constructor to class Balls (I hope)
 
Ball[] myBalls;
 
int N = 50;  // how many balls we want...
 
void setup()
{
  size(200, 200);
  framerate(25);
 
  myBalls = new Ball[N];
 
  for (int i = 0; i < N; i++)
  {
    myBalls[i] = new Ball();
  }
}
 
void loop()
{
  background(0);
 
  for (int i = 0; i < N; i++)
  {
    myBalls[i].run();
  }
}
 
class Ball
{
  float x;
  float y;
  float vx;
  float vy;
  float radius;
 
  Ball()
  {
    x = random(0, width);
    y = random(0, height);
    vx = random(-7, 7);
    vy = random(-7, 7);
    radius = random(3, 9);
  }
 
  void run()
// continues...

 
I noticed that at least a couple of my Balls appear more like octagons. I don't think this was the case in your example. Maybe I messed something up (which I can't find)
 
Also, should I have passed width and height to the constructor (or not in Processing)
 
Many thanks,
 
-K
 

Kevin Pfeiffer
arielm


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #6 on: Jan 16th, 2004, 6:24pm »

hey Kevin,
 
if i would have used a constructor for this class, i would have passed all the parameters from "outside" the class, like:
Code:
// instantiation...
myBalls[i] = new Ball(random(0, width), random(0, height), random(-7, 7), random(-7, 7), random(3, 9));
 
// the constructor
Ball(float myX, float myY, float myVx, float myVy, float myRadius)
{
  x = myX;
  y = myY;
  vx = myVx;
  vy = myVy;
  radius = myRadius;
}

the difference is not very noticeable in such a simple example, but the idea is that it's generally better to separate objects' logic from the main program logic (e.g. you could use the Ball class with a different purpose in mind...)
 
if we're here already, a cleaner way (but yet prone to misunderstandings) of declaring the same constructor:
Code:
Ball(float x, float y, float vx, float vy, float radius)
{
  this.x = x;
  this.y = y;
  this.vx = vx;
  this.vy = vy;
  this.radius = radius;
}

 
concerning the "octagonality" of the circles: in my tutorial, I use p5 version 056 (because its basic .jar file weights ~50K instead of ~100K)... at this time, there was a different algorithm for drawing filled circles (i think it was slower, but it looked better at small radius sizes)
 
 
finally, thanks for pointing us to BlueJ, i was not aware of its existence... i'll take a deeper look at it!
 
did you learn java from scratch using BlueJ?
 

Ariel Malka | www.chronotext.org
kevinP

Email
Re: Tutorial: Simple Motion - Simple OOP
« Reply #7 on: Jan 16th, 2004, 8:37pm »

on Jan 16th, 2004, 6:24pm, arielm wrote:
hey Kevin,
 
if i would have used a constructor for this class, i would have passed all the parameters from "outside" the class, like:

[snipped]
 
Yeah, was my original plan, but for some reason I felt that random balls as the default was truer to your original design; and I thought I could then add an additional constructor that would override this (I think).
 
Quote:

if we're here already, a cleaner way (but yet prone to misunderstandings) of declaring the same constructor:
Code:
Ball(float x, float y, float vx, float vy, float radius)
{
  this.x = x;
  this.y = y;
  this.vx = vx;
  this.vy = vy;
  this.radius = radius;
}

 
This is what I had (I actually did write it this way the first time; I even wrote a question to go with it, which I can now ask:
 
I learned to use the "this.y = y" form, but I've also seen in the examples this:
 
Code:
Object(int _x, int _y)
{
  x = _x;
  y = _y
  // etc.

Is this just a stylistic difference
 
Quote:

concerning the "octagonality" of the circles: in my tutorial, I use p5 version 056 (because its basic .jar file weights ~50K instead of ~100K)... at this time, there was a different algorithm for drawing filled circles (i think it was slower, but it looked better at small radius sizes)

 
Yours was fine; just mine was funny; here's a screenshot at 200%:
http://timo.iu-bremen.de/~pfeiffer/processing/misc/square_ball.png
 
Look at the ball on the right. If I use smooth() it all looks beautiful. So this is just because I'm using 058
 
Quote:

finally, thanks for pointing us to BlueJ, i was not aware of its existence... i'll take a deeper look at it!
 
did you learn java from scratch using BlueJ

 
I actually learned quite a bit (for me) about OOP with Perl. Java (and more OOP) I then learned (am learning) with BlueJ.
 
-K
« Last Edit: Jan 17th, 2004, 4:23pm by kevinP »  

Kevin Pfeiffer
arielm


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #8 on: Jan 16th, 2004, 9:21pm »

re: this.x = x vs _x = x
yeah, i think it's mostly a stylistic difference.
 
re: the ball on the right in your screenshot
the "different drawing alorithm" used in p5 post ~version 56 is responsible for that (one alternative, if shape really matters: using a bitmap...)
 
re: p5 and OOP
i guess unlike "standard java" (including BlueJ), p5 is not focusing on OOP as the main metaphor... instead, a "low-level" approached is used, which seems to me as an advantage, compared to standard java, flash and the likes... (p5 brings us back a bit to the good-old "computer-graphics" school of thinking, which is the foundation for making state-of-the-art interactive works, a la reas, fry, levin, lieberman and co...)
 

Ariel Malka | www.chronotext.org
Ride215

Ride215
Re: Tutorial: Simple Motion - Simple OOP
« Reply #9 on: Apr 7th, 2004, 1:31am »

I noticed that your example uses the public variables of the ball class in the loop method. I was taught to always make those variables private and instead create a method (i.e. getX()) within the ball class that can be used instead. Is this a good habit to keep for higher level programming or is it just extra work?
« Last Edit: Apr 7th, 2004, 1:55am by Ride215 »  
arielm


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #10 on: Apr 7th, 2004, 9:15am »

well, with that tutorial, i tried to go to the point as straight as possible:
 
this is the reason i'm not using "constructors", neither "get" and "set" functions (i.e. your question...)
 
+ in general: i think that the "whatever works" school is appropriate here with processing (more freedom than with "usual" java coding)
 
ariel
 
p.s. i saw your site a few months ago and i was very impressed by the visuals, so.. nice to see you here!
 

Ariel Malka | www.chronotext.org
Ride215

Ride215
Re: Tutorial: Simple Motion - Simple OOP
« Reply #11 on: Apr 8th, 2004, 2:31am »

hehe, thanks for the compliment but that is not my site actually, that is just one of the sites that has inspired me to do web design.
 
kevinP

Email
Re: Tutorial: Simple Motion - Simple OOP
« Reply #12 on: May 15th, 2004, 11:17am »

Jumping back into an old thread...
 
on Apr 7th, 2004, 1:31am, Ride215 wrote:
I noticed that your example uses the public variables of the ball class in the loop method. I was taught to always make those variables private and instead create a method (i.e. getX()) within the ball class that can be used instead. Is this a good habit to keep for higher level programming or is it just extra work

 
I thought I had asked about this once myself, but can't find any answers via search (so far). It seems to me that Processing doesn't support these access modifiers. IOW if I label a field private, I can still access it so:  myObject.myField. Is this just temporary and accidental or is it so intended (I'm afraid that it will make me lazy, okay, lazier.) I'm guessing that when speed counts, though, that "myObject.myField" is faster than "myObject.getField()"...
 
("Happy Weekend")
 
-K
 
 
 
 

Kevin Pfeiffer
arielm


WWW
Re: Tutorial: Simple Motion - Simple OOP
« Reply #13 on: May 15th, 2004, 12:11pm »

actually processing is not doing anything special to treat "access modifiers" like "private", so it's just like any other java environment here.
 
the point is that every processing sketch (even if it's not visible) is contained within a class (that extends BApplet), so when you're defining classes in your sketch: these are always nested classes.
 
this is the reason why "private" seems to you as unsupported: in java, nested class members (fields & methods) are by default public to the class they're nested-in.
 
you'll see the same behavior if you work with a java IDE like eclipse.
 

Ariel Malka | www.chronotext.org
kevinP

Email
Re: Tutorial: Simple Motion - Simple OOP
« Reply #14 on: May 15th, 2004, 7:53pm »

Thanks Ariel! (Sorry if I had asked this before). It's easy (for me) to forget that there is a "meta-structure" in which Processing lives.
 

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »