• Advertisement
Sign in to follow this  

C++ Updation problem

This topic is 2153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Problem is in the updation of LeftCollision, which is of bool type!

It goes to true, when the condition is met in CheckCollision(), but the 'true' value is not reflected in the GameObject::Update() section, where if(LeftCollision==false).... is always false!!

Why??

What could be the problem ??



#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; //initial value (it is a Public: bool type variable)
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) //should stop when 'true', but never stops
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 - 10;
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; //LC updates to true, but does not reflect the value in the Update section above!!
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}

Share this post


Link to post
Share on other sites
Advertisement
Try using breakpoints and look if it execute line 58 to 59 anyway. An access violation somewhere else can make the program pointer go nuts in C++.

Share this post


Link to post
Share on other sites

else
{
return false;
LeftCollision = false;
}

And you expect that to work...how?

Share this post


Link to post
Share on other sites


else
{
return false;
LeftCollision = false;
}

And you expect that to work...how?

Else, statement is for, when ever the collision is not happening, things should go as they are supposed to be. If i'll remove that line, values wont revert back when left collision is not happening.





Try using breakpoints and look if it execute line 58 to 59 anyway. An access violation somewhere else can make the program pointer go nuts in C++.



58/9 are responsible for Scrolling of Triangles and Squares from Right to Left, and they work all the time!!

Share this post


Link to post
Share on other sites


else
{
return false;
LeftCollision = false;
}

And you expect that to work...how?


You found it.

"return false" will exit the function and not execute anything after it. A good convention is to only return a result on the last line.

Share this post


Link to post
Share on other sites
You do realize that ANYTHING AFTER a return statement within the current scope is not executed, right?

Share this post


Link to post
Share on other sites
If i remove, the return false, weird things come up, one of the things stop being rendered!! and only gets updated, ie becomes invisible on screen!! Though, square is rendered, but still the game does not pause when LeftCollision becomes true!!

Share this post


Link to post
Share on other sites

If i remove, the return false, weird things come up, one of the things stop being rendered!! and only gets updated, ie becomes invisible on screen!! Though, square is rendered, but still the game does not pause when LeftCollision becomes true!!


Switch lines 96 and 97. If this is causing weirdness, it's because theres something wrong elsewhere in your code. It's not on lines 96-97;

I suggest you insert breakpoints and step through the debugger line by line to figure out where your getting unexpected behaviors.

Also, your check collisions code looks complicated and nasty. I'd hate to have to debug it or maintain it. I recommend rewriting it to be easier to read and simplifying the logic by breaking it down a bit. I bet that when you simplify it, you'll find the bug somewhere in here and it'll become obvious.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement