We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, trying to figure out how to do collision detection with squares/Rectangles unsure how to figure out. I have a base idea on how to find the distance, however i'm unsure on what to compare it to.
void collisionDetection(){
for(int i = Walls.size()-1; i>=0 ; i--){
Wall current = Walls.get(i);
for(int b1 = tank1Bullet.size()-1; b1>=0; b1--){
tank1Bullet currentBullet = tank1Bullet.get(b1);
//this is problem the problem
if(dist(currentBullet.x,currentBullet.y,current.X,current.Y)<(current.Width)){
tank1Bullet.remove(b1);
//Walls.remove(i);
}
}
class Wall{
float Width;
float Height;
float X;
float Y;
Wall(float x,float y,float w, float h){
Width = w;
Height = h;
X = x;
Y = y;
}
void display(){
fill(0);
rect(X,Y,Width,Height);
}
}
class tank1Bullet extends PVector{
PVector location;
float rotation, bullet_Speed;
tank1Bullet(){
location = new PVector(tank1Location.x, tank1Location.y);
rotation = tank1Rotate;
bullet_Speed = 20;
}
void update(){
location.x = location.x + sin(rotation)*bullet_Speed;
location.y = location.y - cos(rotation)*bullet_Speed;
//condition to removes the bullet from the arrayList if it is off the screen
if (location.x > 0 && location.x < width+500 && location.y > 0 && location.x < height+500) {
}
else {
tank1Bullet.remove(b1);
}
}
void draw_Bullet(){
ellipse(location.x,location.y , 10, 10);
}
}
Answers
can we have the entire code please
Here is an in-depth example of Rectangle / Rectangle collision detection.
This also works with your case, as a square is a type of rectangle.
I can see what they're doing not having any luck tho
this is what i've got and still don't know why it doesnt work.
used exactly as he had
Collision detection between two squares of different size. Code below.
The idea is this. Imagine you have two squares with the same dimensions. Now bring them side to side. How do you know if there is collision?
If you keep your left square fixed in space and move the right square toward the left, you get collision as soon as the left corner of the square (either top or bottom... or the whole edge) is between the edges of the other square. For rectangles and squares, you need to check per dimension (along X first, then Y or vice versa).
However it is not enough to check the left corner/edge of the rectangle. You need to do the same check for the right corner/edge, in case you approach the fixed square from the other side (going left to right).
As a mention before, you need to do these checks also along the Y dimension. This is the reason I have two conditionals below, and you need to satisfy both conditionals to have a full collision going on.
Notice this algorithm is only tested for this example. It might not include all the possible scenarios. It also assumes you are working with squares. This is the reason it is better to use a library as it has proper tested code.
Kf
how do you do collision with a circle and a square then? because atm i'm trying to figure out when the bullet(the ellipse) hits the wall it removes the bullet. Sorry i just realised i worded my question wrong
It is a bit more challenging to write the code but not impossible. I ran out of time today. An easy solution is to draw a square but to model your square as a circle of diameter equal to the side length of the square. In other words:
but when you check for collisions, you assume the square is a circle and you use the collision detection between two circles:
if(dist(x1y1,x2,y2)<(r+len)) { ... collision detected...}
although this model is not totally correct, it will give you a very good approximation. You could continue writing you software based on this approach and if you ever decide to change into something more accurate, you can changed the above line for the proper circle-square collision function.
Kf
For a working example, choose the type of collision that you want from this list:
For example, Circle/Rectangle:
I've decided to scrap that idea I couldn't figure it out. I'll use your approach @kfajer. Im going to change the walls to circles (idea works just as well and is alot easier)
This is what i have when the collision function is active it just removes all the walls
Remove
tank1KeyMovement()
from draw and replace tank1KeyMovement with keyPressed like this:Another change: You classes don't need to extend from PVector so remove any reference to
extends PVector
.Another change: Your class names should start with a capital letter. Example: Wall. However, functions and variable names, including containers, should start with lower case as it is the case for Walls. It should be "walls".
The next code is partially complete. You need to handle ship-wall interactions similar to what I do btw bullet-wall interactions.
A final note: Any information related to your ship should be manage by your ship class. For example, you have an acceleration vector in a global scope. This acceleration vector describes your ship motion. It will be naturally to be defined and managed inside your ship class.
Kf
Thank you very much. Clear concise answers :)