How to get a more natural movement?
in
Programming Questions
•
3 months ago
Hi everyone,
I am building a project that (eventually) the user will be able to bat away balloons(it working by doing a hit test with the mouse atm),its not thrilling i know but I figured it would be a good exercise to help me work towards something a bit more interesting!
I have the ballon class working nicely, the balloons reappear once off the screen etc and the hit test works well, but when they are hit i want them to behave more like a real ballon.
I guess i need some kind of easing but not to sure how to implement it correctly.
I tried adding a targetX/Y variable and a bit of randomisation but i think due to where i placed the variable it kept looping round and changing the targetX making the movement really jittery!
Here my code I hope someone can help!!!
Regards,
Oli
PImage smile;
PImage ballon;
int numBalls = 10;
Ball [] balls = new Ball [numBalls];
void setup() {
size(500, 500);
smooth();
smile = loadImage("smile.jpg");
ballon = loadImage("teal.png");
for ( int i = 0; i < 10; i++) {
balls[i] = new Ball(random(width)+10, random(height)+10, int(random(30)+10));
}
}
void draw() {
image(smile, 0, 0);
//creates 10 different methods
for (int i = 0; i < 10; i++) {
balls[i].display();
balls[i].hit();
balls[i].move();
}
}
class Ball {
float x;
float y;
float d = dist(mouseX, mouseY, x, y);
int diameter;
Ball(float tempX, float tempY, int tempDiameter) {
x =tempX;
y = tempY;
diameter = tempDiameter;
}
//draws the ballon
void display() {
image(ballon, x, y);
}
//controls movement
void move() {
y+=2+random(0, 3); // drop speed
if (y > height + ballon.height) { // if drops off screen send to
y = 0 - ballon.height; // top - height.
x = random(width);
}
}
// check to see if the mouse is inside the image
void hit() {
if (dist(x, y, mouseX, mouseY) < ballon.height || dist(x, y, mouseX, mouseY) < ballon.width) {
float targetX = x;
float targetY = y;
float easing = 0.01;
x += (targetX - x) * easing;
y += (targetY - y) * easing;
x +=random(50)-25;
y +=random(25);
}
}
}
1