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

2013/2/2

Soccer Computer Movement

sirjustinhe sirjustinhe

2013/2/2

#
I want a code that makes the computer follow the ball, but it goes slower. I've been thinking about how to do this, but I don't know what to do. Anyone have any ideas? thanks
MatheMagician MatheMagician

2013/2/3

#
Are you using the move() method to move the computer?
sirjustinhe sirjustinhe

2013/2/3

#
yea i am but it just changes its x coordinates; it doesn't go toward the ball in anyway; also, how can i make it so the ball bounces off a sprite and goes in a different direction depending on where it hit it?
danpost danpost

2013/2/3

#
When using the 'move(int)' method, the direction of movement will be in the direction that the computer is facing dependent on its rotation (or turn). When the rotation is zero, right is the direction of movement and as the number of degrees turn increases through to 359, the direction is shifted clockwise. The 'turnTowards' can be used to rotate the computer. The x and y locations must be retrieves from the Ball object each act cycle and used as the parameters in the 'turnTowards' method call. After the call to that method, then call the 'move' method.
sirjustinhe sirjustinhe

2013/2/3

#
can I just do a turnTowards(Ball.class) and it'll work?
MatheMagician MatheMagician

2013/2/3

#
No, the turntowards method requires a coordinate parameter, not a class parameter. What you need is something like this:
turnTowards(getWorld().getObjects(Ball.class).get(0).getX(),getWorld().getObjects(Ball.class).get(0).getY());
sirjustinhe sirjustinhe

2013/2/4

#
so how should I word my whole method definition?
danpost danpost

2013/2/5

#
If your method is called 'move', then it should be written something like to following:
public void move()
{
    Ball ball = (Ball) getWorld().getObjects(Ball.class).get(0);
    turnTowards(ball.getX(), ball.getY());
    move(3); // adjust speed to suit
}
danpost danpost

2013/2/5

#
There are several different ways to program a bounce off objects. The most standard are those that focus on the centerpoints of both objects, which performs a bounce as if too balls hit each other. The reason being is this type of bounce takes the least checking. The next type of bounce is ball and square block (with no rotation); however, this requires a bit of checking to figure out the new direction of the ball. Next, would be ball and rectangular block (with no rotation). Then, would be ball and rectangular block with rotations of 90 degress increments. Each type requires more and more code to perform the appropriate checking to determine where the ball should proceed.
sirjustinhe sirjustinhe

2013/2/5

#
thanks danpost, but how would I make the computer move toward the ball after I have made it turn toward the ball?
danpost danpost

2013/2/5

#
See line 5 above.
sirjustinhe sirjustinhe

2013/2/5

#
ohhhhhh thanks.
You need to login to post a reply.