Crash of moving objects

Hello everyone,

I have some code which creates two rectangles ( same dimension but different color). I want them to move when I push arrow keys (for white one) and a,s,d,w (for red one) independently eachother. Now I try to do that when they "crush" something happen. For example when they crush on the program write " game over" something like that.

But I could not do that !! I do not have any idea for such a situation.
ANYONE CAN HELP ME ?? Here is the code I have..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
#include <graphics.h>

int main() {
    int x1=20,x2=40,y1=20,y2=40,x3=400,x4=420,y3=300,y4=320;
    
    int ek=30;
    
    char button;
    
    initwindow(600,400);
    
    int x,y;
    setcolor(RED);
    rectangle(x1,y1,x2,y2); //first rectengular shape
    setcolor(WHITE);
    rectangle(x3,y3,x4,y4); //second rectengular shape
    
    for(;;){
          button=getch(); 
          
            setcolor(BLACK);
            rectangle(x1,y1,x2,y2); 
            setcolor(BLACK);
            rectangle(x3,y3,x4,y4);
            if(button==77) { x1=x1+ek; x2=x2+ek;  } // arrowkey left
            if(button==97)  { x3=x3-ek; x4=x4-ek; } // button s
            if(button==75) { x1-=ek; x2-=ek; }     //arrowkey right
            if(button==100) {x3=x3+ek; x4=x4+ek; }// button a
            if(button==72){ y1-=ek; y2-=ek; }     //arrowkey down
            if(button==115) {y3=y3+ek; y4=y4+ek;} // button d
            if(button==80) { y1+=ek; y2+=ek; }    //arrowkey up
            if(button==119) {y3=y3-ek; y4=y4-ek; } // button 119
           setcolor(RED);
           rectangle(x1,y1,x2,y2);
           setcolor(WHITE);
           rectangle(x3,y3,x4,y4);
           
     }
     
    
    closegraph();
    return 0;
    
}
Hey !! Anybody say anything !! Any help !! I think it is not possible to do that ?
You'll need to come up with an algorithm that determines whether or not the two rectangles are intersecting. Once you come up with that, you can pretty easily print a game over message or something similar if it happens to be true.
Thank you but I pretty do not know how to use an adresses in C++. I mean when they are intesect or meet somewhere how can I prove ? I need a information or some code shows they have same adresses when they crush.
Use this :
1
2
inline bool TestPoint(int x, int y, int x1, int y1, int x2, int y2){
return(x>=x1&&y>=y1&&x<=x2&&y<=y2);}

Your rectangle has 4 points so you have to test it 4 times.
Last edited on
@JM

Bad Advice - you should not compare floats like that. It will fail on values that are near to equal.

Can you figure out why this fails?

1
2
3
4
float a = 0.1;
float b = 10.0f * a;

if (b == 1.0f) {}  //false 


The OP had int values anyway. It is possible to get it to work with floating-point, but involves more work.
Last edited on
Thanks TheIdeasMan, I didn't know that. :)
Edit : The code has been changed... Enjoy! If you need help just ask...
Last edited on
Yes I tried that not possible to compare floats :)
And, so come on guys I need to figure out how can it be. A few hint please !!
The code (didn't read carefully) - changed :)
And your hint... About?
@Xphysics

Yes it is possible to compare them, but not directly - as I said it takes more work.

Here is the most valuable advice that anyone can give to anyone who uses a computer connected to the internet.

USE GOOGLE


Not trying to be harsh, but you shouldn't just sit back and expect answers, do some research of your own. Having said that, be careful of advice from forums that you might find - it is not always right.

My apologies if you live in Iran, North Korea or somewhere similarly oppressed so that Google is not allowed.

@JM

Have you answered my question? It is up to you to work it out.
I live in Turkey guys :) I google it for a long time but I do not have much time that is why I want help:) Thnks @Jm it is a little bit help to me now I know what I will do .

Thanks for help..
Why don't you use two circles to instead, thus you can determine whether the two objects crash just by testing the distance between the two circle centers.
A little imagination ....

Google C++ compare floating point.

Hope that helps.

Also I have written a number of replies about this on this forum.
Topic archived. No new replies allowed.