Have gotten down how to make a ball bounce with gravity. Now I want that ball to hit a moving object and bounce off it. Any help on how to to do that?
-a- check ( every draw loop ) for the distance between the 2 objects
https://processing.org/reference/dist_.html
if that is smaller as the sum of the 2 radius BOUNCE
-b- for the bounce could have easy … complicated rules:
- just change the speed vector of the ball
- change speed vector of ball and object
- apply physics math of impuls F * t = mass * delta speed m*dv
- if not center collision find a rebounce angula
the x-value of my object is mouseX.
Actually, ill send my code if you would like to help:
float circleX=1; //direction of ball
float circleY=1; //direction of ball
float speedX=3;//speed of ball
float speedY=1;
float xpos; //position of ball
float ypos; //position of ball
void setup() {
size(640,480);
//starting position of ball
xpos=0;
ypos=0;
}
void draw() {
//background of game
background(44,56,245);
fill(2,188,34);
noStroke();
rect(0,440,width,height);
//Bill Movement
pushMatrix();
translate(mouseX,280);
Bill();
popMatrix();
//Ball
ypos+=speedY;
xpos+=speedX;
//Ball
fill(180);
stroke(0);
strokeWeight(2);
ellipse(xpos,ypos,50,50);
//if ball goes out of area, turn it back
if(xpos>width-50 || xpos<0){
speedX*=-1;
}
//grav
if(ypos>height-50){
speedY*=-1*.99;
}else{
speedY+=1;
}
if(ypos<0){
speedY*=-1;
}
//Ball reset
if(speedY==0){
xpos=0;
ypos=0;
}
}
Btw my object is Bill()
Can’t, i have to have it included for my question.
Basically im supposed to juggle the ball on my characters head
Okay, I just wanted to show you the example
Chrisir
thanks anyway,
Bobby
pls format you code by
PASTE it into
</> code formatter
and i think there is the code for the other object missing
did you start about the distance calculation?
Sorry about that! Yeah i was abit confused about the distance from the points as the ball would constantly bouncing, while the character would follow my mouseX.
i left out the code of the character as i thought would be abit messy and that its just a group of shapes lol. Thank you for spending ur time to help me btw.
so you just make it a rectangle.
bill.x by mouseX, bill.y fix 280,
the width bill.w of the rect would be critical for the bounce
( collision check with the ball )
in words:
if ball ( center ) posx > bill.x but < bill.x+bill.w a bounce is possible.
when ball posy < bill.y ( and fall down ) it will hit:
if ball posy+radius >= bill.y
and what would happen then? bounce up like from ground?
ill show you my character. I do not understand how i can make a variable for Bills position;
void Bill(){
fill(0);
rect(100,100,60,50); //head
fill(255);
rect(110,120,15,5); //left eye
rect(135,120,15,5); //right eye
ellipse(130,136,10,2); //mouth
fill(0);
rect(85,96,90,4); //hat
fill(250,105,73);
rect(110,150,40,20); //body
fill(0);
rect(110,170,5,10); //left foot
rect(145,170,5,10); //right foot
}
the bounce line would be top of HAT?
bill_x = mouseX+85; // translate + hat x
bill_y = 280+96; // fix in translate + hat y
bill_w = 90; // hat rect w
billX= mouseX + 85;
billY= 280 + 96;
billW= 90;
if(xpos == billX){
speedX*=-1;
}
if (ypos == billY){
speedY*=-1;
}
it bounces off something now, im not sure what. Also im not sure how to implement that the width of the hat is area to bounce off- reason to why billW is not being used
- untested idea
billX= mouseX + 85;
billY= 280 + 96;
billW= 90;
if((xpos >= billX) && (xpos <= billX+billW)) {
if (ypos + radius >= billY){
speedX*=-1;
speedY*=-1;
}
}
Hi, did you manage to get your code to work?
No sadly the code will still not make the ball bounce of the hat.
I think the reason has to do with the positioning of my character. Maybe i should make my hat a separate entity and work from there?
I have a similar question and i have no idea how to work it out as well that’s why i was asking