A Pulsar Array. Need Help please

Hey Guys,

this is one of the hardest tasks in the exam. There are another variations in the other exams which I will include in the next days in another topics.

Now This task in german

Schreiben	Sie	eine	Klasse	Pulsar,	die	einen	Kreis	malt,	der	sich	ausdehnt	und	
wieder	zusammenzieht	(also	pulsiert).	Die	Klasse	hat	5	Eigenschaften:	x/yPosition,	Geschwindigkeit	(der	Ausdehnung/Zusammenziehung,	als	float),	
Durchmesser	und	eine	boolesche	Variable	active.
Im	Konstruktor sollen	x/y	zufällig,	Durchmesser	auf	10	und	Geschwindigkeit	
zufällig	(zwischen	0.2	und	1.5)	gesetzt	werden.
Schreiben	Sie	zwei	Methoden fürs	Zeichnen	(render)	und	Updaten	(update).	
Der	Durchmesser	des	Kreises	soll	max.	30	und	min.	5	betragen. Wenn	die	Variable	active false	ist,	soll	
das	Objekt	nicht	pulsieren,	aber	gezeichnet	werden.
Im	Hauptprogramm erzeugen	Sie	ein	Fenster	der	Größe	100x100 mit	weißem	Hintergrund	und 100	
Pulsar-Objekten,	die	alle	gezeichnet	werden	und	pulsieren.
Zusätzlich	soll	per	Tastendruck die	Bewegung von	der	Hälfte aller	Objekte angehalten	werden	können.	
Beim	nochmaligen	Druck	wird	sie	fortgesetzt	usw.	(Verwenden	Sie	dazu	die	Instanzvariable	active).

At the moment my Code is this but it is not finished yet:

class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;

  Pulsar(float x, float y, float s)
  {
    xpos = x*(random(0, 10));
    ypos = y*(random(0, 10));
    speed = s*(random(0.2, 1.5));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);
    xpos++;
    ypos++;
  }

  void update()
  {
    if (xpos >= 30 && ypos >= 30)
    {
      xpos = -xpos;
      ypos = -ypos;
    }

    if (xpos <= 5 && ypos <= 5)
    {
      -xpos = xpos;
      -ypos = ypos;
    }
  }
}
Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
  
  
}


void draw()
{
  
  
  
}

At first my class should be mostly right (I think)
What is missing is that I was not able to write the void keypressed section but I will do this when everything is right.
Could someone controll the class section and say to me what I must exchange or include?
After that I will show you the next try of this code.

Thank you guys. :smiley:

1 Like

You should address some of the obvious errors before posting code:

1 Like

Is this the only part where It needs to be fixed in the class?

You are posting the homework incorrect. It looks distorted. Don’t format it as code but as a quote " " .

Like this - also translate it into English, because most people here speak English.

Schreiben Sie eine Klasse Pulsar, die einen Kreis malt, der sich ausdehnt und
wieder zusammenzieht (also pulsiert). Die Klasse hat 5 Eigenschaften: x/yPosition, Geschwindigkeit (der Ausdehnung/Zusammenziehung, als float),
Durchmesser und eine boolesche Variable active.
Im Konstruktor sollen x/y zufällig, Durchmesser auf 10 und Geschwindigkeit
zufällig (zwischen 0.2 und 1.5) gesetzt werden.
Schreiben Sie zwei Methoden fürs Zeichnen (render) und Updaten (update).
Der Durchmesser des Kreises soll max. 30 und min. 5 betragen. Wenn die Variable active false ist, soll
das Objekt nicht pulsieren, aber gezeichnet werden.
Im Hauptprogramm erzeugen Sie ein Fenster der Größe 100x100 mit weißem Hintergrund und 100
Pulsar-Objekten, die alle gezeichnet werden und pulsieren.
Zusätzlich soll per Tastendruck die Bewegung von der Hälfte aller Objekte angehalten werden können.
Beim nochmaligen Druck wird sie fortgesetzt usw. (Verwenden Sie dazu die Instanzvariable active).

Nach der Aufgabenstellung braucht der Konstruktor keine Parameter.

Du sollst xpos, ypos und speed nur zufällig festlegen. Du kannst es also einfacher machen.

Andere Anmerkungen

