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

2020/4/10

Get one Position

Starlord_20 Starlord_20

2020/4/10

#
how can i get a position some cells in front of my actor? example: for rotation = 90, distance = 3 he would return getY()+3 (but in int)
int targetX = 22;
    int targetY = 10;
    
    Activating act1 = new Activating();
    Activating act2 = new Activating();
    Beam b = new Beam();
    public void act() 
    {
        this.turnTowards(targetX,targetY);
        getWorld().addObject(/*getRotation + 10*/);
    } 
thats the code i need it for
danpost danpost

2020/4/10

#
The following should help:
int distance = 3; // distance from current position
int r = getRotation()/90; // rotation as an element of { 0, 1, 2, 3 }
int dx = (1-r)%2; // horizontal direction as an element of { -1, 0, 1 }
int dy = (2-r)%2; // vertical direction as an element of { -1, 0, 1 }
int x = getX()+dx*distance; // x-coordinate of new location
int y = getY()+dy*distance; // y-coordinate of new location
You need to login to post a reply.