top of page

Interactive Ball

  • Writer: Jessi Cruz
    Jessi Cruz
  • Nov 5, 2019
  • 1 min read

Code:

var brushSize;


var BallX;

var BallY;

var ballSize;


var xSpeed;

var ySpeed;


var bColor;


function setup() {

// put setup code here


createCanvas(1250, 620);


brushSize = 40;


ballX = 50;

ballY = 100;


ballSize = 65;


xSpeed = 10;

ySpeed = 5;


bColor = color(random(0, 255), random(0, 255), random(0,255))

mColor = color(0, 0, 0)

}


function draw() {

// put drawing code here


background(250);


fill(mColor);

ellipse(mouseX, mouseY, brushSize, brushSize);


fill(bColor);

ellipse(ballX, ballY, ballSize, ballSize);


ballX += xSpeed;

ballY += ySpeed;


if ((ballX > (1250-ballSize/2))||(ballX < ballSize/2)){

xSpeed = -xSpeed;


bColor = color(random(0, 255), random(0, 255), random(0, 255))

}

if ((ballY > (620-ballSize/2))||(ballY< ballSize/2)){

ySpeed = -ySpeed;


bColor = color(random(0, 255), random(0, 255), random(0, 255))

}


//if ((ballX = (mouseX - brushSize/2))&&(ballX = (mouseX + brushSize/2)){

if ((mouseX > (ballX-ballSize/2))&&(mouseX < (ballX+ballSize/2))&&(mouseY > (ballY-ballSize/2))&&(mouseY < (ballY+ballSize/2))){

xSpeed = -xSpeed;

ySpeed = -ySpeed;


bColor = color(random(0, 255), random(0, 255), random(0, 255))

mColor = color(random(0,255), random(0, 255), random(0, 255))

}

}

function mousePressed(){

background(250);

}

My goal is to create a program in which your mouse directly effect the ball. Eventually I want the mouse to draw and whatever it draws effects the ball still; sort of creating an environment for the ball to bounce around in.


The problem I am running into right now, is that when the ball touches the mouse, only the ySpeed changes and the ball gets stuck in the mouse, jittering in place, changing colors rapidly. I feel like if the xSpeed changed also it would bounce away before getting stuck in the ySpeed loop. I am not sure though.

Recent Posts

See All
Final Project: SnakeDodge

https://editor.p5js.org/jisojc/sketches/JD5ABmlI- My final project was initially going to be a top-down shooter using a different p5...

 
 
 
Youtube COPPA Algorithm

https://www.theverge.com/2019/11/13/20963459/youtube-google-coppa-ftc-fine-settlement-youtubers-new-rules Youtube was fined a large sum...

 
 
 
Bouncing Ball

https://editor.p5js.org/jisojc/present/1srYyYOP8 I created a second variable, using the same coding for xPos and xSpeed to create the y...

 
 
 

Comments


© 2023 by The Artifact. Proudly created with Wix.com

bottom of page