Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to remove magnetic effect in collision!

This thread is locked; no one can reply to it. rss feed Print
How to remove magnetic effect in collision!
varynoob
Member #14,058
February 2012

Hello everyone,

Looking for some realism in collision and updation, i mean i have a ball in centre, squares coming from right to left,
1. If ball touches the left of square, everything stops.
2. Now the ball should be made to jump over the square, roll on and go ahead. But when i jump, the ball magnetically sticks to the box!! No realism , so what should i do to make it look smooth and kind of real!!

#SelectExpand
1for(int i=0; i < ssize ; i++) 2 { 3 if(shapeS[i].live) // checks whether the square is on the screen 4 { 5 if(((shapeS[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx +7) 6 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx)) 7 &&!((shapeS[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 8 &&(shapeS[i].pos_rect_y1+40 > shapeB.pos_Y - shapeB.boundy)))// basic collision detection for top 9 { 10 shapeB.LeftCollision = false; 11 shapeB.pos_Y = shapeS[i].pos_rect_y1 - shapeB.boundy - 7 ; // this updates ball position and causes magnetic effect 12 shapeB.isOnSquare = true; 13 } 14 15 else if((shapeS[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx+ 7) 16 &&(shapeS[i].pos_rect_x1 > shapeB.pos_X - shapeB.boundx)) // checks for left collision 17 { 18 shapeB.LeftCollision = true; 19 } 20 21 else 22 { 23 shapeB.LeftCollision = false; 24 25 } 26 } 27 }

Steve Terry
Member #1,989
March 2002
avatar

You are only snapping y to the top of the box, if you want to make it roll off you need to apply gravity and forward momentum.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

varynoob
Member #14,058
February 2012

Is it like, adding and && comdition in the JUMP mechanism ? (!isOnGround && LeftCollision) ? ?

How do i apply forward momentum ? ?

Steve Terry
Member #1,989
March 2002
avatar

changing anything in your if statement won't get the ball rolling forward. You will need to post more code if we are to figure out how to apply forward momentum. Usually it just means incrementing x.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

varynoob
Member #14,058
February 2012

The code goes as this(excluding code fr BG, Explosions, Globals, main() part b4 game loop), so where do i apply momentum ?
also, if i want triangles and square in a particular pattern, but random at the same time, ie that pattern should be random.

How do i go for it, i understand it is just some logic, but what i want to ask is, should i take triangle and square in the same struct and same loop in else if conditions or do as i have done here with different structs.

I can attach the setup as well, if needed, the game is running, but with triangle-triangle overlap and square-triangle overlap and that magnetic effect!!

#SelectExpand
1 2/*********************** 3 GAME LOOP 4************************/ 5 while(!done) 6 { 7 ALLEGRO_EVENT ev; 8 al_wait_for_event(event_queue, &ev); 9 10 11 time_remaining --; 12 if((time_remaining/60) % 3 == 0) 13 inc_speed +=0.001; 14 15 16 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 17 { 18 switch(ev.keyboard.keycode) 19 { 20 case ALLEGRO_KEY_LEFT: 21 keys[LEFT] = true; 22 break; 23 case ALLEGRO_KEY_RIGHT: 24 keys[RIGHT] = true; 25 break; 26 case ALLEGRO_KEY_SPACE: 27 keys[SPACE] = true; 28 if(state == TITLE) 29 ChangeState(state, PLAYING); 30 else if(state == LOST) 31 ChangeState(state, PLAYING); 32 33 break; 34 35 } 36 37 } 38 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 39 { 40 switch(ev.keyboard.keycode) 41 { 42 case ALLEGRO_KEY_LEFT: 43 keys[LEFT] = false; 44 break; 45 case ALLEGRO_KEY_RIGHT: 46 keys[RIGHT] = false; 47 break; 48 case ALLEGRO_KEY_ESCAPE: 49 done=true; 50 break; 51 case ALLEGRO_KEY_SPACE: 52 keys[SPACE] = false; 53 break; 54 } 55 } 56 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 57 { 58 done = true; 59 } 60 61 /*********************** 62 UPDATE 63 ************************/ 64 65 66 else if(ev.type == ALLEGRO_EVENT_TIMER) 67 { 68 redraw = true; 69 /*if(keys[LEFT]) 70 MoveLeftBall(shapeB); 71 if(keys[RIGHT]) 72 MoveRightBall(shapeB);*/ 73 74 75 76 if (shapeB.isOnGround || shapeB.isOnSquare ) 77 { // means the ball is on the ground 78 if(keys[SPACE]) 79 { 80 if (shapeB.canJump) //CAN JUMP 81 { 82 shapeB.dy= -10; //VELOCITY UPWARDS 83 shapeB.canJump = false; 84 } 85 } 86 else 87 shapeB.canJump = true; 88 } 89 90 91 92 if(!shapeB.isOnGround || !shapeB.isOnSquare) 93 { 94 shapeB.dy += 0.5; 95 speed = 5+inc_speed ; 96 } 97 else 98 speed = 3+inc_speed; 99 100 if (keys[SPACE] && shapeB.isOnGround && shapeB.isOnSquare && shapeB.dy > 0) 101 shapeB.dy -= 0.1; // got to be LESS than 0.5 here. 102 103 if(shapeB.dy>5) 104 shapeB.dy = 5; 105 106 shapeB.pos_Y += shapeB.dy; 107 108 if(shapeB.pos_Y > HEIGHT-20) 109 { 110 shapeB.pos_Y = HEIGHT-20; 111 shapeB.dy = 0; 112 } 113 114 115 if(state == TITLE) 116 {} 117 else if(state == PLAYING) 118 { 119 if(shapeB.LeftCollision == false) 120 { 121 122 UpdateBackground(BG); 123 124 UpdateTriangle(shapeT,NUM_TRIANGLES, shapeB); 125 126 UpdateSquare(shapeS,NUM_TRIANGLES, shapeB); 127 } 128 UpdateExplosions(explosions); 129 UpdateBall(shapeB); 130 StartTriangle(shapeT, NUM_TRIANGLES); 131 StartSquare(shapeS, NUM_TRIANGLES); 132 CollideTriangle(shapeT,shapeS, NUM_TRIANGLES, shapeB, explosions); 133 CollideSquare(shapeS, NUM_TRIANGLES, shapeB, explosions); 134 if(shapeB.lives <=0 || time_remaining < 1) 135 ChangeState(state, LOST); 136 } 137 else if(state == LOST) 138 {} 139 140 141 /*********************** 142 RENDERING 143 ************************/ 144 145 146 if(redraw && al_is_event_queue_empty(event_queue)) 147 { 148 redraw= false; 149 150 if(state == TITLE) 151 { 152 al_draw_bitmap(title, 0 ,0 ,0); 153 } 154 else if(state == PLAYING) 155 { 156 DrawBackground(BG); 157 DrawBall(shapeB); 158 DrawTriangle(shapeT,NUM_TRIANGLES); 159 DrawSquare(shapeS,NUM_TRIANGLES); 160 DrawExplosions(explosions); 161 162 al_draw_textf(font18, al_map_rgb(255,255,255),0,0,0, 163 "Player has %i lives. Score : %i",shapeB.lives,shapeB.score); 164 165 al_draw_textf(font18, al_map_rgb(255,255,255),600,0,0, 166 "Time Remaining: %i",time_remaining/60); 167 168 } 169 else if(state == LOST) 170 { 171 al_draw_bitmap(lost, 0, 0, 0); 172 al_draw_textf(font18, al_map_rgb(0,255,255),WIDTH - 10 ,20,ALLEGRO_ALIGN_RIGHT, 173 "Final Score : %i",shapeB.score); 174 } 175 176 177 al_flip_display(); 178 al_clear_to_color(al_map_rgb(0,0,0)); 179 180 } 181 } 182 183 } 184 185 /********************************** 186 DESTROYING GAME OBJECTS 187 **********************************/ 188 189 al_destroy_sample(boom); 190 al_destroy_sample_instance(songInstance); 191 al_destroy_bitmap(bgImage); 192 al_destroy_bitmap(title); 193 al_destroy_bitmap(lost); 194 al_destroy_bitmap(expImage); 195 al_destroy_bitmap(ballImage); 196 al_destroy_event_queue(event_queue); 197 al_destroy_timer(timer); 198 al_destroy_font(font18); 199 al_destroy_display(display); 200 201 return 0; 202} 203 204 205 206void InitBall(Ball &shapeB, ALLEGRO_BITMAP *image = NULL) 207{ 208 shapeB.ID=BALL; 209 shapeB.pos_X= WIDTH/2; 210 shapeB.pos_Y= HEIGHT; 211 shapeB.lives = 3; 212 shapeB.speed = 6; 213 shapeB.score = 0; 214 shapeB.boundx = 23; 215 shapeB.boundy = 16; 216 shapeB.dx = 1; 217 shapeB.dy = 1; 218 shapeB.canJump = false; 219 shapeB.isOnGround = true; 220 shapeB.isOnSquare = false; 221 shapeB.LeftCollision = false; 222 223 shapeB.maxFrame = 8; 224 shapeB.curFrame = 0; 225 shapeB.frameCount = 0; 226 shapeB.frameDelay = 5; 227 shapeB.frameWidth = 64; 228 shapeB.frameHeight = 48; 229 shapeB.animationColumns = 8; 230 shapeB.animationDirection = 1; 231 shapeB.animationRow = 0; 232 233 time_remaining = 10800; //A calculated value to display remainging time 234 speed=0; 235 inc_speed=0; 236 237 if(image != NULL) 238 shapeB.image = image; 239 240} 241void DrawBall(Ball &shapeB) 242{ 243 244 int fx = (shapeB.curFrame % shapeB.animationColumns) * shapeB.frameWidth; 245 int fy = shapeB.animationRow * shapeB.frameHeight; 246 247 al_draw_bitmap_region(shapeB.image, fx, fy, shapeB.frameWidth, 248 shapeB.frameHeight, (shapeB.pos_X - shapeB.frameWidth/2), (shapeB.pos_Y - shapeB.frameHeight/2), 0); 249 250 //al_draw_filled_rectangle(shapeB.pos_X - shapeB.boundx, shapeB.pos_Y - shapeB.boundy, 251 // shapeB.pos_X + shapeB.boundx, shapeB.pos_Y + shapeB.boundy, al_map_rgba(255,0,255,100)); 252 253 254} 255//void MoveLeftBall(Ball &shapeB) 256//{ 257// shapeB.pos_X -=shapeB.speed; 258// if(shapeB.pos_X <(WIDTH/2) - 100) 259// shapeB.pos_X = (WIDTH/2) - 100; 260//} 261//void MoveRightBall(Ball &shapeB) 262//{ 263// shapeB.pos_X += shapeB.speed; 264// if(shapeB.pos_X >(WIDTH/2)+100) 265// shapeB.pos_X = (WIDTH/2)+100 ; 266//} 267 268 269void UpdateBall(Ball &shapeB) 270{ 271 if(shapeB.pos_Y < HEIGHT-20) 272 shapeB.isOnGround = false; 273 else 274 shapeB.isOnGround = true; 275 276 if(!shapeB.LeftCollision) 277 { 278 if(++shapeB.frameCount >= shapeB.frameDelay) 279 { 280 if(++shapeB.curFrame >= shapeB.maxFrame) 281 { 282 shapeB.curFrame = 0; 283 284 } 285 286 shapeB.frameCount = 0; 287 288 } 289 290 } 291} 292 293 294void InitTriangle(Triangle shapeT[], int size) 295{ 296 for(int i=0; i<size; i++) 297 { 298 shapeT[i].ID = TRIANGLE; 299 shapeT[i].live = false; 300 301 302 shapeT[i].bound_triangle_x = 10; 303 shapeT[i].bound_triangle_y = 16; 304 shapeT[i].collidingS = false; 305 shapeT[i].collidingT = false; 306 307 } 308} 309 310void DrawTriangle(Triangle shapeT[], int size) 311{ 312 for(int i=0; i<size; i++) 313 { 314 if(shapeT[i].live && shapeT[i].collidingT == false) 315 { 316 317 318 al_draw_triangle(shapeT[i].pos_x1-8,shapeT[i].pos_y1 + 10,shapeT[i].pos_x1,shapeT[i].pos_y1-10, 319 shapeT[i].pos_x1+8,shapeT[i].pos_y1+10,al_map_rgb(255,0,255),2); 320 321 322 } 323 } 324} 325 326void StartTriangle(Triangle shapeT[], int size) 327{ 328 for (int i = 0 ; i < size; i++) 329 { 330 if(!shapeT[i].live) 331 { 332 if(rand() % 400 == 0) 333 { 334 if(time_remaining % 2 == 0 && time_remaining % 25 != 0) 335 { 336 shapeT[i].live = true; 337 shapeT[i].pos_x1 = WIDTH; 338 shapeT[i].pos_y1 = HEIGHT-10; 339 340 break; 341 } 342 } 343 } 344 } 345} 346 347void UpdateTriangle(Triangle shapeT[], int size, Ball &shapeB) 348{ 349 for(int i=0; i<size; i++) 350 { 351 if((shapeT[i].live) && shapeT[i].collidingT == false) 352 { 353 shapeT[i].pos_x1 -= speed; 354 355 356 357 if(shapeT[i].pos_x1-15 < 0) 358 { 359 shapeT[i].live = false; 360 shapeB.score++; 361 } 362 } 363 } 364} 365 366void CollideTriangle(Triangle shapeT[], Square shapeS[], int csize, Ball &shapeB, Explosion &explosion) 367{ 368 for(int i = 0; i < csize; i++) 369 { 370 if(shapeT[i].live) 371 { 372 373 374 if((shapeT[i].pos_x1 - shapeT[i].bound_triangle_x < shapeB.pos_X + shapeB.boundx) 375 && (shapeT[i].pos_x1 + shapeT[i].bound_triangle_x > shapeB.pos_X - shapeB.boundx) 376 &&(shapeT[i].pos_y1 - shapeT[i].bound_triangle_y < shapeB.pos_Y + shapeB.boundy) 377 &&(shapeT[i].pos_y1 + shapeT[i].bound_triangle_y > shapeB.pos_Y - shapeB.boundy)) 378 { 379 380 shapeT[i].live = false; 381 shapeB.pos_Y = shapeT[i].pos_y1 - shapeT[i].bound_triangle_y - shapeB.boundy - 20; 382 383 StartExplosions(explosion,shapeB.pos_X, shapeB.pos_Y); 384 al_play_sample(boom, 1,0,1,ALLEGRO_PLAYMODE_ONCE,0); 385 shapeB.lives--; 386 387 } 388 /*if(time_remaining % 25 == 0) 389 { 390 if(shapeT[i].pos_x1 - shapeT[i].bound_triangle_x < shapeS[i].pos_rect_x1 + 40 391 && shapeT[i].pos_y1 - shapeT[i].bound_triangle_y < shapeS[i].pos_rect_y1+40 392 && shapeS[i].pos_rect_x1 < shapeT[i].pos_x1+shapeT[i].bound_triangle_x 393 && shapeS[i].pos_rect_y1 < shapeT[i].pos_y1+shapeT[i].bound_triangle_y) 394 { 395 shapeT[i].collidingT = true; 396 397 } 398 399 }*/ 400 401 402 403 404 405 406 407 408 } 409 } 410 411} 412 413 414void InitSquare(Square shapeS[], int size) 415{ 416 for(int i = 0; i < size ; i++) 417 { 418 shapeS[i].ID = SQUARE; 419 shapeS[i].live = false; 420 shapeS[i].collidingS = false; 421 } 422} 423void DrawSquare(Square shapeS[], int size) 424{ 425 for(int i = 0; i<size; i++) 426 { 427 if(shapeS[i].live) 428 { 429 al_draw_rounded_rectangle(shapeS[i].pos_rect_x1,shapeS[i].pos_rect_y1, 430 shapeS[i].pos_rect_x1+40,shapeS[i].pos_rect_y1+40,6,6,al_map_rgb(255,255,255),2); 431 } 432 } 433} 434void StartSquare(Square shapeS[], int size) 435{ 436 for (int i = 0 ; i < size; i++) 437 { 438 if(!shapeS[i].live) 439 { 440 if(rand() % 100 == 0) 441 { 442 if((time_remaining % 25) == 0) 443 shapeS[i].live = true; 444 shapeS[i].pos_rect_x1 = WIDTH+40; 445 shapeS[i].pos_rect_y1 = HEIGHT-40; 446 447 break; 448 449 } 450 } 451 } 452} 453void UpdateSquare(Square shapeS[], int size, Ball &shapeB) 454{ 455 for(int i=0; i<size; i++) 456 { 457 if(shapeS[i].live) 458 { 459 shapeS[i].pos_rect_x1 -= speed; 460 461 if(shapeS[i].pos_rect_x1+40 <= 0) 462 { 463 shapeS[i].live = false; 464 shapeB.score++; 465 } 466 } 467 } 468} 469 470void CollideSquare(Square shapeS[], int ssize, Ball &shapeB, Explosion &explosion) 471{ 472 for(int i=0; i < ssize ; i++) 473 { 474 if(shapeS[i].live) 475 { 476 if(((shapeS[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx +7) 477 &&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx)) 478 &&!((shapeS[i].pos_rect_y1 < shapeB.pos_Y + shapeB.boundy) 479 &&(shapeS[i].pos_rect_y1+40 > shapeB.pos_Y - shapeB.boundy))) 480 { 481 shapeB.LeftCollision = false; 482 shapeB.pos_Y = shapeS[i].pos_rect_y1 - shapeB.boundy - 7 ; 483 shapeB.isOnSquare = true; 484 } 485 486 else if((shapeS[i].pos_rect_x1 < shapeB.pos_X + shapeB.boundx+ 7) 487 &&(shapeS[i].pos_rect_x1 > shapeB.pos_X - shapeB.boundx)) 488 { 489 shapeB.LeftCollision = true; 490 } 491 492 else 493 { 494 shapeB.LeftCollision = false; 495 496 } 497 } 498 } 499}

Steve Terry
Member #1,989
March 2002
avatar

I still don't see anywhere where you are updating your x position. If you are on top of a square just keep your x position incrementing.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

varynoob
Member #14,058
February 2012

The x position, increments by itself. i.e the Square keeps on scrolling towards left, while ball is in the same position during the gameplay!
What i meant to say was, when the ball collides with the left of square or comes on top, it magnetically sticks to the square, until the square scrolls beyond the ball's position!

Also, the LeftCollision value does not get updated in the Update() section!!

#SelectExpand
1#include "GameObject.h" 2 3GameObject :: GameObject() 4{ 5 x = 0; 6 y = 0; 7 8 velX = 0; 9 velY = 0; 10 11 dirX = 0; 12 dirY = 0; 13 14 boundX = 0; 15 boundY = 0; 16 17 LeftCollision = false; 18 19 20 maxFrame = 0; 21 curFrame = 0; 22 frameCount = 0; 23 frameDelay = 0; 24 frameWidth = 0; 25 frameHeight = 0; 26 animationColumns = 0; 27 animationDirection = 0; 28 29 image = NULL; 30 31 alive = true; 32 collidable = true; 33} 34 35void GameObject :: Destroy() 36{ 37 38} 39 40void GameObject :: Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY) 41{ 42 GameObject :: x = x; 43 GameObject :: y = y; 44 45 GameObject :: velX = velX; 46 GameObject :: velY = velY; 47 48 GameObject :: dirX = dirX; 49 GameObject :: dirY = dirY; 50 51 GameObject :: boundX = boundX; 52 GameObject :: boundY = boundY; 53} 54void GameObject :: Update() 55{ 56 if(LeftCollision == false) // Always remains false!! Inspite of it being updated to TRUE, below in the Check Collision section!! 57 { 58 x += velX * dirX; 59 y += velY * dirY; 60 } 61} 62void GameObject :: Render() 63{ 64} 65 66bool GameObject :: CheckCollisions(GameObject *otherObject) 67{ 68 float oX = otherObject->GetX(); 69 float oY = otherObject->GetY(); 70 71 int obX = otherObject->GetBoundX(); 72 int obY = otherObject->GetBoundY(); 73 74 75 if( x + boundX > oX - obX && 76 x - boundX < oX + obX && 77 y + boundY > oY - obY && 78 y - boundY < oY + obY && otherObject->GetID() == TRIANGLE) 79 { 80 return true; 81 } 82 else if(((oX < x + boundX + 14)&&(oX+ 40 >x - boundX))&&!((oY < y + boundY)&&(oY+40 > y - boundY)) 83 && otherObject->GetID() == SQUARE) 84 { 85 y = oY - boundX; 86 //x = oX + 40; 87 return true; 88 } 89 else if((oX < x + boundX + 14) && (oX > x - boundX) && otherObject->GetID() == SQUARE) 90 { 91 LeftCollision = true; //this becomes true, as and when condition is me 92 return false; 93 } 94 else 95 { 96 return false; 97 LeftCollision = false; 98 } 99} 100void GameObject :: Collided(int objectID) 101{ 102 103} 104bool GameObject :: Collidable() 105{ 106 return alive && collidable; 107}

Go to: