Results 1 to 9 of 9

Thread: Game Design. Circular Surface Deflection

  1. #1
    Join Date
    Feb 2009
    Location
    Kennesaw, Georgia
    Beans
    122
    Distro
    Ubuntu 12.04 Precise Pangolin

    Game Design. Circular Surface Deflection

    I'm making a simple 2D game. It's coded in Java.

    In the game, there is a planet and objects that move around the planet according the force of gravity. I've got the gravity working but I just can't wrap my mind around how to deal with the object to planet collisions.

    When one of the moving objects strikes the surface of the planet, I want it to properly deflect based on the angle at which it struck. I just can't wrap my brain around how to do this.

    Something to work with:

    Code:
    class Planet {
    
    public int getX();
    public int getY();
    public int getRadius();
    
    }
    
    class MovingObject {
    
    public int posX;
    public int posY;
    public float velX;
    public float velY;
    
    }
    
    class Physics {
    
    public static float getDistane(int x1, int y1, int x2, int y2);
    public static float getAngle(int x1, int y1, int x2, int y2);
    
    }
    
    Planet p;
    MovingObject s;
    .....;
    // if collision
    if (Physics.getDistance(s.posX, s.posY, p.getX(), p.getY()) < p.getRadius) {
    // deflection occurs
    }

    Any help would be much appreciated. Thank you.
    Last edited by Dr Belka; June 12th, 2012 at 11:32 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Game Design. Circular Surface Deflection

    arctan delta(y)/delta(x) -> angle of movement vector

    arctan (yi-y0)/(xi-x0) -> angle created by r anchored in x0,y0 in the point of impact xi,yi

    calculate the difference and mirror that angle on the other side of radius. Use this newly created angle to calculate new movement vector

    ... or something

  3. #3
    Join Date
    Feb 2009
    Location
    Kennesaw, Georgia
    Beans
    122
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Game Design. Circular Surface Deflection

    Thank you so much for your reply.

    Quote Originally Posted by Vaphell View Post
    arctan delta(y)/delta(x) -> angle of movement vector
    Yup, that's what Physics.getDistance() does.

    Quote Originally Posted by Vaphell View Post
    arctan (yi-y0)/(xi-x0) -> angle created by r anchored in x0,y0 in the point of impact xi,yi
    And that's what Physics.getAngle() does.

    Quote Originally Posted by Vaphell View Post
    calculate the difference and mirror that angle on the other side of radius. Use this newly created angle to calculate new movement vector

    ... or something
    And this is what I'm trying to figure out how to do.

  4. #4
    Join Date
    Feb 2009
    Location
    Kennesaw, Georgia
    Beans
    122
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Game Design. Circular Surface Deflection

    Here's a picture showing what I would like to happen.

    Scanned Document.jpg

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Game Design. Circular Surface Deflection

    a - movement angle
    b - radius angle
    ----------------
    c = (180+a)-b
    n = b-c = 2b-180-a

    let's check it: from the picture, a is roughly -80deg (object moving down), b 30deg
    c = 70deg
    n = 30deg-70deg = -40deg which is what you approximately see

  6. #6
    Join Date
    Feb 2009
    Location
    Kennesaw, Georgia
    Beans
    122
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Game Design. Circular Surface Deflection

    Quote Originally Posted by Vaphell View Post
    a - movement angle
    b - radius angle
    ----------------
    c = (180+a)-b
    n = b-c = 2b-180-a

    let's check it: from the picture, a is roughly -80deg (object moving down), b 30deg
    c = 70deg
    n = 30deg-70deg = -40deg which is what you approximately see
    Thank you so much. I'm going to try this out

  7. #7
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Game Design. Circular Surface Deflection

    Isn't this an object bounding off a flat surface?

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Game Design. Circular Surface Deflection

    doesn't matter (unless you are writing some serious physics simulator). You can assume that deflection off the circular surface = deflection off the tangent plane in the point of contact (dashed line on picture)

  9. #9
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Game Design. Circular Surface Deflection

    yea exactly. not sure what the confusing picture's all about then. Or even the mention of a sphere come to think of it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •