[Solved]

Understanding the language, error messages, etc.

[Solved]

Postby minecraftlog21 » Sun Jan 11, 2015 4:01 am

I am developing a game for the gamebuino And I have ran into a Problem where one of collision detectors is not working.

Image
(This is the bug as you can see the ship goes threw the barrier up top.)
Code: Select all

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
int ShipXDisp = 15;
int ShipYDisp = 15;
int ShipXLoc = 0;
int ShipYLoc = 0;
int ShipFace = 0;
static byte PROGMEM Ship[] =
{
  7,7, //width and height
  B00010001,
  B00111001,
  B00111001,
  B00111001,
  B01111101,
  B11111111,
  B00111001,
};
void setup(){
  gb.begin();
  gb.titleScreen(F("Game"));
}

void loop(){
 
  if(gb.collideRectRect(ShipXDisp, ShipXDisp, 7, 7, 0, 0, 8, 48)){
    //ShipYDisp = ShipYDisp + 1;
    ShipXDisp = ShipXDisp + 1;
  }
  if(gb.collideRectRect(1, 1, 83, 7, ShipXDisp, ShipXDisp, 7, 7)){
    ShipYDisp = ShipYDisp + 1;
    gb.popup(F("Colide"),40);
  //  ShipXDisp = ShipXDisp + 1;
  }
 
  if(gb.update()){
    gb.display.drawRect(0, 0, 8, 48);
    gb.display.drawRect(0, 0, 84, 8);
   
    if(gb.buttons.timeHeld(BTN_UP)){
      ShipYDisp = ShipYDisp - 1;
      ShipYLoc = ShipYLoc - 1;
      ShipFace = 0;
    }
    if(gb.buttons.timeHeld(BTN_DOWN)){
      ShipYDisp = ShipYDisp + 1;
      ShipYLoc = ShipYLoc + 1;
      ShipFace = 2;
    }
     if(gb.buttons.timeHeld(BTN_LEFT)){
      ShipXDisp = ShipXDisp - 1;
      ShipXLoc = ShipXLoc - 1;
      ShipFace = 1;
    }
     if(gb.buttons.timeHeld(BTN_RIGHT)){
      ShipXDisp = ShipXDisp + 1;
      ShipXLoc = ShipXLoc + 1;
      ShipFace = 3;
    }

    gb.display.fillCircle(15 - ShipXLoc, 20 - ShipYLoc, 5);
    gb.display.drawBitmap(ShipXDisp,ShipYDisp,Ship,ShipFace,0);
    gb.display.print(ShipXLoc);
    gb.display.print("_");
    gb.display.print(ShipYLoc);
    gb.display.print(" ");
    gb.display.print(ShipXDisp);
    gb.display.print("_");
    gb.display.print(ShipYDisp);
  }
}
Last edited by minecraftlog21 on Sun Jan 18, 2015 10:38 pm, edited 1 time in total.
minecraftlog21
 
Posts: 8
Joined: Sat Dec 27, 2014 5:24 pm

Re: HELP : I can't get collision working

Postby Jamish » Sun Jan 11, 2015 8:39 am

Looks like the problem is that you were calling "ShipXDisp, ShipXDisp" instead of "ShipXDisp, ShipYDisp" in both of your collide calls.

Also, your gb.collideRectRect checks should go inside gb.update(), otherwise that code is getting called a LOT more than once per frame :)

Here's your fixed code:
Code: Select all

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
int ShipXDisp = 15;
int ShipYDisp = 15;
int ShipXLoc = 0;
int ShipYLoc = 0;
int ShipFace = 0;
static byte PROGMEM Ship[] =
{
  7,7, //width and height
  B00010001,
  B00111001,
  B00111001,
  B00111001,
  B01111101,
  B11111111,
  B00111001,
};
void setup(){
  gb.begin();
  gb.titleScreen(F("Game"));
}

void loop(){
  if(gb.update()){
    if(gb.collideRectRect(ShipXDisp, ShipYDisp, 7, 7, 0, 0, 8, 48)){
      //ShipYDisp = ShipYDisp + 1;
      ShipXDisp = ShipXDisp + 1;
    }
    if(gb.collideRectRect(1, 1, 83, 7, ShipXDisp, ShipYDisp, 7, 7)){
      ShipYDisp = ShipYDisp + 1;
    //  ShipXDisp = ShipXDisp + 1;
    }
   
    gb.display.drawRect(0, 0, 8, 48);
    gb.display.drawRect(0, 0, 84, 8);
   
    if(gb.buttons.timeHeld(BTN_UP)){
      ShipYDisp = ShipYDisp - 1;
      ShipYLoc = ShipYLoc - 1;
      ShipFace = 0;
    }
    if(gb.buttons.timeHeld(BTN_DOWN)){
      ShipYDisp = ShipYDisp + 1;
      ShipYLoc = ShipYLoc + 1;
      ShipFace = 2;
    }
     if(gb.buttons.timeHeld(BTN_LEFT)){
      ShipXDisp = ShipXDisp - 1;
      ShipXLoc = ShipXLoc - 1;
      ShipFace = 1;
    }
     if(gb.buttons.timeHeld(BTN_RIGHT)){
      ShipXDisp = ShipXDisp + 1;
      ShipXLoc = ShipXLoc + 1;
      ShipFace = 3;
    }

    gb.display.fillCircle(15 - ShipXLoc, 20 - ShipYLoc, 5);
    gb.display.drawBitmap(ShipXDisp,ShipYDisp,Ship,ShipFace,0);
    gb.display.print(ShipXLoc);
    gb.display.print("_");
    gb.display.print(ShipYLoc);
    gb.display.print(" ");
    gb.display.print(ShipXDisp);
    gb.display.print("_");
    gb.display.print(ShipYDisp);
  }
}
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: HELP : I can't get collision working

Postby minecraftlog21 » Sun Jan 11, 2015 9:41 pm

Jamish wrote:Looks like the problem is that you were calling "ShipXDisp, ShipXDisp" instead of "ShipXDisp, ShipYDisp" in both of your collide calls.

Also, your gb.collideRectRect checks should go inside gb.update(), otherwise that code is getting called a LOT more than once per frame :)


Thanks for the fixed code shows how carful you need to be :D
minecraftlog21
 
Posts: 8
Joined: Sat Dec 27, 2014 5:24 pm

Re: HELP : I can't get collision working

Postby Jamish » Mon Jan 12, 2015 5:39 pm

minecraftlog21 wrote:Thanks for the fixed code shows how carful you need to be :D


Careful indeed. I make those kind of mistakes all the time, haha.
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 16 guests

cron