gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > need help with collision detection

#92583 - AdamZZ - Fri Jul 14, 2006 12:09 am

im making a Pong game for ds to learn how to make ds games but i cant make a collision detection. does this exist?
if (PA_GetSpriteCollision(screen,sprite#1,sprite#2))
{
// code to be executed when coliding.
}


#1 = the sprite to use in my case ball that has the number3
#2 = sprite to colide with

#92645 - Zed0 - Fri Jul 14, 2006 9:47 am

No, that command doesn't work.
If you are using PAlib then see this tutorial for collision detection, both rectangular and circular.
_________________
__Zed0__
FlasheMe'd Silver DS - GBAMP-CF - 512MB CF Card - R4DS - 2GB Micro SD Card

#92682 - AdamZZ - Fri Jul 14, 2006 1:09 pm

when i tried it it didnt work :S can some1 post a collision code ?

Code:

#include <PA9.h>
 
// PAGfxConverter Include
#include "gfx/all_gfx.c"
#include "gfx/all_gfx.h"
 
int main(void){
 
   PA_Init(); //PAlib inits
   PA_InitVBL();
   
   PA_LoadSpritePal(0, // Screen
         1, // Palette number
         (void*)paddle_Pal);   // Palette name
               
   PA_LoadSpritePal(0, // Screen
         2, // Palette number
         (void*)ball_Pal);   // Palette name
               
   PA_CreateSprite(0, // Screen
         1, // Sprite number
         (void*)paddle_Sprite, // Sprite name
         OBJ_SIZE_8X32, // Sprite size
         1, // 256 color mode
         1, // Sprite palette number
         243, 95); // X and Y position on the screen
         
   PA_CreateSprite(0, // Screen
         3, // Sprite number
         (void*)ball_Sprite, // Sprite name
         OBJ_SIZE_8X8, // Sprite size
         1, // 256 color mode
         2, // Sprite palette number
         127, 95); // X and Y position on the screen
         
   PA_CreateSprite(0, // Screen
         2, // Sprite number
         (void*)paddle_Sprite, // Sprite name
         OBJ_SIZE_8X32, // Sprite size
         1, // 256 color mode
         1, // Sprite palette number
         5, 95); // X and Y position on the screen
 
   while(1) // Infinite loops
   {
        PA_WaitForVBL();
      
      if(Stylus.Held == 1)
      {
         PA_SetSpriteXY(0,1,243,Stylus.Y);
         PA_SetSpriteXY(0,2,5,Stylus.Y);
      }
   }
return 0;
}


this is the code i use to make the sprites and to move the two paddles
please change this to make it work with the rectangle collision.

#92697 - Zed0 - Fri Jul 14, 2006 3:58 pm

Well for a start you're going to have to set up variables for each sprite saying where they are and their sizes, then get onto the collision code in same way as the tutorial I posted.
BTW, no one is just going to do it for you.
_________________
__Zed0__
FlasheMe'd Silver DS - GBAMP-CF - 512MB CF Card - R4DS - 2GB Micro SD Card

#92715 - AdamZZ - Fri Jul 14, 2006 5:00 pm

ok but how do i set varibles for the sprites??

#92762 - naleksiev - Fri Jul 14, 2006 9:01 pm

Hey I don't see your ball moving how you expect to have collisions. Try something more simple like this. I made you a collision detection with the screen bound.

I hope I didn't messed up something :(

Code:

#define SCR_WIDTH 256
#define SCR_HEIGHT 256
#define BALL_DIAMETER 8


....

int ballX =  127;
int ballY = 95;
int ballSpeedX = 1;
int ballSpeedY = 1;
int paddleX = 5;
int paddleY;

while(1) // Infinite loops
{
     PA_WaitForVBL();

     ballX += ballSpeedX;
     ballY += ballSpeedY;

     if (ballX <= 0 || ballX >= SCR_WIDTH - BALL_DIAMETER)
     {
          ballSpeedX = -ballSpeedX;
     }
     if (ballY <= 0 || ballY >= SCR_HEIGHT - BALL_DIAMETER)
     {
          ballSpeedY = -ballSpeedY;
     }

     if(Stylus.Held == 1)
     {
          PA_SetSpriteXY(0,1,243,Stylus.Y);
          PA_SetSpriteXY(0,2,5,Stylus.Y);

          PA_SetSpriteXY(3,2,ballX,ballY);

          paddleY = Stylus.Y;
     }
}



It's not so complex try to understand it and because you have the ballX, ballY, paddleX, paddleY you will be able to try to implement the collision with the paddle by your own.

Next time show your collision code that you tried and it's not working and maybe more people will help you. Also if you realy wrote something probably it will be completly different from the code that someone will send to you and you will not be able to compare and find you misstake.

#92788 - AdamZZ - Fri Jul 14, 2006 11:16 pm

i didnt want to make the bal move until i got collision :S

thanks for helping me :D

but that code doesnt work :S it when trying to build the nds it gives me a error on line 80 and line 80 is the } of while(1) whats wrong?
main.c: error: expected declaration or statement at end of input (im not english im swedish) and i dont know what they mean :S

im sorry if im a little "give me more" type but im new to this and i need help. :S if you would be the one that needs help i would help you cause i know how it is to be new to something. (im a very good VB6 and Actionscript programmer) im not new to programing but im new to nds and it has some code thats similiar too actionscript (maybe cause actionscript is based on c and nds coding is made in c :D)

i guess that this is the code for collision on the paddle.
Code:
if (ballX <= paddleX && ballY >= paddleY - 32 && ballY <= paddleY )
(made a pong game in VB6 that uses the same code (well almost))

#92880 - naleksiev - Sat Jul 15, 2006 4:33 am

About the error I think there is missing { or } in your code. I do not have PA_lib and I didn't try to compile the code that I gave to you. I check it againt and it looks fine to me. The best way to check for {} is if you always indent your code but for example I use Edit Plus to code some times and there is a shortcut Ctrl+] which you can use is when you are pointing on some bracket to go on the oposite one. I think most of the text editors that are made for developers support this shortcut :)

To start debuging by your self if you do not understand the error you can at least try to remove some of the lines. You just add about 10 lines and your code can't be compiled anymore just remove all and add them one by one to see what's wrong.

I'm glad you try to write something :) But I believe that your anchor point for the sprite must be the very left top point so the code will be like this.

Code:
if (ballX <= paddleX && ballY >= paddleY && ballY <= paddleY + 32)


I think the correct sequence is to write first the movement of the ball and then the collision. You can not test the collision if there's no object to collide.

#92891 - Ant6n - Sat Jul 15, 2006 7:35 am

rectangle collision should be 4 tests, if i remember right.
i have something like

Code:
if(
          (shipx+SHIPWIDTH >= rockx) && (rockx+rockwidth >= shipx)
    &&  (shipy+SHIPHEIGHT >= rocky) && (rocky+rockheight >= shipy)
)


in some code of mine. pls correct me if i am wrong, i tend to talk bs whenits late

#92904 - AdamZZ - Sat Jul 15, 2006 1:28 pm

thanks :D im gonna delete everything line by line too see whats the problem just like you said :D

and now when i have almost no line i found the problem :D the main fuction didnt have a end curly bracket :S

#92906 - naleksiev - Sat Jul 15, 2006 2:08 pm

Ant6n wrote:
rectangle collision should be 4 tests, if i remember right.
i have something like


You are right! But because in this game the paddle is so close to the very left side of the screen you can ignore the check for the ball exit from the left side of the paddle.

It wasn't bad for first collision detection :)

#92911 - AdamZZ - Sat Jul 15, 2006 2:56 pm

Thanks all! :D and a special thanks to naleksiev for helping me with collision detection :D

http://upload.kinstry.co.uk/users/adamzz/PongDS.rar

the game is in version 1.0 planning to make up to version 4.0

right now the game is starting when you start the game on your ds or emulator. it has score and you are competing against yourself.

in version 4.0 (final) these modes will be availible.
VS you
VS AI
VS friend (on same ds)

once again Thanks all!!

#93190 - naleksiev - Mon Jul 17, 2006 3:49 pm

He he :)

I tried the game only on a notebook without mouse and it was hard for me to move but I think the ball is moving only on angles 45 + (90 * x). I mean the speed on X and Y is the same. So this will be good feature to make the ball move more like blockbreaker the angle to depends from the location collision with the paddle.
Excuse me if it's working and I couldn't see it.

Btw. Vs friend will be easer I will recomend you to start from it. I think you are going to make it one of the players to move with the DPAD and the other one with B and X. It's going to be easy addition for your current game so you can maybe release the game with both :).

#93208 - Sausage Boy - Mon Jul 17, 2006 7:28 pm

Reminds me of my Pong clone, Extreme Pong.
_________________
"no offense, but this is the gayest game ever"