Results 1 to 7 of 7
  1. #1
    MrWhite117 is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Processing exercise, Need HELP!

    im 1st year mmu games design student and im stuck on this exercise, all i'm trying to do is get this ball to bounce around the screen and ive gotten so far but not sure how to proceed any suggestions.

    the ball keeps disappearing.

    //bouncing ball

    float x; //ball x position
    float dx = 3; //ball x direction is right, step 5
    float y; //ball y position
    float dy = 3;

    void setup () //runs once at start
    {
    size(500, 250);
    }

    void draw() //runs repeatedly
    {
    background(0); //black
    stroke(255, 0, 0); //pen red
    fill(255, 255, 0); //yellow
    ellipse(x, y, 10, 10);
    x = x + dx; //move ball x right
    y = y + dy; //move ball y

    //Collision detection
    //collide right hand edge?
    if (x>=500)
    dx = -dx; //reverse x direction

    else if (y>=250)
    dy = -dy;

    else if (x<=-500)
    dx = +dx;

    else if (y<=-250)
    dy = +dy;
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Processing exercise, Need HELP!

    I am not familiar with the framework this runs in, but is it really neccessary to set the background all the time? It looks like it's a race between the ball and the background.

    Other than that, you might want to take the width of the ball into account for the right hand edge. It bounces at x >= 500, but I assume the base coordinate of the ball is its top left. You would then want to bounce at x >= 500 - width_of_the_ball. Same for the bottom.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Processing exercise, Need HELP!

    Can you make a small complete program that compiles, executes and shows the problem?

    Be sure to wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Processing exercise, Need HELP!

    This is Processing, so this actually *is* an SSCCE/MCVE.

    And yeah, setting the background like that is correct. It's just clearing the previous frame.

    Anyway, you have two problems that I see:

    First, the y value of the top of the window is 0, but you're trying to bounce your ball off of y value -250, which is above the top of the window.

    Secondly, this doesn't do what you think it does:

    dy = +dy;

    You might be looking for an absolute value function here? If you're confused about what I'm saying, set up a little example program that uses the println function to print out the value of dy after the above line.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Processing exercise, Need HELP!

    Quote Originally Posted by KevinWorkman View Post
    T
    First, the y value of the top of the window is 0, but you're trying to bounce your ball off of y value -250, which is above the top of the window.

    Secondly, this doesn't do what you think it does:

    dy = +dy;
    Good catch!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    MrWhite117 is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Re: Processing exercise, Need HELP!

    Quote Originally Posted by SurfMan View Post
    I am not familiar with the framework this runs in, but is it really neccessary to set the background all the time? It looks like it's a race between the ball and the background.

    Other than that, you might want to take the width of the ball into account for the right hand edge. It bounces at x >= 500, but I assume the base coordinate of the ball is its top left. You would then want to bounce at x >= 500 - width_of_the_ball. Same for the bottom.
    thank you for the help but i figured it out in the end.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Processing exercise, Need HELP!

    Quote Originally Posted by MrWhite117 View Post
    thank you for the help but i figured it out in the end.
    "When you do things right, people won't be sure you've done anything at all..."
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 5
    Last Post: 03-19-2013, 12:30 PM
  2. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 06:54 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 05:48 PM
  4. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 11:14 AM

Tags for this Thread

Posting Permissions

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