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

2017/3/12

Ints doubles and setLocation()

Nosson1459 Nosson1459

2017/3/12

#
(Press here to see the problem). Basically the setLocation method uses ints, and if you open the coding in the above scenario you'll see that in order for me to get exact coordinates I need one that uses doubles. Even the one from the smoothMover class just rounds it off. (plz don't ask for any details unless you have seen the scenario it should answer most questions about anything I left out).
danpost danpost

2017/3/12

#
I noticed when running the scenario that the vector of movement was off by more than a mere degree. Then, I noticed what you were doing in the 'move' method with 'deltaX' and 'deltaY' -- which looked quite strange. The following would produce movement a lot closer (but not exact) to what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int rotation; // to hold direction to mouse click event
 
private void move()
{
    if (!shouldMove && Greenfoot.mouseClicked(null))
    {
        mouse = Greenfoot.getMouseInfo();
        turnTowards(mouse.getX(), mouse.getY());
        rotation = getRotation();
        setRotation(0);
        shouldMove = true;
    }
    if (shouldMove && !isAtEdge())
    {
        setRotation(rotation);
        move(3);
        setRotation(0);
    }
}
This will get you within one degree rotation of accuracy. For even better accuracy, the rotation will have to be tracked in a more accurate way. My QActor class, found in my Asteroids w/improved QActor SuperClass, can be used for fine-tuning both location and rotation values.
danpost danpost

2017/3/12

#
Using my QActor class, I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
public void act() // QActor class has a 'move' method
{
    if (!shouldMove && Greenfoot.mouseClicked(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        turnTowards(mouse.getX(), mouse.getY()); // face where mouse click event occurred
        addForce(3*QVAL, getQR()); // set velocity to 3 along the current rotational direction
        setRotation(0);
        shouldMove = true;
    }
    if (shouldMove && !isAtEdge()) move();
}
I did realize that the 'turnTowards(int, int)' method in the QActor class was not setting accurate q-rotational values (it was rounding to nearest unit). So, if you decide to use the class, change its implementation to this:
1
2
3
4
public void turnTowards(int x, int y)
{
    setQRotation((int)(QVAL*(Math.atan2(y-getY(), x-getX())*180/Math.PI)));
}
For even more accuracy, bump the value of QVAL up to 1000.
Nosson1459 Nosson1459

2017/3/12

#
If I was using the TurnTowards() method I wouldn't have made this discussion. I guess I assumed it was obvious that I wanted the Pop to stay facing the same way the whole time. I would think that you could use the Math.toDegrees method instead of doing *180/Math.PI (I didn't notice this method at first which could mean that you didn't either or maybe you just think that it's easier to do what you did).
danpost danpost

2017/3/12

#
Nosson1459 wrote...
If I was using the TurnTowards() method I wouldn't have made this discussion. I guess I assumed it was obvious that I wanted the Pop to stay facing the same way the whole time.
It is only used to get the direction to the mouse click (and for moving with the SmoothMover class) However, in all cases, the rotation is immediately set back to zero afterwards. With both codes given (the one for use with the SmoothMover class and the one for use with the QActor class) the actor will visibly remain at zero turn.
Nosson1459 Nosson1459

2017/3/12

#
I've uploaded it again using your QActor class and it still rotates(Pops )
danpost danpost

2017/3/12

#
You had too much in your act method. The entire class should look like this:
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
import greenfoot.*;
 
public class Pop extends QActor
{
    private boolean shouldMove;
 
    public Pop()
    {
        setImage(new GreenfootImage("RedHexagon.png"));
    }
 
    public void act()
    {
        move();
    }
 
    public void move()
    {
        if (!shouldMove && Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            turnTowards(mouse.getX(), mouse.getY()); // face where mouse click event occurred
            addForce(3*QVAL, getQR()); // set velocity to 3 along the current rotational direction
            setRotation(0);
            shouldMove = true;
        }
        if (shouldMove && !isAtEdge()) super.move();
    }
}
I used 'move' with 'super.move' to execute 'move' in the QActor class.
Nosson1459 Nosson1459

2017/3/13

#
thnx it works
Nosson1459 Nosson1459

2017/3/13

#
I want to reverse the x value when it hits the edge of the world, what values should I put in the parameters of the addForce method? should I try this
1
addForce(3*QVAL,-getQR());
Nosson1459 Nosson1459

2017/3/13

#
I tried it and it only works for the left side
danpost danpost

2017/3/13

#
Nosson1459 wrote...
I want to reverse the x value when it hits the edge of the world, what values should I put in the parameters of the addForce method? should I try this
1
addForce(3*QVAL,-getQR());
That would only cancel out any vertical movement and add to any horizontal movement. Negating the rotational value only points in the opposite direction when the rotation is 90 or 270 degrees. You could use the following to "bounce" off a vertical surface (or left and right edges of the world):
1
setVX(-getVX());
Nosson1459 Nosson1459

2017/3/13

#
thnx
You need to login to post a reply.