Results 1 to 2 of 2

Thread: bouncy?

  1. #1
    saintxxrow is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Question bouncy?

    I'll get straight to it. I have created a ballcourt class and I need to make a circle object to move diagonally round the ballcourt, bouncing off it's edges.
    Not too difficult. I can get the ball to move diagonally, but I can't get the ball to bounce off in another direction. I am using a while loop to do the diagonal work like so: (using boolean methods with this.)

    public void ricochet (int x, int y, int hits)
    {
    int hitCount = 0;

    while(hitCount < hits)
    {
    x = 1 + this.getBallXPos();
    y = 1 + this.getBallYPos();
    this.setBallXPos(x);
    this.setBallYPos(y);
    this.delay(20);
    //Needs to be in a while loop

    if (this.isTouchingLeftOrRight()) //obvious desdriptive methods
    {
    hitCount = hitCount + 1;
    this.setBallXPos(this.getBallXPos() - 1);
    this.delay(20);
    //this is the problem above. it needs to be in this sort of structure, need a solution, but not sure what to do.PLEASE HELP!! :)
    }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Instead of adding one to x, consider using a variable to remember how much x is supposed to increment.

    Java Code:
    public void ricochet (int x, int y, int hits)
    {
        int hitCount = 0;
        int deltaX = 1;
    
        while(hitCount < hits)
        {
            x = this.getBallXPos() + deltaX;
            // etc
    Now when you detect a horizontal collision with a wall change deltaX appropriately.

Similar Threads

  1. Legion of the Bouncy Castle Java Cryptography API 1.39
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-03-2008, 04:27 PM
  2. Legion of the Bouncy Castle Java Cryptography API 1.38
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-12-2007, 05:53 PM
  3. Replies: 0
    Last Post: 06-16-2007, 09:42 AM

Posting Permissions

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