Hello!
There is such a hierarchy:
class RigidBody{ ... protected: sf::FloatRect rect; }; class Item : public RigidBody{ ... }; class Block : public Item{ ... }; Individually, all classes work fine. But if I convert Block *[] to RigidBody ** for processing physics inside the parent, the value of the rect variable is not saved (reset):
Block *blocks[20]; ... RigidBody **bodys = (RigidBody **)blocks; How to fix this? Thank!
UPDATE: updatePhysics
void RigidBody::updatePhysics(sf::Time dt, RigidBody **bodys, int numBodys){ float elapsed = dt.asSeconds(); if(this->dynamic){ this->velocity.x += this->acceleration.x * elapsed; // NOTE: Maybe I'll need to add gravity by x this->velocity.y += this->acceleration.y * elapsed + GRAVITY; if(abs(this->velocity.x) > this->maxVelocity.x){ this->velocity.x = ((this->velocity.x > 0) ? 1 : -1) * this->maxVelocity.x; } if(abs(this->velocity.y) > this->maxVelocity.y){ this->velocity.y = ((this->velocity.y > 0) ? 1 : -1) * this->maxVelocity.y; } this->rect.left += this->velocity.x * elapsed; this->rect.top += this->velocity.y * elapsed; } else { this->velocity.x += this->acceleration.x * elapsed; this->velocity.y += this->acceleration.y * elapsed; this->rect.left += this->velocity.x * elapsed; this->rect.top += this->velocity.y * elapsed; } for(int i = 0; i < numBodys; i++){ this->checkCollisionAndResolve(bodys[i]); } } UPDATE 2: checkCollisionAndResolve
bool RigidBody::checkCollisionAndResolve(RigidBody *object){ // TODO: add other collision types support bool isColliding = this->rect.intersects(object->rect); if(object->bodyType != RIGIDBODY_NONE && isColliding){ sf::Vector2f centers[2]; centers[0] = object->getCenter(); centers[1] = this->getCenter(); int distanceByX = (centers[0].x - centers[1].x) / (object->getWidth() / 2); int distanceByY = (centers[0].y - centers[1].y) / (object->getHeight() / 2); int absoluteDistanceByX = abs(distanceByX); int absoluteDistanceByY = abs(distanceByY); if(abs(absoluteDistanceByX - absoluteDistanceByY) < 0.1f){ // Corner collision if(distanceByX < 0){ this->rect.left = object->getRight(); if(this->velocity.x < 0){ this->velocity.x = 0; } } else { this->rect.left = object->getLeft() - this->getWidth(); if(this->velocity.x > 0){ this->velocity.x = 0; } } if(distanceByY < 0){ this->rect.top = object->getBottom(); if(this->velocity.y < 0){ this->velocity.y = 0; } } else { this->rect.top = object->getTop() - this->getHeight(); if(this->velocity.y > 0){ this->velocity.y = 0; } } if(rand() % 2){ this->velocity.x = -this->velocity.x * object->getRestitution(); } else { this->velocity.y = -this->velocity.y * object->getRestitution(); } } else if(absoluteDistanceByX > absoluteDistanceByY){ // Side collision if(distanceByX < 0){ this->rect.left = object->getRight(); this->velocity.x = 0; } else { this->rect.left = object->getLeft() - this->getWidth(); this->velocity.x = 0; } this->velocity.x = -this->velocity.x * object->getRestitution(); } else { // Bottom or top collision if(distanceByY < 0){ this->rect.top = object->getBottom(); this->velocity.y = 0; } else { this->rect.top = object->getTop() - this->getHeight(); this->velocity.y = 0; } //this->velocity.y = -this->velocity.y * object->getRestitution(); } } return isColliding; }
rectis nullified when casting toRigidBody **. - user26699sf::FloatRect rect;-protected, and in pastebin -public. Is this a typo or is the code wrong? Show the value ofrectbefore and after the lineRigidBody **bodys = (RigidBody **)blocks;- add code withrectoutput in the question around this line. - Igor