Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Updation problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Varypro   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 March 2012 - 12:41 PM

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}


Sponsor:

#2 Dawoodoz   Members   -  Reputation: 294

Like
0Likes
Like

Posted 01 March 2012 - 12:54 PM

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++.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#3 Washu   Senior Moderators   -  Reputation: 4900

Like
1Likes
Like

Posted 01 March 2012 - 01:02 PM

else
{
    return false;
    LeftCollision = false;
}
And you expect that to work...how?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#4 Varypro   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 March 2012 - 01:12 PM

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!!

#5 Dawoodoz   Members   -  Reputation: 294

Like
-1Likes
Like

Posted 01 March 2012 - 01:12 PM

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.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#6 Washu   Senior Moderators   -  Reputation: 4900

Like
0Likes
Like

Posted 01 March 2012 - 01:15 PM

You do realize that ANYTHING AFTER a return statement within the current scope is not executed, right?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#7 Varypro   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 March 2012 - 01:42 PM

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!!

#8 slayemin   Members   -  Reputation: 2425

Like
0Likes
Like

Posted 01 March 2012 - 04:18 PM

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.

Eric Nevala

Indie Developer | Dev blog





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS