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

2017/6/1

turning objects in "while"

Talrasha Talrasha

2017/6/1

#
Hello, I've got a problem concerning the turn of objectives, while i turn them in a "while" loop.
while(temprotation<rotationv2)
            {
                if(durationMillis2>=10)
                {          
                    gegner1.turn(1);
                    zeitblock = false;
                    temprotation = temprotation + 1;
                    System.out.println(temprotation);
                }
                if(zeitblock!=true)
                {
                    startTime2 = System.currentTimeMillis();
                }
                zeitblock = true;
                long currentTime = System.currentTimeMillis();
                durationMillis2 = (int) (currentTime - startTime2);
                
            }    
the object is turning in the "while loop", that is what the variables say. The value of temprotation is increasing by 1 every loop. I checked that already by a print, but the object is not visibly changing. It is staying on it's rotation till it jumps out of the "while loop" then the object instant rotates to the new rotation. But i want a smooth rotation. I am very happy if you can help me ty, Talrasha
danpost danpost

2017/6/1

#
To make the turning visible, use 'if' instead of 'while'.
Talrasha Talrasha

2017/6/2

#
hey, i can't replace an "if", because the object needs to turn till the end of the rotation before process is accessing the next method. I can describe the Situation short: The object is a tank. The Object recieves the coodinates, where it shall move next, and before moving there, it shall rotate to towards this point on the place it is at the moment. And this in slow motion. If i now write an "if" it turns 1 and starts moving.
Super_Hippo Super_Hippo

2017/6/2

#
Where is this code located? Where is the code which moves the tank? Do you know how the act method works?
danpost danpost

2017/6/2

#
In the code given above, you should be able to remove lines 6, 9, 10, 11, 13 and 14 without changing what the code does. Also, lines 15 and 16 should probably be outside the block (regardless of whether it is a 'while' or 'if' block). There are some inherent problems with what I see in your code. First, the tank will only turn right and will never turn more than 359 degrees, at most. Second, because rotation is in units of whole degrees, turning toward a given set of coordinates will almost never have the tank land at those exact coordinates after moving. Another factor that will determine how close the tank comes to its target location is the speed of the tank; as the speed is set lower and lower towards one, the accuracy decreases exponentially. In fact, at a speed of one, regardless of the rotation of the tank, it will only travel in one of eight directions -- those that are a multiple of 45 degrees. Using a system that allows for smooth movement and rotation is suggested (my QActor support class provides actors with both; it can be found in my Asteroids w/improved QActor SuperClass scenario).
davmac davmac

2017/6/3

#
Insert a delay in the while loop in order to make the incremental changes visible:
Greenfoot.delay(1);
You should probably then remove your own timing code. Alternatively, use the world's repaint method.
danpost danpost

2017/6/3

#
davmac wrote...
Insert a delay in the while loop in order to make the incremental changes visible:
Greenfoot.delay(1);
You should probably then remove your own timing code. Alternatively, use the world's repaint method.
This would be a viable alternative provided that no other actions are taking place simultaneously -- that is, the rest of the world is essentially paused while the tank turns and moves to its target location.
davmac davmac

2017/6/4

#
This would be a viable alternative provided that no other actions are taking place simultaneously
Yes. OP doesn't mention any problem with other actors' actions being blocked, so I figure there's a good chance it's not an issue here.
Talrasha Talrasha

2017/6/5

#
Hey guys, i fixed the Problem, by the method "repaint()" @davmac thank you. Now i've got a new problem now, that was already mentioned. As long as the tank is turning, all other methods freeze.
danpost wrote...
This would be a viable alternative provided that no other actions are taking place simultaneously -- that is, the rest of the world is essentially paused while the tank turns and moves to its target location.
I didn't use the delay method tho. After the tank turned, when he starts to move the other methods work again.
Danpost wrote...
In the code given above, you should be able to remove lines 6, 9, 10, 11, 13 and 14 without changing what the code does. Also, lines 15 and 16 should probably be outside the block (regardless of whether it is a 'while' or 'if' block).
why should i put these lines outside ?
Danpost wrote...
There are some inherent problems with what I see in your code. First, the tank will only turn right and will never turn more than 359 degrees, at most.
i know, i only qouted one part of the code, but i already wrote the whole code.
Danpost wrote...
Second, because rotation is in units of whole degrees, turning toward a given set of coordinates will almost never have the tank land at those exact coordinates after moving.
yes i know, because of this i made an intervall for 10 +-.
Danpost wrote...
Another factor that will determine how close the tank comes to its target location is the speed of the tank; as the speed is set lower and lower towards one, the accuracy decreases exponentially. In fact, at a speed of one, regardless of the rotation of the tank, it will only travel in one of eight directions -- those that are a multiple of 45 degrees
i don't understand this point. For a smoother movement i use the class SmoothMover. here is the whole code for the method.
public void movement()
    {
        if(movementprüfer==false)
        {
            zufallszahl1=(int)(Math.random()*200)+gegner1.getX()-100;
            zufallszahl2=(int)(Math.random()*200)+gegner1.getY()-100;
            rotationv1=gegner1.getRotation();
            gegner1.turnTowards(zufallszahl1,zufallszahl2);
            rotationv2 = gegner1.getRotation();
            gegner1.setRotation(rotationv1);
            temprotation = rotationv1;
            System.out.print(temprotation + "  " +rotationv2+ "  | ");
            while(temprotation>rotationv2)
            {
                if(durationMillis2>=10)
                {          
                    gegner1.turn(-1);
                    zeitblock = false;
                    temprotation = temprotation - 1;
                    //System.out.println(temprotation);
                    w.repaint();
                }
                if(zeitblock!=true)
                {
                    startTime2 = System.currentTimeMillis();
                }
                zeitblock = true;
                long currentTime = System.currentTimeMillis();
                durationMillis2 = (int) (currentTime - startTime2);
                
            }  
            while(temprotation<rotationv2)
            {
                if(durationMillis2>=10)
                {          
                    gegner1.turn(1);
                    zeitblock = false;
                    temprotation = temprotation + 1;
                    //System.out.println(temprotation);
                    w.repaint();
                }
                if(zeitblock!=true)
                {
                    startTime2 = System.currentTimeMillis();
                }
                zeitblock = true;
                long currentTime = System.currentTimeMillis();
                durationMillis2 = (int) (currentTime - startTime2);
                
            }            
            movementprüfer = true;
        }
        gegner1.move(2);
        if(gegner1.getX()<=zufallszahl1+10&&gegner1.getX()>=zufallszahl1-10&&gegner1.getY()<=zufallszahl2+10&&gegner1.getY()>=zufallszahl2-10)
        {
            movementprüfer = false;
        }
    }
To come back to my quetion. How can i use other methods whlie the process is in the while loop? Already ty for the great help btw.
Super_Hippo Super_Hippo

2017/6/5

#
There is no way to do it, that's why you shouldn't execute anything in loops unless either the whole loop should execute immediately or nothing has to happen anywhere else in the code. The act method is called every act cycle. You can somewhat call it a loop, but each actor's act method (in the active world and the active world's act method itself) is executed once one after another. So you have to make sure that each time the act method is executed, the tank knows what to do. You can do that by saving the target rotation and then turn step by step to the rotation each act cycle.
Talrasha Talrasha

2017/6/5

#
Alright. i gonna try to programm this tommorow.
You need to login to post a reply.