0 Members and 1 Guest are viewing this topic.
//Made by NXT with major physics help from BuilderBoy#define LEFT_OF_SCREEN_X 0#define RIGHT_OF_SCREEN_X 99#define TOP_OF_SCREEN_Y 63#define BOTTOM_OF_SCREEN_Y 0#define SCREEN_WIDTH 100#define SCREEN_HEIGHT 64#define TOTAL_OBJECTS 5#define BOUNCE (0.5)#define GRAVITY -0.2#define FRICTION 0.3//define the objectstruct object{ float X; float Y; int width; int height; float mass; float velX; float velY; //Displaying stuff byte DRAW_OPT;};object box[TOTAL_OBJECTS];bool Collide(object ob1, object ob2){ if(((trunc(ob1.X) + ob1.width + 1) >= ob2.X) && (trunc(ob1.X) <= (trunc(ob2.X) + ob2.width + 1)) && ((trunc(ob1.Y) + ob1.height + 1) >= trunc(ob2.Y)) && (trunc(ob1.Y) <= (trunc(ob2.Y) + ob2.height + 1))) { return(true); } return(false);}float muldiv(float _input, float _interval){ _input = floor(_input * _interval); return(_input / _interval);}task main(){ bool collided; //Setting variables for boxes for(int c=0; c < TOTAL_OBJECTS; c++) { box[c].X = (40) + Random(5); box[c].Y = Random(40); box[c].width = Random(6) + 2; box[c].height = Random(5) + 2; box[c].velX = Random(3) - 1; box[c].velY = Random(2) - 1; box[c].mass = Random(4) + 1; } while(1) { collided = 0; for(int c=0; c < TOTAL_OBJECTS; c++) { for(int d=0; d < TOTAL_OBJECTS; d++) { if(d != c) { if(Collide(box[c],box[d])) { box[c].X = (40) + Random(5); box[c].Y = Random(40); collided = 1; } } } } if(collided == 0) break; } float backupBoxX; float backupBoxY; float backupBoxX2; float backupBoxY2; float backupVelX; float backupVelY; float backupVelX2; float backupVelY2; box[0].DRAW_OPT = DRAW_OPT_FILL_SHAPE | DRAW_OPT_LOGICAL_XOR; box[0].mass = 3; while(true) { SetDisplayFlags(DISPLAY_REFRESH_DISABLED); ClearScreen(); if(ButtonPressed(BTNCENTER,0)) { box[0].velY += 0.6; } else if(ButtonPressed(BTNEXIT,0)) { box[0].velY -= 0.6; } if(ButtonPressed(BTNRIGHT,0)) { box[0].velX += 0.6; } if(ButtonPressed(BTNLEFT,0)) { box[0].velX -= 0.6; } //Processing for each box and displaying for(int c=0; c < TOTAL_OBJECTS; c++) { //Backs up X and Y Coordinates for collision backupBoxX = box[c].X; backupBoxY = box[c].Y; //Calculating X and Y Coordinates box[c].velY += GRAVITY; backupVelY2 = box[c].velY; backupVelX2 = box[c].velX; box[c].X += box[c].velX; box[c].Y += box[c].velY; //Collisions with walls of screen if(box[c].Y <= BOTTOM_OF_SCREEN_Y) //Collide with bottom of screen { box[c].Y = backupBoxY; if(abs(box[c].velX) < FRICTION) { box[c].velX = 0; } else if(box[c].velX < 0) { box[c].velX += FRICTION; } else { box[c].velX -= FRICTION; } box[c].velY = -box[c].velY*BOUNCE; } if(box[c].Y + box[c].height >= TOP_OF_SCREEN_Y) //Collide with bottom of screen { box[c].Y = backupBoxY; box[c].velY = -box[c].velY*BOUNCE; } if(box[c].X <= LEFT_OF_SCREEN_X) //Collide with left of screen { box[c].X = backupBoxX; box[c].velX = -box[c].velX*BOUNCE; } if(box[c].X + box[c].width >= RIGHT_OF_SCREEN_X) //Collide with right of screen { box[c].X = backupBoxX; box[c].velX = -box[c].velX*BOUNCE; } //Collisions with other objects for(int d=0; d < TOTAL_OBJECTS; d++) { if(d != c) { if(Collide(box[c],box[d])) { box[c].X = backupBoxX; backupVelY = box[c].velY; backupVelX = box[c].velX; if(Collide(box[c],box[d])) { //Collision on Y axis box[c].Y = backupBoxY; box[c].velX = backupVelX2; box[c].velY = backupVelY2; //V1 = (C*M2*(V2-V1)+M1*V1+M2*V2)/(M1+M2) box[c].velY = (BOUNCE*box[d].mass*(box[d].velY - backupVelY) + box[c].mass*backupVelY + box[d].mass*box[d].velY)/(box[c].mass+box[d].mass); box[d].velY = (BOUNCE*box[c].mass*(backupVelY - box[d].velY) + box[d].mass*box[d].velY + box[c].mass*backupVelY)/(box[d].mass+box[c].mass); } else { //Collision on X axis box[c].velX = backupVelX2; box[c].velY = backupVelY2; box[c].Y = backupBoxY; box[c].velX = (BOUNCE*box[d].mass*(box[d].velX - backupVelX) + box[c].mass*backupVelX + box[d].mass*box[d].velX)/(box[c].mass+box[d].mass); box[d].velX = (BOUNCE*box[c].mass*(backupVelX - box[d].velX) + box[d].mass*box[d].velX + box[c].mass*backupVelX)/(box[d].mass+box[c].mass); } } } } RectOut(trunc(box[c].X),trunc(box[c].Y),box[c].width,box[c].height,box[c].DRAW_OPT); } SetDisplayFlags(DISPLAY_REFRESH | DISPLAY_ON); Wait(17); //Waits 17ms to refresh the screen }}
if(Collide(box[c],box[d])) { //Collision on Y axis box[c].Y = backupBoxY; box[c].velX = backupVelX2; box[c].velY = backupVelY2; //V1 = (C*M2*(V2-V1)+M1*V1+M2*V2)/(M1+M2) box[c].velY = (BOUNCE*box[d].mass*(box[d].velY - backupVelY) + box[c].mass*backupVelY + box[d].mass*box[d].velY)/(box[c].mass+box[d].mass); box[d].velY = (BOUNCE*box[c].mass*(backupVelY - box[d].velY) + box[d].mass*box[d].velY + box[c].mass*backupVelY)/(box[d].mass+box[c].mass); }