4
\$\begingroup\$

I am writing a game in Java, I have this long method to move GameBodyObject towards the point:

public boolean moveTowardsPoint(Point p, double stepSize, double minDistance, ArrayList<GameBodyObject> objects,  boolean shouldStepInto) {
    double dx = p.getX() - getPosition().getX();
    double dy = p.getY() - getPosition().getY();

    boolean isClose = true;

    Point lastPosition = getPosition().clone();

    if(Math.sqrt(dx * dx + dy * dy) > minDistance) {
        isClose = false;
        setRotation(Math.atan2(dy, dx));


        // Though the sqrt is expensive operation
        // We check first without using sqrt
        if (shouldStepInto && Math.abs(dx) + Math.abs(dy) < stepSize) {
            if (Math.sqrt(dx * dx + dy * dy) < stepSize) {
                getPosition().setX(p.getX());
                getPosition().setY(p.getY());
            }
        } else {
            getPosition().setX(getPosition().getX() + (stepSize * Math.cos(getRotation())));
            getPosition().setY(getPosition().getY() + (stepSize * Math.sin(getRotation())));
        }

    }


    for(GameBodyObject obj : objects) {
        if(!obj.equals(this)) {
            if (checkCollision(obj)) {
                setPosition(lastPosition);
                return isClose;
            }
        }
    }

    return isClose;

}

checkCollision checks if there is a collision and if yes it returns true otherwise it returns false.

Now if GameBodyObject collides it stops. What if I want this object to walk around the collider? simply to find a path around when the collision occurs. (I don't want to implement any A* or other pathfinding algorithms btw)

Is it possible without pathfinding?

I was thinking that if collision occurs I try to move more to the left, if the collision occurs again I try to move a bit to the right, and then move a more than a littlebit left, and if the collision doesn't occur I move to that direction. But that's bruteforce and very inefficient.

IMPORTANT EDIT: ALL my GameBodyObjects are circles with getRadius() and getPosition().getX(), getPosition().getY()

EDIT

I've tried to calculat normal vector of vector pointing from me (gameBodyObject1) and the colliding gameBodyObject2. Then I moved towards that normal vector. I got a jiggling bouncing effect which I don't want. The bouncing effect appeared when there was a multiple colliding objects. After moving by the normal I could walk through the objects which is also not wanted effect. Maybe I should check if that new movement creates a collision.

\$\endgroup\$
  • \$\begingroup\$ You're probably looking for steering. \$\endgroup\$ – MichaelHouse Aug 1 '17 at 17:06
  • \$\begingroup\$ I'd assume the other GameBodyObjects are moving also. \$\endgroup\$ – clabe45 Aug 1 '17 at 17:12
  • \$\begingroup\$ some of them are moving some of them are static. I check that with gameBodyObject.isStatic() \$\endgroup\$ – durisvk10 Aug 1 '17 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.