Collisions (i think)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
flyboy2012
Posts: 11
Joined: Tue Oct 07, 2008 8:50 am

Collisions (i think)

Post by flyboy2012 »

I am trying to launch a .PBP when i have my image (sprite) over my other image (item1). The program should start when i press cross as long as my sprite is the the boundaries (x50 y50) also the same size of my image (item1)but when i press cross nothing happens so...
How have i set my main.c file up wrong or what do i need to add?

Code: Select all

#include <oslib/oslib.h>
#include <psploadexec_kernel.h>
#include <psploadexec_kernel.h>
#include <systemctrl.h>
#include <stdio.h>
#include <stdlib.h>
PSP_MODULE_INFO&#40;"PSPWindows", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

void RunPBP&#40;char* file&#41;&#123;
struct SceKernelLoadExecVSHParam param;
char argp&#91;256&#93;;
int  args;

strcpy&#40;argp, file&#41;;
args = strlen&#40;file&#41;+1;

memset&#40;&param, 0, sizeof&#40;param&#41;&#41;;
param.size = sizeof&#40;param&#41;;
param.args = args;
param.argp = argp;
param.key = NULL;
param.vshmain_args_size = 0;
param.vshmain_args = NULL;
sctrlKernelLoadExecVSHMs2&#40;file,&param&#41;;
&#125;

//declaration of the pointers of our images
OSL_IMAGE *background, *sprite, *taskbar, *startmenu, *item1;

int collision&#40;int x0, int y0, int x1, int y1, int w0, int h0, int w1, int h1&#41;
&#123;
	int hit = 0;
	
	if&#40;x0+w0 > x1 && x0 < x1+w1 && y0+h0 > y1 && y0 < y1+h1&#41;hit = 1;
	
	return hit;
&#125;

//Main
int main&#40;&#41;&#123;
//Set item starting positions
	int item1X = 0;
	int item1Y = 0;
	int item2X = 50;
	int item2Y = 70;
	int item3X = 50;
	int item3Y = 90;
	//Set sprites starting position
		int spriteX;
		int spriteY;
	
	//Initialization of the Oslib library
	oslInit&#40;0&#41;;

	//Initialization of the graphics mode
	oslInitGfx&#40;OSL_PF_8888, 1&#41;;

	//loads our images into memory
	background = oslLoadImageFile&#40;"background.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	taskbar = oslLoadImageFile&#40;"taskbar.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	startmenu = oslLoadImageFile&#40;"startmenu.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	sprite = oslLoadImageFile&#40;"sprite.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	item1 = oslLoadImageFile&#40;"internet.png", OSL_IN_RAM, OSL_PF_5551&#41;;

	//Verification that all files are present
	if &#40;!background || !sprite&#41;
		oslFatalError&#40;"It is impossible to load one or more of the images that are required in this program. Please make sure you have all the files required in the eboot folder"&#41;;

	
	//Main while loop
	while &#40;!osl_quit&#41;
	&#123;
		//To be able to draw on the screen
		oslStartDrawing&#40;&#41;;
		
		//initiate the PSP's buttons
		oslReadKeys&#40;&#41;;
		if &#40;osl_keys->pressed.start&#41;
			oslDrawImage&#40;startmenu&#41;;

		//Sprite movement
		if &#40;osl_keys->analogX > 50&#41; sprite->x += 2;
		if &#40;osl_keys->analogX < -50&#41; sprite->x -= 2;
		if &#40;osl_keys->analogY > 50&#41; sprite->y += 2;
		if &#40;osl_keys->analogY < -50&#41; sprite->y -= 2;
		
		//Draw the images to the screen
		oslDrawImage&#40;background&#41;;
		oslDrawImage&#40;taskbar&#41;;
		oslReadKeys&#40;&#41;;
		if &#40;osl_keys->held.start&#41;
			oslDrawImage&#40;startmenu&#41;;
		oslDrawImage&#40;item1&#41;;

		oslReadKeys&#40;&#41;;
		if&#40;osl_keys->held.cross&#41;//If cross is pressed
		&#123;
			if&#40;collision&#40;spriteX, spriteY, item1X, item1Y, 50, 50, 50, 50&#41; && !&#40;collision&#40;spriteX, spriteY, item2X, item2Y, 50, 50, 50, 50&#41;&#41; && !&#40;collision&#40;spriteX, spriteY, item3X, item3Y, 50, 50, 50, 50&#41;&#41;&#41;//If the sprite is on the first item and not on the other two
			&#123;
				RunPBP&#40;"ms0&#58;/PSP/GAME/GTH/EBOOT.PBP"&#41;;
			&#125;
			if&#40;collision&#40;spriteX, spriteY, item2X, item2Y, 50, 50, 50, 50&#41; && !&#40;collision&#40;spriteX, spriteY, item1X, item1Y, 50, 50, 50, 50&#41;&#41; && !&#40;collision&#40;spriteX, spriteY, item3X, item3Y, 50, 50, 50, 50&#41;&#41;&#41;//If the sprite is on the second item and not on the other two
			&#123;
				//Set the items X & Y to the sprites
				item2X = spriteX;
				item2Y = spriteY;
			&#125;
			if&#40;collision&#40;spriteX, spriteY, item3X, item3Y, 50, 50, 50, 50&#41; && !&#40;collision&#40;spriteX, spriteY, item1X, item1Y, 50, 50, 50, 50&#41;&#41; && !&#40;collision&#40;spriteX, spriteY, item2X, item2Y, 50, 50, 50, 50&#41;&#41;&#41;//If the sprite is on the third item and not on the other two
			&#123;
				//Set the items X & Y to the sprite's
				item3X = spriteX;
				item3Y = spriteY;
			&#125;
		&#125;
		
	    	oslDrawImage&#40;sprite&#41;;
		//Ends drawing mode
		oslEndDrawing&#40;&#41;;

		//Synchronizes the screen 
		oslSyncFrame&#40;&#41;;	

	&#125;
	
	//Terminate the program
	oslEndGfx&#40;&#41;;
	oslQuit&#40;&#41;;
	return 0;

