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

2019/3/25

SetRotation doesn't work always for the direction of movement

dejong1968 dejong1968

2019/3/25

#
I have a world with this code:
super(800, 600, 1); 
Ball ball = new Ball(){};
ball.setRotation(10); 
addObject(ball, 5, 5);
The goal is to add a Ball to the world with the rotation already set (tot 10) and when the game starts, the ball should start moving at an angle of, in this case, 10 degrees. (in the future, that 10 should be a random number) I have an image of a square(*), black 'ball' with size 10x10 pixels. (*) = so I can see visually if the ball is rotated. The only code in the void act of the ball is:
move(1);
What I expect is when the game starts the ball starts moving to the right at an angle of 10 degrees ( 0 degrees = east of course) in case of the code above. But it doesn't...... Here is what happens when I fill in different numbers instead of 10 - Number >=0 AND <= 30 --> Ball goes at angel of 0 degrees, so straight east - Number >=30 AND <= 60 --> Ball goes at an estimated angle of 45 degrees - Number >= 61 AND <=90 --> Ball goes straight down (angle of 90 degrees) In all these cases: 1. I see visually that the ball is rotated 2. When I inspect the Ball (right click, Inspect) I see that the rotation is correct according to the code above. So the ball rotates ok, but it only moves in 3 directions: horizontally, vertically and appr 45 degrees. I've tried different images (size, shape) but with no effect. Can anybody tell me what I'm doing wrong? Thanks!
Super_Hippo Super_Hippo

2019/3/25

#
An Actor is always in the middle of a cell. In other words, the x and y coordinates of Actors are Integers. If you are moving 1 cell, then there are 8 possible cells it can reach (actually 9 if it reaches the end of the world and doesn't move at all), so it can only move in steps of 45 degrees. You can test this behavior by changing the movement to 2 cells per act. The number of possible directions increases. To be able to use every direction correctly, you need to save the current position in double variables. The easiest way is to import the SmoothMover class and let the Ball class extend the SmoothMover class. If you also need the rotation to be a double, then you can adjust its code. Otherwise, you don't need to change anything in it. Btw, there is no need for the {} in line 2.
dejong1968 dejong1968

2019/3/26

#
Thanks! It works fine!
You need to login to post a reply.