Jump to content



Photo
- - - - -

Box and Collision Testing


  • Please log in to reply
No replies to this topic

#1 GodOfCows

GodOfCows

    Casio Addict

  • Members
  • PipPipPip
  • 79 posts
  • Gender:Male

  • Calculators:
    fx-9860gii, fx-9750gii, TI-92, TI-89, TI-83 Plus

Posted 04 January 2019 - 04:04 AM

I got board so I made a program that can generate boxes and test collision for a ball. I know it's pretty boring but I wanted to know how games could generate objects willy nilly and interact with the enviroment. I am going to try and make a breakout type game with this. Also, I know the code is terrible optimized and poorly written (I'm lazy) 

https://www.youtube....h?v=N_LhlV61BuI

/*****************************************************************/
/*                                                               */
/*   CASIO fx-9860G SDK Library                                  */
/*                                                               */
/*   File name : [ProjectName].c                                 */
/*                                                               */
/*   Copyright (c) 2006 CASIO COMPUTER CO., LTD.                 */
/*                                                               */
/*****************************************************************/
#include "fxlib.h"


//****************************************************************************
//  AddIn_main (Sample program main function)
//
//  param   :   isAppli   : 1 = This application is launched by MAIN MENU.
//                        : 0 = This application is launched by a strip in eACT application.
//
//              OptionNum : Strip number (0~3)
//                         (This parameter is only used when isAppli parameter is 0.)
//
//  retval  :   1 = No error / 0 = Error
//
//****************************************************************************



struct objects {
	unsigned char x, y, xx, yy, active;

} object[100]; //The boxes and their two opposite corners

struct ball { 
	unsigned short x, y, xx, yy, up, left;
} ball; //balls two opposite corners and what directions it is going

unsigned char index; //To know how many boxes have been made


void object_generate(unsigned short, unsigned short, unsigned short, unsigned short);// Sets the boxes cordinates and increases the index
void draw_objects(); //Draws all boxes that have been generated
void initialize_objects(); //Sets all boxes active variable and index to 0;
void move_ball(); //The heart of the program, moves the ball and sees if it has collided with a box and if so, delete and change direction
void draw_ball(); //Draw the ball
void initialize_ball(); //Setup the ball


void object_generate(unsigned short x, unsigned short y, unsigned short xx, unsigned short yy)
{ 
	
	object[index].x = x;
	object[index].y = y;
	object[index].xx = xx;
	object[index].yy = yy;
	object[index].active = 1;
	index++;

}

void draw_objects()
{
	unsigned char i; 
	unsigned short x, y; //cordinates to draw the boxes
	
	for (i = 0; i <= index; i++)
	{
		if (object[i].active)
		{

			for (x = 0; x <= object[i].xx-object[i].x; x++) //draw the top and bottom
			{
				/*
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].y,0);// top
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].yy,0);// bottom
				*/ //If I needed to call draw_box multiple times I would delete this to erease the last iteration
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].y,1);// top
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].yy,1); // bottom
			}

			for (y = 0; y <= object[i].yy-object[i].y; y++)
			{
				/*
					Bdisp_SetPoint_VRAM(object[i].x,object[i].y+y,0); //left
				Bdisp_SetPoint_VRAM(object[i].xx,object[i].y+y,0); //bottom
				*/ // same as above

				Bdisp_SetPoint_VRAM(object[i].x,object[i].y+y,1); //left
				Bdisp_SetPoint_VRAM(object[i].xx,object[i].y+y,1); //right
			}


		}
	}
	Bdisp_PutDisp_DD(); 
}

void initialize_objects()
{
	unsigned char i;
	index = 0;
	for (i = 0; i <= 100; i++)
	{
		object[i].active = 0; //effectively turns of the box collission and wont be draw if draw_box is called again
	}
}

void draw_ball(unsigned short x, unsigned short y, unsigned char put)
{ 

	if (put)
	{
Bdisp_SetPoint_VRAM(x,y,1);Bdisp_SetPoint_VRAM(x+1,y,1);Bdisp_SetPoint_VRAM(x+2,y,1);
Bdisp_SetPoint_VRAM(x,y+1,1);Bdisp_SetPoint_VRAM(x+1,y+1,1);Bdisp_SetPoint_VRAM(x+2,y+1,1);
Bdisp_SetPoint_VRAM(x,y+2,1);Bdisp_SetPoint_VRAM(x+1,y+2,1);Bdisp_SetPoint_VRAM(x+2,y+2,1);
	} else
	{
Bdisp_SetPoint_VRAM(x,y,0);Bdisp_SetPoint_VRAM(x+1,y,0);Bdisp_SetPoint_VRAM(x+2,y,0);
Bdisp_SetPoint_VRAM(x,y+1,0);Bdisp_SetPoint_VRAM(x+1,y+1,0);Bdisp_SetPoint_VRAM(x+2,y+1,0);
Bdisp_SetPoint_VRAM(x,y+2,0);Bdisp_SetPoint_VRAM(x+1,y+2,0);Bdisp_SetPoint_VRAM(x+2,y+2,0);
	}
} //Ya ya, I know casio default library is terrible for drawing, but hey, im lazy!