Du hast dia nicht auf 10 gesetzt

Das Pulsieren wird so nicht funktionieren, denke ich.

Das kannst du testen, wenn du die Klasse von draw aus auch benutzt. So wie es jetzt ist, kannst du die Klasse nicht testen, und wir auch nicht. Versuche nur Code zu posten, der ein lauffähiges Programm ist.

Chrisir

Ich habe nun mein Code erweitert.
Leider funktioniert es nicht.

Ich verstehe es nicht.
Habe nun den Code auf das Hauptprogramm übertragen aber leider erscheint da gar nichts.
Eigentlich müssten nun die Bälle aufblasen und kleiner werden -.-.

class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;

  Pulsar()
  {
    xpos = (random(0, 10));
    ypos = (random(0, 10));
    speed = (random(0.2, 1.5));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);
    xpos++;
    ypos++;
  }

  void update()
  {
    if (xpos >= 30 || ypos >= 30)
    {
      xpos = -xpos;
      ypos = -ypos;
    }

    if (xpos <= 5 || ypos <= 5)
    {
      xpos = -xpos;
      ypos = -ypos;
    }
  }
}
Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
 for(int i= 0; i < p.length; i++)
 {
   p[i] = new Pulsar();
 }
  
  
}


void draw()
{
  for(int i= 0; i < p.length; i++)
 {
   p[i].render();
   p[i].update();
 }
  
}
1 Like

Das Hauptprogramm ist gut.

Dein handling von xpos und ypos ist falsch.

Lies nochmal die Aufgabe; was genau soll pulsieren? Die Position???

Hey Guys,
yesterday I was very busy with my work.

Now I have nearly completed this task.

Here is my code.
I am very confident this time:

This is the main code

Pulsar[] p = new Pulsar[100];

void setup()
{
 size(100,100); 
 for(int i= 0; i < p.length; i++)
 {
   p[i] = new Pulsar();
 }
  
  
}


void draw()
{  background(255);
  for(int i= 0; i < p.length; i++)
 {
   p[i].render();
   p[i].update();
 }
  
}

This is the class code

class Pulsar {
  float xpos;
  float ypos;
  float speed;
  float dia;
  boolean active;


  Pulsar()
  {
    xpos = (random(0, 100));
    ypos = (random(0, 100));
    speed = (random(0.2, 1.5));
    dia = (random(5, 30));
  }

  void render()
  {
    fill(255);
    ellipse(xpos, ypos, dia, dia);


    dia++;
  }

  void update()
  {
    if (dia >= 30)
    {
      dia = -dia;
    }

    if (dia < 5)
    {
      dia = +dia;
    }
  }
}

Now i need to make the part with the boolean active.

If I use Keypressed half of the Pulsar balls need to stop.
I think I need to make this in the class code.
But I am not sure how.

The part in the class is not filled cause I dont know how:

void keyPressed()
{
  if(active == true)
  {
    
    
    
  }


}

After this is filled the task should be fine.

1 Like

I am not happy with this.

You are working with a negative diameter, which works, but is more like a work-around. There is no such thing as a negative diameter in the real world.

Instead, remember the ball that gets reflected on the sides of the rect: x = x + speedX;

I would make the pulsar like the ball!

  • Instead of dia++; better say dia = dia + diaSpeed; (or speed) (or dia += diaSpeed;). The var diaSpeed would be 1 initially or random like in the assignment. (One could argue, this belongs into the method update() instead of render(). Why?)

  • Then instead of dia = -dia; better diaSpeed = - diaSpeed; (in both places in your code) (or speed)

Es steht sogar in der Aufgabe

Geschwindigkeit (der Ausdehnung/Zusammenziehung, als float)

da ist dein speed, den du aber nicht mehr benutzt!!!

Warmest regards,

Chrisir

1 Like

This should be in the class:

 if(active == true)  // or just if(active) 
  {
    
    // what belongs here??? You already have this. 
    
  }

Details

in keyPressed() - a function outside the class - you want to for-loop over the array(!) and randomly switch half of the pulsars to inactive. E.g. with if(random(100)<50).... and then count the active ones and stop at 50.

active must be inside the class because it is a property of the class and different for each pulsar (the objects derived from the class).

Chrisir

1 Like