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

2012/5/13

How do I make an object turn towards another object?

steved steved

2012/5/13

#
I'm making a game and I need to make one of my objects turn towards the players object at random periods of time. The reason I don't always want it to folow you is because it is faster then you. I will probably just use the same random number thing that I use to make it randomly move around the world so that shouldn't be a problem. All I really need is a method that turns an object towards another object.
danpost danpost

2012/5/13

#
If you are using Greenfoot 2.2.0, there is an Actor class method turnTowards(Actor actor). If not, there are some older discussions that has the code you want.
steved steved

2012/5/13

#
No I'm not using greenfoot 2.2.0 ill try getting it. Thanks for the help!
steved steved

2012/5/13

#
when you say (Actor actor) do you mean like (Pie pie), (Actor Pie), or (Actor pie) with pie defined above? Could you explain how to use this method or give me the url of a page that explains it?
danpost danpost

2012/5/13

#
For the turnTowards() method, I think any of the three would work; but I would stay away from the second one (objects should be reference with lowercase starting letters; 'Pie' makes me want to think it is a class). As the only thing that turnTowards(Actor actor) needs from the actor is its location, which is a Greenfoot Actor class method, it is not neccessary to further sub-classify it (casting it more specifically). But, it does not hurt to, either; as 'Pie' would be a sub-class of Actor and inherits the methods within the Actor class.
tylers tylers

2012/5/13

#
turnTowards(Pie); put that when you want it to happen.
danpost danpost

2012/5/13

#
To further clarify, in the class of the object that is to follow the player:
if (Greenfoot.getRandomNumber(100) < chance && !getWorld().getObjects(Player.class).isEmpty())
{
    Player player = (Player) getWorld().getObjects(Player.class).get(0);
    turnTowards(player);
}
where 'chance' is set the the percent chance the object will follow the player.
davmac davmac

2012/5/13

#
turnTowards requires a location (x and y coordinates), not an actor. To modify the above example:
    if (Greenfoot.getRandomNumber(100) < chance && !getWorld().getObjects(Player.class).isEmpty())  
    {  
        Player player = (Player) getWorld().getObjects(Player.class).get(0);  
        turnTowards(player.getX(), player.getY());  
    }  
danpost danpost

2012/5/13

#
davmac wrote...
Is turnTowards() like facing a different class?
It can be used to face towards another object, which could be of the same or a different class.
@davmac, I saw this in another discussion, and I inferred from it, incorrectly, that turnTowards took an object (or actor) as its parameter. You can find the quoted statement here. @steved, please pardon my mis-information.
steved steved

2012/5/14

#
@davmac, thanks I finally got it working! @danpost, no problem and thanks for all the help and for the random thing. @tylers, I tried that and it doesn't seem to work ill mess with it and see if I can figure that out. It sure would be simpler.
You need to login to post a reply.