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

2015/3/20

How to get distance to another class?

EduandErnst EduandErnst

2015/3/20

#
Hi, I am currently trying to get the distance between my main actor and another class. So I'm like standing on a random position in the world and somewhere is a obstacle. So I coded
Actor obstacle = getOneObjectAtOffset(200, 0, obstacle.class);
if (obstacle != null && Greenfoot.isKeyDown("up")){
setLocation(getX() "??", getY());
}
So when I'm in horizontal range of the obstacle and I press the "up" key I want to move forward to the exact position of the obstacle ( the obstacle is at a random position). Is there a way to get the distance?
Super_Hippo Super_Hippo

2015/3/20

#
The distance is 200 because you check for the position 200 cells to the right. In general, the distance is the square root of the sum of the squares of the x and y differences. (→Pythagoras)
double r = Math.sqrt(Math.pow(getX()-obstacle.getX(),2)+Math.pow(getY()-obstacle.getY(),2));
danpost danpost

2015/3/20

#
If coded like you have it above, your main character will instantaneously teleport to the location set on line 3. I am not sure if that is what you want. Also, it will only happen if the obstacle is 200 pixels to the right. If you were closer, say 100 pixels away, and 200 pixels to the right was passed the obstacle, then it would not be detected using 'getOneObjectAtOffset'. I mention this because you used the word 'range' in your initial post.
EduandErnst EduandErnst

2015/3/20

#
Yes that's the problem, obviously I can jump 200 pixels towards the right but if I was closer than 200 pixels, I'd stuck in the obstacle. That's why I'm asking how to check for the actual distance between two objects.
danpost danpost

2015/3/20

#
It sounds like you have a user-controlled character and want to implement some collision detection, so the character does not pass through the obstacle. If that is the case, you are over-complicating things. First determine user-controlled movement direction; then, move the actor, then check for touching obstacle and, if touches, move back off the obstacle:
int dx = 0; // for user directed horizontal movement
if (Greenfoot.isKeyDown("down")) dx--; // I would use "left" key for left
if (Greenfoot.isKeyDown("up")) dx++; // I would use "right" key for right
setLocation(getX()+dx, getY()); // move horizontally as directed
if (isTouching(obstacle.class) setLocation(getX()-dx, getY()); // move back off obstacle
If moving more than one pixel per act (actor has a horizontal speed factor), just multiple 'dx' by the value of that factor in the 'setLocation' statements.
EduandErnst EduandErnst

2015/3/20

#
No I've non collision detecting problem. I "only" want to climb over the obstacle, the climbing is animated and in order to prevent animation bugs like flying through air or something I'd like to put the main actor at the position of the obstacle while "up" is pressed and actor is in range. Afterwards while the same conditions are still true, I want the actor so slowly move upwards towards the top of the obstacle. I only have one idea...
int actorx = (int) getX();
        int obstaclex = (int) imgsize.getX();
        int myWidth = (int) getImage().getWidth();
        int spritemywidth = (int) myWidth/2;
        int boxwidth = (int) imgsize.getImage().getWidth();
        int spritebox = (int) boxxwidth/2;
        int blubb = boxbox-boyboy;
        int uff = blubb + spriteewidth - spriteboxx;
but this definitely is not very smart..
EduandErnst EduandErnst

2015/3/20

#
EduandErnst wrote...
No I've non collision detecting problem. I "only" want to climb over the obstacle, the climbing is animated and in order to prevent animation bugs like flying through air or something I'd like to put the main actor at the position of the obstacle while "up" is pressed and actor is in range. Afterwards while the same conditions are still true, I want the actor so slowly move upwards towards the top of the obstacle. I only have one idea... (imgsize is a variable with the obstacle)
int actorx = (int) getX();
int obstaclex = (int) imgsize.getX();
int myWidth = (int) getImage().getWidth();
int spritemywidth = (int) myWidth/2;
int boxwidth = (int) imgsize.getImage().getWidth();
int spritebox = (int) boxxwidth/2;
int blubb = obstaclex-actorx;
int uff = blubb + spritewidth - spritebox;
but this definitely is not very smart..
// sry for double post
danpost danpost

2015/3/20

#
What if you changed my code above to this:
int dx = 0; // for user directed horizontal movement
if (Greenfoot.isKeyDown("left")) dx--; 
if (Greenfoot.isKeyDown("right")) dx++;
setLocation(getX()+dx, getY()); // move horizontally as directed
if (isTouching(obstacle.class))
{
    setLocation(getX()-dx, getY());
    if (Greenfoot.isKeyDown("up")) setLocation(getX(), getY()-1);
}
With this, as long as you are 'pressing' against the obstacle (holding the left or right key down), you can move up the side of it with the 'up' key (all I did was add line 8).
EduandErnst EduandErnst

2015/3/20

#
I'm sorry this is my fault. I didn't mention that the obstacle is movable. So when I run into it, it just moves with the actor, anyhow it probably is a great idea for walls. What do you think about something like this?
int image-size = obstacle.getImage().getWidth();
int spriteimagesize = (int) image-size/2;
Actor obstacle = getOneObjectAtOffset(spriteimagesize+200, 0, obstacle.class);
if (obstacle != null && Greenfoot.isKeyDown("up")){

int a = 0;
do {
a++;
setLocation(getX()+a, getY());
}while (getOneObjectAtOffset(spriteimagesize, 0, obstacle.class) == null; 
if (getOneObjectAtOffset(boxwith/2,0,Box.class) != null && Greenfoot.isKeyDown("up")){
setLocation(getX(),getY() -10);
animateBoxUpRight();
}
}
danpost danpost

2015/3/20

#
EduandErnst wrote...
What do you think about something like this? < Code Omitted >
I think you should use my code above for the character and then, in the class of the moving obstacle, have any player intersecting it move when it moves -- keeping things simple. I am presuming, however, that the obstacle will continue to move normally regardless of any interaction with the player (the obstacle pushes the player around). If you need help with the code for the class of the moving obstacle in this regard, post what movement code you have for it (and anything related to that movement)..
You need to login to post a reply.