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

2011/6/2

Make an object rotate in direction of an other object

I want to have an Object, which rotates in one point. This rotation sould follow the position of an other object, which moves around the first object. But I've got no idea, how to make this come true. Thanks for any help!
I'm sorry for the same posts... how to delete this theard?
1NS4NE 1NS4NE

2011/6/2

#
MyHeartRocksNRolls wrote...
I want to have an Object, which rotates in one point. This rotation sould follow the position of an other object, which moves around the first object.
Just use this method: public void pointAtObject(Actor o) { setRotation((int)(180*Math.atan2(o.getY()-getY(),o.getX()-getX())/Math.PI)); } Actor o ist the object you want your object point at.
I'm sorry, but i can't use it as a public void act How do I have to write it? I do it this way: pointAtObject(Man.class);
webmessia webmessia

2011/6/2

#
If you want your object to point towards the Man you may have multiple Man instances so you need to know which one you want to point to. Method 1) If you want to point to the man that you are colliding with then use
public void act(){
    Actor coll = getOneIntersectingObject(Man.class);
    if(coll != null){
        pointAtObject(coll);
    }
}
Method 2) Extension of the first method, will continue to point at the man last collided with.
private Actor trackedMan = null;

public void act(){
    Actor coll = getOneIntersectingObject(Man.class);
    if(coll != null){
        trackedMan = coll;
    }
    if(trackedMan != null){
        pointAtObject(trackedMan);
    }
}
Method 3) Just point at the first Man added to the world
private Actor trackedMan = null;

public void act(){
    try{
        trackedMan = getWorld().getActors(Man.class).get(0);
    } catch (Exception e) {
        //
    }
    if(trackedMan != null){
        pointAtObject(trackedMan);
    }
}
You'll also need somewhere in your actor 1NS4NE's method pointAtObject(Actor o); I wrote all these in the browser so I don't know if they will work or not, but give them a go.
The first 2 methods work fine, but the third doesn't. and what does the word "coll" mean? It seems like it would be to hard for me make my game idea come true. Maybe this way it's easier: I want that a "Zombie" follows a "Man", but the "Zombie" does it only when the man is in a specific range. Kind of like in a tower defense game.
1NS4NE 1NS4NE

2011/6/2

#
first add this line at top of your code: import java.util.List; than add this to your act-method: public void act(){ List man = getObjectsInRange( ,Man.class); if(man.size()!=0) pointAtObject(man.get(0)); } if you wanna let your zombies just move if the Man.class is in range, you need to replace "if(man.size()!=0) pointAtObject(man.get(0));" by "if(man.size()!=0){ pointAtObject(man.get(0)); }"
My editor looks like this: import java.util.List; public class zombie extends Mover { public void pointAtObject(Actor c) { setRotation((int)(180*Math.atan2(c.getY()-getY(),c.getX()-getX())/Math.PI)); } public void act() { List man = getObjectsInRange( 10 ,Man.class); if(man.size()!=0) { pointAtObject(man.get(0)); <---- The programm says here is a mistake: pointAtObject(greenfoot.actor) in zombie cannot be applied to (java.lang.Object) move(); } } }
webmessia webmessia

2011/6/2

#
The reason for the error is because List is generic class because it can be a list of any type of object. In order to tell it you want it to be a list of Man declare the list using
List<Man> man = getObjectsInRange(10, Man.class); 
That should solve your problem. If you want more depth on generics Google java generics tutorial. However with regards to lists (the most common use of generics) just remember to put the class the list is of in those triangle brackets after list. so for a list of actors List<Actor> listOfActors; For an ArrayList (another type of list) you can contruct an arrayList object so you have to add the class to the constructor aswell like so
ArrayList<Actor> listOfActors = new ArrayList<Actor>();
Hope that helps :)
Now it works perfectly!!! Thank you!!! For me it seems like kind of magic. I don't understand what this "language" is saying, I'm only able to use the very basics.
1NS4NE 1NS4NE

2011/6/3

#
yeah sorry my mistake you could also just change the method's parameter into "public void pointAtObject(Man c)" would work too.
Just one little thing: The method pointAtObject could be implemented simplier: There's a method turnTowards(int x, int y) implemented in the Actor class, which does the calculation of the angle for us. Cheers! Hanspeter
ttg4l ttg4l

2016/2/22

#
MyHeartRocksNRolls wrote...
I'm sorry for the same posts... how to delete this theard?
Deleting is very difficult, it takes a lot of time from admins and mods
ttg4l ttg4l

2016/2/22

#
MyHeartRocksNRolls wrote...
Now it works perfectly!!! Thank you!!! For me it seems like kind of magic. I don't understand what this "language" is saying, I'm only able to use the very basics.
MAGIC IS NOT REAL
ttg4l ttg4l

2016/2/22

#
.
You need to login to post a reply.