void initialize_ball()
{
ball.x = 1;
ball.y = 1;
ball.xx = 3;
ball.yy = 3;
ball.up = 0;
ball.left = 0;
} //setup the ball

void move_ball()
{
	unsigned char i, x, y;

if (ball.x <= 2)
	ball.left = 0;
if (ball.xx >= 125)
	ball.left = 1;
if (ball.y <= 3)
	ball.up = 0;
if (ball.yy >= 60)
	ball.up = 1;
//if bal hits the wall or sides change direction

for (i = 0; i <= index; i++) //collision detection
{
	if (object[i].active) //if box isn't active, no need to see if its colliding
	{

		
		if (!(ball.xx < object[i].x) && !(ball.x > object[i].xx) && !(ball.yy < object[i].y) && !(ball.y > object[i].yy)) // colliding
		{

			object[i].active = 0;

		if (ball.yy <= object[i].y || ball.y >= object[i].yy) 
			if (ball.x >= object[i].x || ball.xx <= object[i].xx) //top or bottom
			{
				if (ball.up)
					ball.up = 0;
				else ball.up = 1;
			}

		if (ball.xx <= object[i].x || ball.x >= object[i].xx) 
			if (ball.y <= object[i].yy || ball.yy >= object[i].y)// sides
			{
				if (ball.left)
					ball.left = 0;
				else ball.left = 1;
			}

for (x = 0; x <= object[i].xx-object[i].x; x++)//erease the box we just hit
			{
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].y,0); //top/bottom
				Bdisp_SetPoint_VRAM(object[i].x+x,object[i].yy,0);
			}

			for (y = 0; y <= object[i].yy-object[i].y; y++)
			{
					Bdisp_SetPoint_VRAM(object[i].x,object[i].y+y,0); //sides
				Bdisp_SetPoint_VRAM(object[i].xx,object[i].y+y,0);

			}




		}



}
}

draw_ball(ball.x,ball.y,0); //erease the ball 

if (ball.up)
{
	ball.y--;
	ball.yy--;
} else {
	ball.y++;
	ball.yy++;
}

if (ball.left)
{
	ball.x--;
	ball.xx--;
} else {
	ball.x++;
	ball.xx++;
} //change the cordinates based on direction
draw_ball(ball.x,ball.y,1); //draw ball at new cordinates
Bdisp_PutDisp_DD();
} //end of move_ball


void ticker()
{
	
	move_ball();

}//this function serves no purpuse in this iteration of the program

void control()
{
	 unsigned short key;
	while (1)
	{
		draw_ball(ball.x,ball.y,0);

	GetKey(&key);

initialize_objects();


//This is the main feature of the program, this can easily be made to be mapped to a array to make a breakout game of sorts, which I plan to do
//it generates a box which the ball can interact with
object_generate(20,20,30,30);
object_generate(100,20,110,30);
object_generate(10,50,20,55);
object_generate(30,5,35,10);
object_generate(50,30,55,35);

draw_objects(); //draw the boxes that we just made


}//end of while
}

int AddIn_main(int isAppli, unsigned short OptionNum)
{
	
   
Bdisp_AllClr_DDVRAM();

initialize_objects();
initialize_ball();


//This is the main feature of the program, this can easily be made to be mapped to a array to make a breakout game of sorts, which I plan to do
//it generates a box which the ball can interact with
object_generate(20,20,30,30);
object_generate(100,20,110,30);
object_generate(10,50,20,55);
object_generate(30,5,35,10);
object_generate(50,30,55,35);

draw_objects(); //draw the boxes that we just made


    while(1){
        
        SetTimer(1,25,ticker);
        control();
     
       
        
    }

    return 1;
}




//****************************************************************************
//**************                                              ****************
//**************                 Notice!                      ****************
//**************                                              ****************
//**************  Please do not change the following source.  ****************
//**************                                              ****************
//****************************************************************************


#pragma section _BR_Size
unsigned long BR_Size;
#pragma section


#pragma section _TOP

//****************************************************************************
//  InitializeSystem
//
//  param   :   isAppli   : 1 = Application / 0 = eActivity
//              OptionNum : Option Number (only eActivity)
//
//  retval  :   1 = No error / 0 = Error
//
//****************************************************************************
int InitializeSystem(int isAppli, unsigned short OptionNum)
{
    return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
}

#pragma section







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users