 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
cruisx
Joined: 25 Mar 2008 Posts: 8
|
Posted: Wed Apr 02, 2008 8:08 am Post subject: need some help please *beginer* |
|
|
ok i need some help guys. I am trying to lean how to do collision detection with 2 objects on the screen but i am kinda stuck, im sure the problem is an easy one, but for a beginner i am having some trouble.
| Code: | //Oslib header file
#include <oslib/oslib.h>
//This is needed for eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
//this creates pointers for our images.
OSL_IMAGE *sprite, *sprite2, *sprite3;
//definations
#define DOWN 0
#define UP 35
#define RIGHT 70
#define LEFT 105
//variables
int sprite_position;
int sprite_march;
//declare the function like buttons and spriteAnimation
void Buttons();
void SpriteAnimate();
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY,OSL_IMAGE *img3, float img3posX, float img3posY );
int main()
{
//start the Oslib library
oslInit(0);
//start the graphixs mode
oslInitGfx(OSL_PF_8888, 1);
//sets the transparency color
oslSetTransparentColor(RGB(255,0,255));
//loads the images into memory
sprite = oslLoadImageFile("sprite.png", OSL_IN_RAM, OSL_PF_5551);
sprite2 = oslLoadImageFile("sprite2.png", OSL_IN_RAM, OSL_PF_5551);
sprite3 = oslLoadImageFile("sprite3.png", OSL_IN_RAM, OSL_PF_5551);
//Disables the transparent color so it cannot be used again
oslDisableTransparentColor();
//sees if all files are avaiable
if (!sprite || !sprite2||!sprite3)
oslDebug("! one or more of the files were missing, the program cannot run");
//Sets the sprites original position, lets see if i can get 2 sprites on da bloody screen
sprite->x = 155;
sprite->y = 95;
sprite_position = DOWN;
//sets second sprite position
sprite2->x = 230;
sprite2->y = 130;
//sets thrid sprite pos
sprite3->x = 270;
sprite3->y = 130;
//main while loop
while (!osl_quit)
{
//to be able ot draw on the screen
oslStartDrawing();
//Buttons
Buttons();
//draws a green background
oslDrawGradientRect(0,0,480,272,RGB(0,128,0),RGB(0,128,0), RGB(128,255,128), RGB(128,255,128));
//this draws the images to the screen
oslDrawImage(sprite);
oslDrawImage(sprite2);
oslDrawImage(sprite3);
//stops the drawing mode
oslEndDrawing();
//sync the screen
oslSyncFrame();
}
//terminate the program
oslEndGfx();
oslQuit();
return 0;
}
void Buttons()
{
//starts up the psp buttons
oslReadKeys();
if (osl_keys->held.down)
{
if(!collision(sprite, sprite->x, sprite->y + 2, sprite2, sprite2->x, sprite2->y, sprite3, sprite3->x, sprite3->y ))
{
//sprite movement
sprite->y +=2;
//sets sprite position on sprite sheet
sprite_position = DOWN;
//calls sprie animate
SpriteAnimate();
}
}
if (osl_keys->held.up)
{
if(!collision(sprite, sprite->x, sprite->y - 2, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y ))
{
sprite->y-= 2;
sprite_position = UP;
SpriteAnimate();
}
}
if (osl_keys->held.right)
{
if(!collision(sprite, sprite->x + 2, sprite->y, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y ))
{
sprite->x += 2;
sprite_position = RIGHT;
SpriteAnimate();
}
}
if (osl_keys->held.left)
{
if(!collision(sprite, sprite->x - 2, sprite->y, sprite2, sprite2->x, sprite2->y,sprite3, sprite3->x, sprite3->y ))
{
sprite->x -= 2;
sprite_position = LEFT;
SpriteAnimate();
}
}
//If a button is not pressed
if (!osl_keys->held.value)
{
//Start the variable over for when a button is pressed again
sprite_march = 0;
//Sets the sprite's direction
oslSetImageTileSize(sprite,0,sprite_position,22,35);
}
}
void SpriteAnimate()
{
//Moves the sprite in the row that it is in
sprite_march++;
//Moves the sprite constantly
oslSetImageTileSize(sprite,(sprite_march * 22),sprite_position,22,35);
//resets the sprite movement in that row
if (sprite_march == 6) sprite_march = 0;
}
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY )
{
int collision;
collision = 0;
float img1width = img1->stretchX;
float img1height = img1->stretchY;
float img2width = img2->stretchX;
float img2height = img2->stretchY;
float img3width = img3->stretchX;
float img3height = img3->stretchY;
if ((img1posX + img1width > img2posX) &&
(img1posX < img2posX + img2width) &&
(img1posX + img1width > img3posX) &&
(img1posX < img2posX + img3width) &&
(img1posY + img1height > img2posY) &&
(img1posY < img2posY + img2height) )
{
collision = 1;
}
return collision;
}
|
I know where the problem is but i dont know what im doing wrong
| Code: | if ((img1posX + img1width > img2posX) &&
(img1posX < img2posX + img2width) &&
[B] (img1posX + img1width > img3posX) &&
(img1posX < img2posX + img3width) &&[/B]
(img1posY + img1height > img2posY) &&
(img1posY < img2posY + img2height) )
{ |
if i put that in, none of the sprites have collision but if i take out the Bolded parts the first sprite has collision but the second one dose not.(second sprite as in the second object, not the character that is moving). How do i modify this correctly for every new object on the screen? help will be greatly appreciated. |
|
| Back to top |
|
 |
jean

Joined: 05 Jan 2008 Posts: 489
|
Posted: Wed Apr 02, 2008 7:03 pm Post subject: |
|
|
Just a few hints:
1) Collision is to be performed between two entities at a time.
2) Collision is generally not a commutative task because speed and acceleration are taken into account *BUT* In your case it's simple and commutative, so be sure not to check the same two times.
3) Write a single function that checks if a point is inside a shape and reuse it everytime you need to.
i.e.
| Code: |
for each shape
for each vertex of current shape
check if the vertex is inside any other shape
if so, append a "collision detected between a and b" flag into a list
parse the list and for each collision detected, act accordingly
|
|
|
| Back to top |
|
 |
