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

2018/12/26

Rotating canon from the end

1
2
3
danpost danpost

2018/12/27

#
dev4ltar wrote...
is there a way to make it more smoother to place the canon to 0 degree and 300?
Of course, only change toward target angle a little at a time:
private void canonMove()
{
    // initial rotation
    int oldRot = getRotation();
    if (oldRot == 0) oldRot = 360;
    // target
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int y = mouse.getY();
    if (y > getY()) setRotation(0); else turnTowards(mouse.getX(), y);
    if (getRotation() < 300 && getRotation() != 0) setRotation(300);
    // new target rotation
    int newRot = getRotation();
    if (newRot == 0) newRot = 360;
    // change direction
    int direction = (int)Math.signum(newRot-oldRot);
    if (oldRot == 300 && direction < 0 || oldRot == 360 && direction > 0) direction = 0; // limiting when to change angles
    setRotation(oldRot+direction);
}
dev4ltar dev4ltar

2018/12/27

#
thank you master, and sorry for too much asking idk about signum idk about direction but line 16 tell the rotation limit is from 300 to 360 degree right? it feels itchy to understand the code you don't know
danpost danpost

2018/12/27

#
dev4ltar wrote...
idk about signum idk about direction but line 16 tell the rotation limit is from 300 to 360 degree right? it feels itchy to understand the code you don't know
signum: basically gets the sign of the evaluated expression -1 for a negative value 0 for zero 1 for a positive value direction: given one of the values above indicating which way the angle should be adjusted -1 for counter-clockwise 0 for no change 1 for clockwise line 16: correct.
dev4ltar dev4ltar

2018/12/27

#
nothing wrong with the code, i just curious about the code 300 = max top angle, 300 = -60 degree 360 = max bottom angle, 360 = 0 degree ^ from my view 1. get rotation if oldRot == 0, oldRot value set to 360 (initial object rotation) else oldRot = X Degree 2. if mouse's y > object's Y setRotation(0) , but the canon cannot rotated to bottom, isn't the bottom less than object's Y(minus)? so the canon should be can't rotate to top?how to determine top and bottom? is 300 become the bottom here? 3. if rotation <300 and not 0 set rotation 300 or limit the rotation to 300 4. get rotation if newRot ==0, newRot value set to 360 else newRot = X Degree 5. line 15-17 when rotation happen if (oldRot == 300 && direction < 0 or oldRot == 360 && direction > 0) direction = 0; while the rotation is 300 to top(counter clockwise) or 360 to bottom (clockwise) there's no change on angle direction the rotation set to oldRot+(0) or can you explain it? sorry if i bother you and thank you very much for teaching me
danpost danpost

2018/12/27

#
1. gets the current rotational angle, which is returns as values between 300 and 359, inclusive, or 0; 0 is changed to 360 to make a simple range of 300 to 360. This value will be used with a small change value to smoothly adjust the angle of the canon. 2. If mouse's y is greater than canon's y, then mouse is visually below the canon and therefore the canon's angle should be horizontal (0, or 360); otherwise point canon toward mouse (rotation angle to be between 180 and 359). I think the confusion is due to the fact that the canon's top limiting angle is the lowest angular value and the canon's bottom limiting angle is the highest (360) angular value. 3. This line (line 10) is probably not needed as the value of the direction variable (line 15) is not affected by that limitation. 4. getting target rotation in same range as oldRot. direction variable is assigned the change required to alter the rotation toward the target rotation. 5. if limits will be exceeded by the change, zero the change amount. The rotation is then set back to oldRot plus the change amount.
dev4ltar dev4ltar

2018/12/27

#
line 15 direction always 0? line 16 if oldRot = 300 counter clockwise or oldRot = 360 clockwise, direction not changed or stuck there ? else set rotation based oldRot+direction(always 0?) i still don't get about line 16 on && direction < 0 and && direction > 0, i've tried to delete one/both, and the canon can't rotate or stop at max/min angle but still can't figured out the function
danpost danpost

2018/12/27

#
dev4ltar wrote...
line 15 direction always 0?
No. Should be -1, 0 or 1.
line 16 if oldRot = 300 counter clockwise or oldRot = 360 clockwise, direction not changed or stuck there ?
If anything is wrong on that line, it would be possible missing parentheses about the "&&"ed groups; but, I do not think they are needed there.
else set rotation based oldRot+direction(always 0?)
Cannot be a problem there.
i still don't get about line 16 on && direction < 0 and && direction > 0, i've tried to delete one/both, and the canon can't rotate or stop at max/min angle but still can't figured out the function
I will manually test the code given. Give me a few.
dev4ltar dev4ltar

2018/12/27

#
danpost wrote...
No. Should be -1, 0 or 1.
isn't oldRot and newRot will always be the same? this might be the key for me to understand the code, can give example of what happening? yes nothing wrong on line 16 but i think it says if (oldRot == 300 && direction < 0 || oldRot == 360 && direction > 0) direction value set to 0 or the direction is not changed, this mean stuck or the direction not able to change else set rotation based oldRot+direction(always 0?)
danpost danpost

2018/12/27

#
dev4ltar wrote...
isn't oldRot and newRot will always be the same?
No. The rotation of the actor may have change at lines 9 and 10.
danpost danpost

2018/12/27

#
Problem is you were getting a NullPointerException error when trying to get mouse's location. Insert the following at line 8:
if (mouse == null) return;
dev4ltar dev4ltar

2018/12/27

#
so let's say line 16 the cursor move and the value of (oldRot == 300 && direction < 0 || oldRot == 360 && direction > 0) is 330 and - 1 when it reach 300 and -1 the direction set to 0 else set rotation based oldRot(X+direction(-1/0/1)) direction = 0 mean no change on the angle or stuck isn't it? but it still able to change direction, why?
dev4ltar dev4ltar

2018/12/27

#
no there's no problem nor error from your code, i just try to understand your code on line 15 to 17
danpost danpost

2018/12/27

#
direction will hold a value that will be the adjustment to the previous angle. If it is -1 and the angle was already 300, we do not want the angle to change (as 300-1 is 299, which is outside the valid range of angles). Likewise, if it is 1 and the angle was already 360, we do not want the angle to change. In either case, we want no change in angle from the previous angle. So, the direction to change the angle in is set to zero in those cases. Maybe I should have named the direction field changeAmount or angleChange, or something more along that line.
dev4ltar dev4ltar

2018/12/27

#
if i delete && direction < 0 and && direction > 0, why it can't rotate?
dev4ltar dev4ltar

2018/12/27

#
if i delete && direction < 0 it rotate to max angle (300) and then i'm unable to rotate it again, what happen?
There are more replies on the next page.
1
2
3