Hi there,
I wanted to use this collision detection system http://jeffreythompson.org/collision-detection/circle-rect.php
in an older code of mine because i thought it could make the if statements less complicated by just using one boolean function.
Problem is that my whole program seems to get stuck at some point. When debugging i get to finish the draw function one more time, then it freezes.
The freezing happens
- when adding the FAT part of code that puts the ball out of the platform
- after reversed velocity.y was added the first time, when o.i == 4
- after the draw function finished
Ball ball = new Ball();
int size = 1;
Platform[] platforms = new Platform[size];
boolean CHECK;
void setup()
{
size(300, 400);
for (int i=0; i<size; i++) {
platforms[i] = new Platform();
}
}
void draw()
{
background(23);
for (int i=0; i<size; i++) {
ball.bounceplatform(platforms[i]);
if(platforms[i].j > 0|| platforms[i].i > 0) {
CHECK = true;
}
}
if(!CHECK) {
ball.move();
}
for (int i=0; i<size; i++) {
platforms[i].display();
}
ball.display();
CHECK = false;
}
class Ball
{
PVector xy;//current position
PVector velocity;
float w, h, d=40;
color col;
float gravity;
int j;
Ball()
{
xy = new PVector (100, 100);
velocity = new PVector (0, 0);
w = d;
h = d;
col = color(20, 490, 430 );
gravity = 9.81/60/6;
}
void display()
{
fill(col);
ellipse(xy.x, xy.y, w, h);
}
void move()
{
//normal movement
//velocity increases
velocity.y += gravity;
//position gets updated
xy.add(velocity);
}
void bounceplatform(Platform o) {
//if platform hit
if (collision(o)) {
//bounce from above
if (velocity.y > 0 && o.j == 0||o.i>0) {
o.i++;
if (o.i < 4) {
if (xy.y + h/2 < o.xy.y) {
move();
}
}
if (o.i >= 4) {
//if (xy.y + h/2 > o.xy.y) {
move();
//}
}
if (o.i == 3) {
velocity.y = -abs(velocity.y);
}
if (o.i == 6) {
o.i = 0;
}
}
}
}
boolean collision(Platform o) {
float eX = xy.x, eY;
float distEF, distX, distY;
if (xy.x <= o.xy.x) eX = o.xy.x;
else if (xy.x >= o.xy.x+o.w) eX = o.xy.x + o.w;
else eX = xy.x;
if (xy.y <= o.xy.y) eY = o.xy.y;
else if (xy.y >= o.xy.y+o.h) eY = o.xy.y+o.h;
else eY = xy.y;
//calculate distance of xy from e
distX = xy.x - eX;
distY = xy.y - eY;
distEF = distX*distX + distY*distY;
//in case the distance is smaller diameter/2 return true
if (distEF <= h/2*h/2) {
return true; //<>//
} else return false;
}
}
class Platform
{
PVector xy;
color col;
float w, h;
int i = 0, j = 0;
boolean CHECK = false;
Platform()
{
xy = new PVector(100, 200);
col = color(20, 490, 430 );
w = 50;
h = 4;
}
void display()
{
fill(col);
rect(xy.x, xy.y, w, h, 2);
}
}
…but i dont know why. If you do, please tell me reason.
Thx