&#125;
Onii
Posts: 40
Joined: Sun Oct 05, 2008 1:07 pm

Post by Onii »

I mean no offense, but after reviewing this code, I think programming the PSP my be a bit out of your league.

That aside:

If you want to find out what your program is doing, you're going to either have to load it in a debugger (read: PSPLink + GDB) or throw some printf()s in there. I don't know the first thing about OSL but I'm pretty sure I saw a post on these forums showing its printf (or the equivalent) call.

If you can't figure out either of those methods I'd suggest starting out on an easier project, or come back to the PSP after you have a game or two on another platform.
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

This is an apple .
This is an orange .
This is Sparta(aaaaaaaaaa).

no , No , NO^2 , ........... this is a (yet another) (poorly written) f. shell.


PS:I'm the only shell-hater ?
hibbyware
Posts: 78
Joined: Wed Mar 28, 2007 10:29 am

Post by hibbyware »

PosX100 wrote: PS:I'm the only shell-hater ?
I have a love hate relationship with shells,

I hate how all these incomplete shells keep getting released when they should wait for it to mature a bit first,

I love to use my own shell as I don't like the XMB much but it will still stay as a private shell as there are already loads of other shells available,

So what I'm saying is that it seems ok to make your own shell for learning and enjoyment but releasing that shell is another matter,
flyboy2012
Posts: 11
Joined: Tue Oct 07, 2008 8:50 am

Post by flyboy2012 »

i did not plan to release this shell. it is intended for personal use. i am learning to code and this is my first project so it shouldn't surprise you that it is poorly written but i still need help
Onii
Posts: 40
Joined: Sun Oct 05, 2008 1:07 pm

Post by Onii »

Then throw in printf()s everywhere you think something interesting may happen. For example:

put a printf("1"); inside the read keys check ,
then printf("2"); inside of the first collision check,
then printf("3"); inside your RunPBP

Then run your program and see what numbers show up. Then at least you'll know where your problem is, and therefore what to read up on, or ask specific questions about.
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

I don't think debugging each function will really help in this case.
Its just insanely bad code design combined with extremely bad code.

First of all , you're calling "readKeys" function 3 times each frame...wtf.
Second, "*->held" method means that "$" button should be...held down,which isn't what you really want.
Third,why on earth you're combining collision detection for each object that you don't actually have
to check?.
Fourth,better re-write collision detection function(see my previous post...in your first(?) thread) , it just looks so bad with all these parameters.
Fifth,

Code: Select all

 if &#40;osl_keys->held.start&#41;
         oslDrawImage&#40;startmenu&#41;; 
LOL.
Sixth,

Code: Select all

          //Set the items X & Y to the sprite's
            item3X = spriteX;
            item3Y = spriteY; 
LOL^2.
Seventh,
Listen , ..."flyboy2012"...
Download an IDE , and start making a few console (text) games on windows/linux/whatever and
small utilities ,and ......you should be able to "fly" in the year 2012....JUST forget psp for now.
Post Reply