Adding velocity to a ball
in
Programming Questions
•
2 years ago
Hey, another noob question here, sorry. Currently Im trying to make a little piece of code in which you simply click and drag a ball to throw it around an area. It's all very amatuer code and I'm stuck with getting the physics of it going. It can drop and rise on the y axis, but velocity still has no impact on it and the user can't "throw" the ball. if the mouse is released it just drops from where it is. I've been looking into PVectors as a way to solve it, but it's lost me. I've also looked at a few examples of what I want to achieve online, but obviously I don't want to just straight copy, I want to develop my own code Any ideas on how to get this working would be greatly appreciated. Oh and the RGB stuff is just for colour
.
Here's what I have so far
.
Here's what I have so far
- int greenfill = 250;
int redfill = 0;
int bluefill = 0;
int count = 1;
float speed = 0;
float gravity = 0.1;
float x = (700/2);
float y = (400/2);
//just the basic floats and ints
void setup(){
size (700,400);
background(255,255,255);
}
void draw(){
background(255,255,255,10);
noStroke();
smooth();
fill(redfill,greenfill,bluefill,100);
ellipse(x,y,50,50);
//draws the ellipse and the background
if(count<2){
}
dist(x, y, mouseX, mouseY);
if(dist(x, y, mouseX, mouseY) < 60){
if (mousePressed == true) {
x = mouseX;
y = mouseY;
}
}
// enables the mouse to click and drag on the ball
if(keyPressed) {
if(key == 'r' || key == 'r') {
redfill = 250;
greenfill = 0;
bluefill = 0;
} }
if(keyPressed) {
if(key == 'g' || key == 'g') {
greenfill = 250;
redfill = 0;
bluefill = 0;
}}
if(keyPressed) {
if(key == 'b' || key == 'b') {
greenfill = 0;
redfill = 0;
bluefill = 250;
}}
//the three different colours the ball can be from the buttons r, g and b
y = y + speed;
speed = speed + gravity;
if(y > 395){
//gravity on the ball
speed = speed * -.95;
}
if (y < 50){
speed = speed * -.95;
}
//what stops the ball falling off the bottom or flying out the top. doesn't work so well as of yet
}
1