# Need a little Help

**URL:** <https://discourse.processing.org/t/need-a-little-help/22874>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 28, 2020, 7:23am UTC](https://discourse.processing.org/t/need-a-little-help/22874 "2020-07-28T07:23:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_ahnafpiash](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_ahnafpiash/32/10108_2.png) [@the\_ahnafpiash](https://discourse.processing.org/u/the_ahnafpiash)\
**Post date:** [July 28, 2020, 7:23am UTC](https://discourse.processing.org/t/need-a-little-help/22874/1 "2020-07-28T07:23:55Z")

</div>

/\* I am trying for so long to solve my assignment but I am stuck… need a little help please!  
here is my assignment question:  
Create a ball of predefined size that bounces around the canvas, reversing directions when  
it contacts any edge of the canvas.

- The ball should start with its center 1/4 of the way from the top of the canvas and 1/4 of  
the way from the left edge of the canvas.
- The ball should never be able to exit the canvas by any amount.  
• Modify your program so that the ball travels at a specific angle, instead of using predefined  
X and Y speeds.
- Use a global state variable to store the angle at which the ball is traveling. The  
TOTAL\_SPEED of the ball should be 3 pixels per frame. This will  
require trigonometry!
- Think of the TOTAL\_SPEED of the ball as the length of a right angle  
triangle, and then calculate the X and Y speeds of the ball  
using the angle and TOTAL\_SPEED .
- Give the ball a random direction (angle) when the program starts.
- When the ball hits an edge, you can simply invert its angle (add PI).

\*/

//Here is my code

final int TOTAL\_SPEED=3;  
float angle;  
float bounces = 0;  
float mousex;  
float mousey;  
float direction;  
float m;  
boolean ballMove;

float ballX,ballY; //Its initial position - somewhere “random”  
final int B\_SIZE = 30; //Its diameter - it will be a circle  
final int BALL\_COLOUR = #FFFF00; //Its colour - this is “leather brown” maybe

float ballSpeedX; //Move 3 pixels every 60th of a second  
float ballSpeedY; //Move only 2 pixels every 60th of a second

void setup(){  
size(1000,500);  
ballX=width/4;  
ballY=height/4;  
ballSpeedX = 3;  
ballSpeedY = 2;  
ballMove = false;  
//direction = PI;

}

void draw(){

background(0); //Erase the old ball  
mouse();  
drawBall();  
println(mousex,mousey,mouseX,mouseY,ballMove);  
}

void drawBall()

{ if (ballMove)

```
 {moveBall();}

```

fill(BALL\_COLOUR);  
ellipse(ballX,ballY,B\_SIZE/1.5,B\_SIZE/1.5);  
}

void moveBall()

{

if (ballX\>(width-B\_SIZE/2) || ballX\<B\_SIZE/2 || ballY\>height-B\_SIZE/2)  
{  
ballSpeedX=0;  
ballMove=false;  
}  
if (ballY\>height-B\_SIZE/2 || ballY\<B\_SIZE/2)  
{  
ballSpeedY=0;  
ballMove=false;

}  
ballX+=ballSpeedX;  
ballY+=ballSpeedY_tan(0.5_QUARTER\_PI);

println(sin(0.9\*QUARTER\_PI));  
}

void mouse()  
{if (mousePressed)  
{ mousex=mouseX;  
ballMove = true;  
}

}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 28, 2020, 7:51am UTC](https://discourse.processing.org/t/need-a-little-help/22874/2 "2020-07-28T07:51:52Z")

</div>

It might help to vastly simplify your code.

You’ll only need the following variables:

- The constant `TOTAL_SPEED`
- Two floats for the X and Y positions of the ball.
- One float for the angle of the ball’s movement.
- One float for the radius of the ball.

This leads to the following code:

```auto
final float s = 3; // "TOTAL_SPEED"
float x, y, a, r;

void setup(){
  size(600,400);
  x = width * .25;
  y = height * .25;
  a = random(TWO_PI);
  r = 15;
}

void draw(){
  background(0);
  move_ball();
  draw_ball();
}

void move_ball(){
  x += ???; // TODO - How does the x component of the speed change? It's based on the angle and the total speed!
  y += ???; // TODO - How about the Y component?
  if( x <= r ){ // Left bounce.
    // TODO - What happens when the ball bounces?
  }
  if( x+r >= width ){ // Right bounce.
    // TODO - What happens when the ball bounces?
  }
  if( y <= r ){ // Top bounce.
    // TODO - What happens when the ball bounces?
  }
  if( x+r >= width ){ // Bottom bounce.
    // TODO - What happens when the ball bounces?
  }
}

void draw_ball(){
  fill( 0,200,0 );
  noStroke();
  ellipseMode(CENTER);
  ellipse(x,y,2*r,2*r);
}

```

---

<div class="post-metadata">

**Author:** ![the\_ahnafpiash](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_ahnafpiash/32/10108_2.png) [@the\_ahnafpiash](https://discourse.processing.org/u/the_ahnafpiash)\
**Post date:** [July 28, 2020, 8:10am UTC](https://discourse.processing.org/t/need-a-little-help/22874/3 "2020-07-28T08:10:21Z")

</div>

I can’t figure out how to put the angle in this code.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 28, 2020, 8:41am UTC](https://discourse.processing.org/t/need-a-little-help/22874/4 "2020-07-28T08:41:37Z")

</div>

Draw a diagram. You have a ball, which has a center. There’s a direction it’s going in, so draw an arrow from the center to represent the speed. What’s the angle? What parts of the triangle are the X and Y components? Do you know enough calculus?

Post your diagram for more help.
