We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › "connecting" objects together
Page Index Toggle Pages: 1
"connecting" objects together (Read 769 times)
"connecting" objects together
May 30th, 2007, 2:37pm
 
I'm going a bit crazy.

What I need to do is to create an object, say a rectangle, then create another object, say a dot, and "connect" the dot to the rectangle. The dot should "stick" to the rectangle when I move the rectangle, and also when I move the canvas. I can get one or the other to work - moving the canvas moves the rectangle and dot together (translate() solves this easily enough), or keeping the dot fixed to the rectangle when I move the rectangle - but not *both*.

The closest I've come is in a model that made the rectangle from a PGraphic, then created the dot within the rectangle. I like this approach, but the problem is that I need to be able to arbitrarily resize my rectangle, and PGraphics always gave me errors when I tried to do that using resize() - index out of bounds, or something like that.

I'm sure this is basic stuff for graphics people, but I'm an audio guy... duh... Wink

Any help greatly appreciated.

J.
Re: "connecting" objects together
Reply #1 - May 30th, 2007, 2:39pm
 
I should clarify, acutally, that I need to be able to move the "dot" on the surface of the rectangle. I need to move the dot, add more dots, remove them, and drag them around... That is, the dot will ultimately be its own class.

J.
Re: "connecting" objects together
Reply #2 - May 31st, 2007, 7:59pm
 
okay, so browsing around the documentation I found a PShape class. But it doesn't seem to be covered in much detail - or rather, there are no examples, as yet. It does, however, look like exactly what I need! Is PShape new, old, supported, unsupported? I'm guessing it's new, since it really is a much-needed class...

J.
Re: "connecting" objects together
Reply #3 - May 31st, 2007, 10:22pm
 
Suggest you structure your classes such that they mimic the problem...  Say you have a "Box" class and a "Dot" class.  The Box class should probably "own" an instance of the Dot class (or a whole array of dots), and the Dot will keep a reference back to its parent and use coordinates *relative* to its parent.  So when the Box is moved, the Dot moves without any additional effort -- the dot would be drawn using something like:
 ellipse(parent.x+x, parent.y+y, w, h);
Or you could nest calls to translate() so the box translates and draws first, then its dots draw relative to that.  To keep the Dot in the Box simply limit its coordinates to the width/height of its parent.
Re: "connecting" objects together
Reply #4 - Jun 1st, 2007, 12:19am
 
davbol wrote on May 31st, 2007, 10:22pm:
Or you could nest calls to translate() so the box translates and draws first, then its dots draw relative to that.  To keep the Dot in the Box simply limit its coordinates to the width/height of its parent.


hmm... that seems like a good idea. I'll see if I can put something like that together in the morning. Thanks!

J.
Page Index Toggle Pages: 1