Help Beginner

This is a discussion on Help Beginner within the C Programming forums, part of the General Programming Boards category; I already have the collision function working fine...now in the collision function i used target[i] (which at the time of ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Help Beginner

    I already have the collision function working fine...now in the collision function i used target[i] (which at the time of testing i set i=0in which loads the image target[0] for my collision detection( I have 6 images target[0],target[1],target[2] etc). But now i want to use the collision function for all my array of images so i was wondering if i could get ("i" //variable) to equal to 0,1,2,3,4,5 which corresponds with all my images target[0],target[1],target[2] etc.So in the end all i will have in my collision function is target[i] which will cover collision for my 6 images.


    its driving me nuts

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    you want to use a for-loop

    Code:
    for(i = 0; i < 6; i++) {
    // your code here
    }

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    i am not sure...i have been learning on my own so not much experience on how things work..how can i use the loop to get all objects of my target[] array to equal to (i)?

  4. #4
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    the loop i posted will run 6 times - i will be 0 the first time (since it's initialized by i = 0; ) and incremented by 1 every run (i++; )

    So you will be able to use target[i] inside of the loop to access all items of your array.

    There is a nice tutorial on this site: here

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    int x;

    for(x = 0; x < 6; x++) {
    i=x;
    is this rite?

  6. #6
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    it's missing a } at the end
    If "i" is an int too, you would not need to use x - you can use i instead.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    OK thanks I'll give it a try now

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by IceBall View Post
    it's missing a } at the end
    Code:
    If you only have {one command within brackets}, 
    you can optionally leave them out:
    
    for(x = 0; x < 6; x++) i=x;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    yeah MK27 thats correct
    i was reffering to b-k24's code --> an opening-brace requires always a closing-brace.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There was no problem -- I was just mentioning a permissible alternative. Which that maddening avatar should not be. I may have to kill you if you don't come up with something more pleasant
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    still not working

    here is my code
    am coding for the psp.

    Code:
    #include <oslib/oslib.h>
    #include <stdio.h>
    
    
    
    //Necessary to create eboot
    PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
    PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
    
    //declaration of the pointers of our images
    OSL_IMAGE *background,*crosshair,*bg,*target[5],*gun,*hud;
    
    
     //variables
    #define RIGHT  0
    
    
    float j=0,sty,collision=0,collide=0;
    int counter,target_choice=0,stretchx,stretchy,width,height;
    int gun_march=0,gun_position;
    int i;
    int p;
    void Hud();
    void GAME();
    void keys();
    void target_AI();
    void GunAnimate();
    
    bool collisionHS( float targetposX, float targetposY, float crosshairposX, float crosshairposY)
    {
    int collision,collide=0;
    collision = 0;
    int *strhx=&stretchx;
    
    
    target[i]->y=targetposY;
    target[i]->x=targetposX;
    crosshair->x=crosshairposX;
    crosshair->y=crosshairposY;
    
    
    float targetwidth = 35+*strhx;
    float targetheight = sty;
    float crosshairwidth = 10;
    float crosshairheight = 10;
    
     
     if ((targetposX + targetwidth > crosshairposX) &&
    (targetposX < crosshairposX + crosshairwidth) &&
    (targetposY + targetheight+20 > crosshairposY) &&
    (targetposY < crosshairposY + crosshairheight))
    {
    collision = 1; collide=2;
    }
    
    
    return collision;
    }
    
    
    bool collisionBD( float targetposX1, float targetposY1, float crosshairposX1, float crosshairposY1)
    {
    int collision1;
    collision1 = 0;
    int *strhx1=&stretchx;
    
    
    target[i]->y=targetposY1;
    target[i]->x=targetposX1;
    crosshair->x=crosshairposX1;
    crosshair->y=crosshairposY1;
    
    
    float targetwidth1 = 35+*strhx1;
    float targetheight1 = sty;
    float crosshairwidth1 = 10;
    float crosshairheight1 = 10;
    
    
    
    
    
     
    
               if((targetposX1 + targetwidth1 > crosshairposX1) &&
    (targetposX1 < crosshairposX1 + crosshairwidth1) &&
    (targetposY1+25 + targetheight1+50 > crosshairposY1) &&
    (targetposY1+25 < crosshairposY1 + crosshairheight1)){ collision1 = 1;}
           
    
    
    return collision1;
    }
    
    int GetRandomNum(int lo, int hi)
    {
     SceKernelUtilsMt19937Context ctx;
     sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
     u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
     rand_val = lo + rand_val % hi;
     return (int)rand_val;
    }
    
    void keys(){
    
               //crosshair movement
              if(osl_keys->held.right)crosshair->x+=4;
    		  if(osl_keys->held.left)crosshair->x-=4;
    		  if(osl_keys->held.up)crosshair->y-=4;
    		  if(osl_keys->held.down)crosshair->y+=4;
    		  //
    		  if (osl_keys->held.circle){ target[i]->stretchX++; 
    		  target[i]->stretchY++;
    		    stretchx++;
    	    stretchy++;
    		  sty+=.80;} //zoom target
    		  
    		  if (osl_keys->held.cross){ target[i]->stretchX--; 
    		  target[i]->stretchY--;
    		    stretchx--;
    	    stretchy--;
    		  
    		   sty-=.80;} //zoom target
    
    }
    
    void GAME(){
    
    	
    	    ///darw images
    	    oslDrawImage(bg);
    		oslDrawImage(target[0]);
    		oslDrawImage(target[1]);
    		oslDrawImage(crosshair);
    		oslDrawImage(hud);
    		oslDrawImage(gun);
    		
    		
    		
    
    		////keys
    		keys();
    		////
    		GunAnimate();
    		//
    		Hud();
    		//
    		target_AI();
    
    
    }
    
    void target_AI(){
    	//
    	
    	  if (collisionHS( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,60,"headshot");
    	  if (collisionBD( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,70,"bodyshot");
    
       
    
    
    }
    void GunAnimate()
       {	
    		
    
    		//Moves the sprite constantly with no static position
    		oslSetImageTileSize(gun,(gun_march * 36),gun_position,36,60);
    
    		//resets the sprite movement in that row
    		if(crosshair->x>145 && crosshair->x<345 && gun->y>86)gun_march = 0;
    		if(crosshair->y<86 && crosshair->x<145)gun_march = 1;
    		if(crosshair->x>345 && crosshair->y<86)gun_march = 2;
    		if(crosshair->x<145 && crosshair->y>86)gun_march = 3;
    		if(crosshair->x>345 && crosshair->y>86)gun_march = 4;
    
      
     }
    
    void Hud(){
    
    	//
    	if(hud->x<0)hud->x+=8;
    
    
    }
    		
    int main(int argc, char **argv)
    {
    	//Initialization of the Oslib library
    	oslInit(0);
    
    	//Initialization of the graphics mode
    	oslInitGfx(OSL_PF_8888, 1);
    
    	oslInitConsole();				//Text
    
    	oslSetTransparentColor(RGB(255,0,255));
    	
    	//loads our images into memory
    	crosshair = oslLoadImageFile("crosshair.png", OSL_IN_RAM, OSL_PF_5551);
    	bg = oslLoadImageFile("bg.png", OSL_IN_RAM, OSL_PF_5551);
    	 target[0] = oslLoadImageFile("target.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[0]==NULL) oslFatalError("t didn't load in");
         target[1] = oslLoadImageFile("target1.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[1]==NULL) oslFatalError("t1 didn't load in");
         target[2] = oslLoadImageFile("target2.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[2]==NULL) oslFatalError("t2 didn't load in");
         target[3] = oslLoadImageFile("target3.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[3]==NULL) oslFatalError("t3 didn't load in");
    	  target[4] = oslLoadImageFile("target4.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[4]==NULL) oslFatalError("t4 didn't load in");
    	  target[5] = oslLoadImageFile("target5.png", OSL_IN_RAM, OSL_PF_5551);
         if(target[5]==NULL) oslFatalError("t5 didn't load in");
    	 gun = oslLoadImageFile("gun.png", OSL_IN_RAM, OSL_PF_5551);
    	  hud = oslLoadImageFile("hud.png", OSL_IN_RAM, OSL_PF_5551);
    
    
    
    	
       
        
    	//positions
       crosshair->x =100;
       crosshair->y =150;
    
       target[0]->x =200;
       target[0]->y =60;
       target[1]->x =250;
       target[1]->y =120;
    
       gun->x =300;
       gun->y =220;
       hud->x =-200;
       hud->y =237;
    
       //for loop
    for(p=0; p < 6; p++ )i=p;
      
        
       
    	 	//main while loop
    	while (!osl_quit)
    	{
    		//To be able to draw on the screen
    		oslStartDrawing();
    
    		//Clear the screen
    		oslCls();
    
    		//initiate the PSP's buttons
    		oslReadKeys();
    		///
    		GAME();
    
    
    
    
    		
    
    
    
    
    		//Ends drawing mode
    		oslEndDrawing();
    
    		//Synchronizes the screen
    		oslSyncFrame();		
    	}
    
    	//Terminate the program
    	oslEndGfx();
    	oslQuit();
    	return 0;
    }
    I tried this and the collision only worked for one of my targets(target[1])and not target[0]

    //for loop
    for(p=0; p < 2; p++ ){
    if(p==0)i=0;
    if(p==1)i=1;
    }
    Last edited by b-k24; 02-21-2009 at 08:36 AM.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    your problem - using of too many global vars

    get rid of them and pass the data the function should work with as a parameters
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're going to want i and p to be local variables, so you can use them freely inside any function.


    Code:
      //for loop
       for(p=0; p < 2; p++ ) {
          if(p==0)i=0;
          if(p==1)i=1;
       }
    It seems like the above is doing this:

    Code:
       //for loop
       for(p=0; p < 2; p++ ) {
          i = p;
       }
    But since there is no other code in that for loop, it just results in:

    Code:
      i = 1;
    Which probably isn't what you want.

    Working with i and p as local variables may seem more of a PITA right now, but it will definitely improve your programming - and we're right here if you need to ask something about it.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    plz explain more about this local variables of which u speak..and yea the for loop (i)is always equal to 1.when i want (i)value to equal 0then1then0then1 etc.
    Last edited by b-k24; 02-21-2009 at 11:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21