This site requires JavaScript, please enable it in your browser!
Greenfoot back
omnix
omnix wrote ...

2017/12/30

How to swich lenght values in greenfoot? (dx and dy)

omnix omnix

2017/12/30

#
Hello, my question is in the headline. I should make a scenario, which includes a space with three dics (in the code their are called "scheibe" (german for disc)). Each of the discs has to bounce away while it gets in contact with an other disc. So my teacher has explained me that I should swich the dx and dy values which stands for the increment in x- and y-direction of each moving disc. And in general I dont know how to swich values in greenfoot, exspecially in the code of this scenario. The gaps where the swich is needed are marked with question marks. In the whole code there are three gaps of questionmarks so all possible combinations between the dics when they touching each other are accounted. I have translated the comment over the questionmarks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color.*;
import java.awt.Font.*;
 
 
public class cAnzeige extends Actor {
    
    cScheibe meineScheibe[]; // meineScheibe ist eine Array-Instanz der Klasse cScheibe
    double m;
    double sx, sy, abstand; // Datentypzuweisung der Variablen sx, sy, abstand mit double 
    int t = 0 ; // Datentypzuweisung mit int und Initialisierung mit 0 für Variable t
     
    public cAnzeige(String text, cScheibe scheibe[]) {  // allgemein zugänglicher Konstruktor der Klasse cAnzeige
        meineScheibe = scheibe;  // das Objekt scheibe geht hervor aus Unterklasse meineScheibe
        GreenfootImage anzeige = new GreenfootImage(150,50); // Erzeuge ein neues Objekt der Klasse GreefoodImage mit den Maßen 150 x 50
        anzeige.setFont(new Font("Sans Serife", 24)); // Festlegung der Schriftart "Sans Serif" mit Schriftgröße 24 für die Anzeige (anzeige)
        anzeige.setColor(Color.BLUE); // Festlegung der Farbe BLUE (Blau) für die Anzeige (anzeige)
        anzeige.drawString(text, 5, 25); // Erzeugung von Array der Zeichen: text, 5, 25 (String) für anzeige
        setImage(anzeige); // anzeige ist Attribut von setImage > "wird als Bild gesetzt"
    }
     
    public void act() {
        GreenfootImage anzeige = getImage(); // getImage geht aus der Klasse GreenfootImage hervor
        anzeige.clear(); // Leere das Image
         
         
        sx = meineScheibe[0].px-meineScheibe[1].px; // x - Abstand zwischen der 0. und 1. Scheibe
        sy = meineScheibe[0].py-meineScheibe[1].py; // y - Abstand zwischen der 0. und 1. Scheibe
        abstand = Math.sqrt(meineScheibe[0].px-meineScheibe[1].px*meineScheibe[0].px-meineScheibe[1].px+meineScheibe[0].py-meineScheibe[1].py*meineScheibe[0].py-meineScheibe[1].py); // Abstand der beiden Scheiben  
        if(abstand<=50) {  // If-Anweisung: Wenn der boolesche Ausdruck "abstand kleiner/gleich 50" wahr (true) ist...
             
            // swich the dx and dy values of meineScheibe[0] und meineScheibe[1]
             
             
                   ???
           ???
           ???
            
           ???
           ???
           ???
             
            t++;  // inkrementiere t um 1*/
 
        }
         
        sx = meineScheibe[0].px-meineScheibe[2].px; // x - Abstand zwischen der 0. und 2. Scheibe
        sy = meineScheibe[0].py-meineScheibe[2].py; //  y - Abstand zwischen der 0. und 2. Scheibe
        abstand = Math.sqrt(meineScheibe[0].px-meineScheibe[2].px*meineScheibe[0].px-meineScheibe[2].px+meineScheibe[0].py-meineScheibe[2].py*meineScheibe[0].py-meineScheibe[2].py); // Abstand der beiden Scheiben  
        if(abstand<=50) {  // If-Anweisung: Wenn der boolesche Ausdruck "abstand kleiner/gleich 50" wahr (true) ist...
             
            // swich the dx and dy values of meineScheibe[0] und meineScheibe[2]
             
             
                        ???
            ???
            ???
             
            ???
            ???
            ???
             
            t++;  // inkrementiere t um 1
 
        }
         
        sx = meineScheibe[1].px-meineScheibe[2].px; // x - Abstand zwischen der 1. und 2. Scheibe
        sy = meineScheibe[1].py-meineScheibe[2].py; //  y - Abstand zwischen der 1. und 2. Scheibe
        abstand = Math.sqrt(meineScheibe[1].px-meineScheibe[2].px*meineScheibe[1].px-meineScheibe[2].px+meineScheibe[1].py-meineScheibe[2].py*meineScheibe[1].py-meineScheibe[2].py); // Abstand der beiden Scheiben  
        if(abstand<=50) { // If-Anweisung: Wenn der boolesche Ausdruck "abstand kleiner/gleich 50" wahr (true) ist...
             
            // swich the dx and dy values of meineScheibe[1] und meineScheibe[2]
             
            
                        ???
            ???
            ???
             
            ???
            ???
            ???
             
            t++;  // inkrementiere t um 1
 
        }
         
         
        anzeige.drawString(t + " Stöße", 5, 25);
        // Erzeugung von Array der Zeichen: t + " Stöße", 5, 25 (String) für anzeige
        
    }   
    }
     
//Es wurde selbstständig gemacht
Yehuda Yehuda

2017/12/31

#
If an object collided with another object after moving along the X axis then you negate 'dx'.
1
dx = -dx;
If an object collided with another object after moving along the Y axis then you negate 'dy'.
1
dy = -dy;
I think you're just meant to do both.
Super_Hippo Super_Hippo

2017/12/31

#
Why don't you have the discs checking if they are touching each other? Also, you could ask your teacher why you don't update your Greenfoot versions.
danpost danpost

2017/12/31

#
Doing what Yehuda suggests will have the discs reversing directions. This will not be visually appealing as discs bounce off one another. Having the discs exchange their direction values might be a little better, but still will not appear to be a normal bounce. To create a real looking bounce, the size of the discs, as well as direction and speed, and their world locations at the time the bounce occurs must all be taken into account. It gets quite complicated. From what you wrote above:
omnix wrote...
my teacher has explained me that I should swich the dx and dy values which stands for the increment in x- and y-direction of each moving disc.
I get the impression that the two dx values need switched and the two dy values need switched.
1
2
3
4
5
6
7
// given two values
int value1, value2;
 
// to switch them
int hold = value1;
value1 = value2;
value2 = hold;
You need to login to post a reply.