PosX100
Joined: 15 Aug 2007 Posts: 98
|
Posted: Wed Apr 02, 2008 9:33 pm Post subject: |
|
|
So , you're not making a tile-based game?
Well , then , search on google how to perform bounding box collision.
Its really easy.
The calculation is simple , you just need to check if x1,x1+w1,y1,y1+h1 ,
x2,x2+w2,y2,y2+h2 collide....
w=width
h=height
x=x position
y=y position
In games , bounding box collision usually used before performing any other type collision(eg :pixel/and any other complex collision formula) to save up cpu , but for a very simple game(like a shooter for example) it should be enough..
[EDIT]
Hey, wait..you already have a collision detection function in your code
| Code: |
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY )
|
But you're doing a big mistake.
Why you're checking 3 objects?
You should check 2 objects at a time.
So , this should be enough:
| Code: |
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY)
{
//Its better to use a pointer to the actual struct/class member variable..
//you can also use pointer reference for the images...
float * img1width = &img1->stretchX,img1height = &img1->stretchY,
img2width = &img2->stretchX,img2height = &img2->stretchY;
return ((img1posX + img1width > img2posX) &&
(img1posX < img2posX + img2width) &&
(img1posY + img1height > img2posY) &&
(img1posY < img2posY + img2height) ));
} |
|
|
| Back to top |
|
 |
cruisx
Joined: 25 Mar 2008 Posts: 8
|
Posted: Thu Apr 03, 2008 7:32 am Post subject: |
|
|
| PosX100 wrote: | So , you're not making a tile-based game?
Well , then , search on google how to perform bounding box collision.
Its really easy.
The calculation is simple , you just need to check if x1,x1+w1,y1,y1+h1 ,
x2,x2+w2,y2,y2+h2 collide....
w=width
h=height
x=x position
y=y position
In games , bounding box collision usually used before performing any other type collision(eg :pixel/and any other complex collision formula) to save up cpu , but for a very simple game(like a shooter for example) it should be enough..
[EDIT]
Hey, wait..you already have a collision detection function in your code
| Code: |
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY, OSL_IMAGE *img3, float img3posX, float img3posY )
|
But you're doing a big mistake.
Why you're checking 3 objects?
You should check 2 objects at a time.
So , this should be enough:
| Code: |
int collision(OSL_IMAGE *img1,float img1posX, float img1posY, OSL_IMAGE *img2, float img2posX, float img2posY)
{
//Its better to use a pointer to the actual struct/class member variable..
//you can also use pointer reference for the images...
float * img1width = &img1->stretchX,img1height = &img1->stretchY,
img2width = &img2->stretchX,img2height = &img2->stretchY;
return ((img1posX + img1width > img2posX) &&
(img1posX < img2posX + img2width) &&
(img1posY + img1height > img2posY) &&
(img1posY < img2posY + img2height) ));
} |
|
oh i did that for the second image becasue the first image is the moving sprite is it not? and also what part of my code should i replace with yours? Thanks once again though =) ill llook into the box collision |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|