objects and collisions
in
Programming Questions
•
3 years ago
hello to all!
im new to programming and i am trying to make a class of objects that detecs another objects.
like a game, where the character collides with the environment.
i made a simple primitive test that worked fine... untill i decided to add a sencond object.
the first objects works fine and it "knows" when the "player" is nearby. the second acts like nothing is happening.
i dont have a clue why.
the code:
thanks in advance.
sorry if theres any "engrish"... my first language is portuguese.
EDIT: i forgot to say: i read lots of posts about collisions and stuff. all people cares are about circles colliding... oh god!
and, if i posted in the wrong forum, please, move it to the right one.
im new to programming and i am trying to make a class of objects that detecs another objects.
like a game, where the character collides with the environment.
i made a simple primitive test that worked fine... untill i decided to add a sencond object.
the first objects works fine and it "knows" when the "player" is nearby. the second acts like nothing is happening.
i dont have a clue why.
the code:
- int x, y;
- //if left, right, up or down (in this order) directions are possible
- boolean esq, dir, aci, aba;
- ArrayList objs;
- void setup(){
- objs = new ArrayList();
- size(800, 600, P2D);
- x = y = 100;
- //initialy, all directions are possible
- esq = dir = aci = aba = true;
- }
- void draw(){
- background(0);
- //the "player"
- fill(255);
- rect(x, y, 10, 10);
- //add 1st object
- objs.add(new Obj(200, 200));
- //add 2nd object (uncomment to work)
- objs.add(new Obj(400, 300));
- for (int i = objs.size()-1; i >= 0; i--){
- Obj t = (Obj)objs.get(i);
- t.Onde();
- t.Mostra();
- }
- textSize(20);
- fill(255);
- text("acima " + aci + " abaixo " + aba + " esquerda " + esq + " direita " + dir, 15, 15);
- text(mouseX + " " + mouseY, 30, 30);
- text(x + " " + y, 45, 45);
- }
- class Obj{
- int X, Y;
- Obj(int eixoX, int eixoY){
- X = eixoX;
- Y = eixoY;
- }
- void Onde(){
- //tests collisions
- if ((y + 11 > Y) && (Y + 31 > y)){
- if (x + 11 == X){dir = false;}
- else if (x == X + 31){esq = false;}
- else{esq = true; dir = true;}
- }
- else if ((x + 11 > X) && (X + 31 > x)){
- if (y + 11 == Y){aba = false;}
- else if (y == Y + 31){aci = false;}
- else{aba = true; aci = true;}
- }
- else{aci = aba = esq = dir = true;}
- }
- void Mostra(){
- //displays the objects
- fill(200, 100, 100);
- rect(X, Y, 30, 30);
- }
- }
- void keyPressed(){
- if (key == CODED){
- if (keyCode == UP){
- if (aci){y -= 1;}
- }
- else if (keyCode == DOWN){
- if (aba){y += 1;}
- }
- else if (keyCode == RIGHT){
- if (dir){x += 1;}
- }
- else if (keyCode == LEFT){
- if (esq){x -= 1;}
- }
- }
- }
- void mouseClicked(){
- x = mouseX;
- y = mouseY;
- }
thanks in advance.
sorry if theres any "engrish"... my first language is portuguese.
EDIT: i forgot to say: i read lots of posts about collisions and stuff. all people cares are about circles colliding... oh god!
and, if i posted in the wrong forum, please, move it to the right one.
1