strange contact between bodies - cocos2d-iphone

I have a strange problem.
the shot of my players, when it collides with a body, destroys it without problems, but, when it hits the two bodies at once, it crashes.
shooting player
b2Body *shooting = [_lhelper newBodyWithUniqueName:#"shoot" world:_world];
CCSprite *shootingSprite = (CCSprite *)shootingBody->GetUserData();
shootingBody.position = pos;
shootingBody->SetTransform(b2Vec2(pos.x/PTM_RATIO,
pos.y/PTM_RATIO),
CC_DEGREES_TO_RADIANS(angle));
contact
if([spriteA tag] == ENEMY && [spriteB tag] == SHOT)
{
int animIdx = [(NSNumber*)[spriteA userData] intValue];
if(animIdx < 2)
{
[spriteA setTextureRect:MY_RECTS[animIdx]];
[spriteA setUserData:[NSNumber numberWithInt:animIdx+1]];
}
else
{
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyA]];
}
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyB]];
}
else if([spriteB tag] == ENEMY && [spriteA tag] == SHOT)
{
int animIdx = [(NSNumber*)[spriteB userData] intValue];
if(animIdx < 2)
{
[spriteB setTextureRect:MY_RECTS[animIdx]];
[spriteA setUserData:[NSNumber numberWithInt:animIdx+1]];
}
else
{
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyB]];
}
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyA]];
}
update
std::vector<Contact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos)
{
Contact contact = *pos;
//[self checkBodies2:&contact];
}
for(NSValue* val in objectThatWillBeDeleted)
{
b2Body* body = (b2Body*)[val pointerValue];
[_lhelper removeBody:body];
}
[objectThatWillBeDeleted removeAllObjects];
}
}
I do not understand where the error.

I do not understand where the error either because you didn't say where it crashes :)
But I would guess you are trying to destroy the same body twice.
When the shot hits two enemies in the same time step, the shot body gets added to the objectThatWillBeDeleted list twice. You just need to make that list unique before you destroy the contents of it.

Related

Loop-based collision system only works with the last enemy added to vector

I created an Enemy vector and used push_back twice to add two identical enemies. Inside a for loop, I created a collision system to prevent my player from going inside the enemies. However,for some reason it only works with the last enemy added.
I would like to know what should I do in order to make it work with every single enemy in that vector.
EnemyCounter = 0;
for (iter = EnemyArray.begin(); iter != EnemyArray.end(); iter++)
{
EnemyArray[EnemyCounter].update(Seconds);
window.draw(EnemyArray[EnemyCounter].CharacterSprite);
if (player.RectShape.getGlobalBounds().intersects(EnemyArray[EnemyCounter].RectShape.getGlobalBounds()))
{
if (player.LastDirection == 0) player.canMoveDown = false;
else if (player.LastDirection == 1) player.canMoveLeft = false;
else if (player.LastDirection == 2) player.canMoveRight = false;
else if (player.LastDirection == 3) player.canMoveUp = false;
}
if (!player.RectShape.getGlobalBounds().intersects(EnemyArray[EnemyCounter].RectShape.getGlobalBounds())) player.ResetCanMove();
EnemyCounter++;
}

Keeps on getting terminate called after throwing an instance of 'std::out_of_range'

bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
///**Get borad*/
std::vector<Square*> board = game->getBoard();
int maxDistanceTravel = 0;
int playerCanTravel = 0;
//first square and last square must be present
if (path[0] == 0 && path[path.size() - 1] == (board.size() - 1)) {
for (int i = 0; i < path.size() - 1; i++) {
/**Max distance of each board from player*/
maxDistanceTravel = compute(board.at(path[i]), player);
playerCanTravel = path[i + 1] - path[i];
if ((playerCanTravel > maxDistanceTravel) && (playerCanTravel <= 0)) {
return false;
}
}
return true;
}
return false;
}
I am a new c++ learner and I am getting the same error over and over again but could not figure out what is wrong with it, it is not obviously an out of range, please help, thanks.
I would first try doing some early checking to make sure you're inputs are valid. Perhaps something like:
bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
assert(!path.empty());
if (path.size() < 2) return false; // or whatever your minimum size is.
As long as you're at it, you should probably scrub the other inputs.
bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
assert(!path.empty());
assert(game);
assert(player);
if (path.size() < 2) return false; // or whatever your minimum size is.
I have a rule of thumb to use references for arguments that should never be null. It looks like in your code that game isn't allowed to be null. So perhaps you could change the function:
bool GameUtil::isValidPath(std::vector<int>& path, Player& player, Game& game) {
assert(!path.empty());
if (path.size() < 2) return false; // or whatever your minimum size is.
And then change where you call. So probably in your code you have something like:
// somewhere buried in your code:
Game * myGame = .../// however you created it.
Game * myPlayer = .../// however you created it.
GameUtil * myGameUtil = .../// however you created it.
...
auto isValid = myGameUtil->isValidPath(path, myPlayer, myGame);
You change it to:
// somewhere buried in your code:
Game * myGame = .../// however you created it.
Game * myPlayer = .../// however you created it.
GameUtil * myGameUtil = .../// however you created it.
...
auto isValid = myGameUtil->isValidPath(path, *myPlayer, *myGame);
Hope that helps.

arc4random() stopped working after project update, Thread 1: EXC_ARITHMETIC

I got a warning on xcode 6.0.1 that I needed to update my project, so I did...
...but then I got issues with arc4random() % instead
Everything was working fine before this update, but now I get the following message:
"Thread 1: EXC_ARITHMETIC (code = EXC_1386_DIV, Subcode = 0x0) and the game crashes.
I tried with arc4random_uniform but the game seems to "pause" and nothing happens. What I mean is that I could see that it entered the if(bType == pt || bType == pl) statement but there is seems to "pause" and loop endlessly.
any suggestions?
-(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl amount:(int)amountOfbricks{
CCLOG(#"BRICKS:placeInGrid");
//CCLOG(#"pt %d", pt);
//CCLOG(#"pl %d", pl);
int bType = arc4random() % amountOfbricks;
//int bType = arc4random_uniform(amountOfbricks);
if(bType == pt || bType == pl){
CCLOG(#"bType == pt || bType == pl");
[self placeInGrid:place pt:pt pl:pl amount:amountOfbricks];
return;
}
else{
CCLOG(#"else");
CCSpriteBatchNode * b = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];
mySprite = [CCSprite spriteWithBatchNode:b rect:[self setBrickType:bType]];
[b addChild:mySprite z:1];
self.bricksType =bType;
[self.mySprite setPosition:place];
}
}
UPDATE:
the if-else statement below looks for saved data at the beginning of the game, to see if it´s a saved game or a new game.
The problem as it seems, after the project update is that the game thinks that there is already saved data and it goes to the first if statement ( if(gameData)), causing amountofbricks to be equal to 0, instead of going into the else statement where amountofbricks is equal to 4.
I don´t know how to solve this issue.
if ([[GameManager sharedGameManager] isContinuePressed] == NO && [[GameManager sharedGameManager] isNewPressed] == YES) {
if(gameData){
CCLOG(#"gameData && isContinuePressed == NO && isNewPressed == YES");
//Set the local instance of myobject to the object held in the gameState filler with the key "myObject"
level = 1;
[[GameManager sharedGameManager] setHScore:[decoder decodeIntegerForKey:#"HighScore"]];
highscore = [[GameManager sharedGameManager] ReturnHScore];
countTime = 300;
AmountOfBricks = [[GameManager sharedGameManager] ReturnAmountOfBricks];
BeginningOfGame = YES;
CCLOG(#"AmountOfBricks %d", AmountOfBricks);
}
else{
CCLOG(#"!gameData && isContinuePressed == NO && isNewPressed == YES");
if([[GameManager sharedGameManager]isThereASavedGame] ==YES){
CCLOG(#"isThereASavedGame == YES");
score = 0;
highscore = [[GameManager sharedGameManager] ReturnHScore];
level = 1;
countTime = 300;
AmountOfBricks = 4;
BeginningOfGame = YES;
CCLOG(#"AmountOfBricks %d", AmountOfBricks);
}
else{
CCLOG(#"isThereASavedGame == NO");
score = 0;
highscore = 0;
level = 1;
countTime = 300;
AmountOfBricks = 4;
BeginningOfGame = YES;
CCLOG(#"AmountOfBricks %d", AmountOfBricks);
}
}
}
UPDATE: I found the issue.
the issue was in my game manager.m file.
NSMutableData *gameData;
NSKeyedUnarchiver *decoder = nil;
NSString *documentPath = [documentsDirectory stringByAppendingPathComponent: #"gameState.dat"];
gameData = [NSData dataWithContentsOfFile:documentPath];//before the update, I as using this line. was working perfectly. after the update I got a warning on this line "incompatible pointer" and xcode recommended to update to the line below but the line below started to return a zero value. which caused the game to crash.
//gameData = [NSMutableData dataWithData:[NSData dataWithContentsOfFile:documentPath]];
I wager amountOfBricks is 0. You can't modulo by 0 like you can't divide by 0.

saving contact bodies to be destroyed

In my code I would like to destroy one of two contacted bodies. Within the beginContact the following method in CCPhysicsSprite is called:
-(void)contactMade:(CCPhysicsSprite*)contactedSprite {
int spriteTag1 = self.tag;
int spriteTag2 = contactedSprite.tag;
if (((spriteTag1 == 3) && (spriteTag2 == 4)) || ((spriteTag1 == 4) && (spriteTag2 == 3)) {
CCPhysicsSprite* heroSprite = (CCPhysicsSprite*)[self getChildByTag:4];
b2World* world;
world->DestroyBody(heroSprite.b2Body);
heroSprite.b2Body = NULL;
[heroSprite.parent removeChild:heroSprite];
}
I get a signal SIGABRT pointing to
b2Assert(m_bodyCount > 0);
After searching on this issue. I read that the contact body has to be saved and destroyed after the timestep. How can I do this, given that I have set my contact conditions in the CCPhyscisSprite.
You can add a flag ( like : isDead ... ) to your physical object and in collision event just change that flag value to TRUE .
-(void) CollisionBegin:(b2Fixture*)target With:(b2Fixture*) source
{
if ( target->GetBody()->GetType() == b2_dynamicBody)
{
yourCustomClass *temp = (yourCustomClass *)target->GetBody()->GetUserData();
temp->isDead = true ;
}
}
Then in update function after step get all physical world's object and find that specific object by flag ( Here : isDead ) , and destroy that .
-(void) update: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 3;
world->Step(dt, velocityIterations, positionIterations);
// remove your box2d object here , after step function
for ( b2Body *b = world->GetBodyList(); b; )
{
b2Body *baba = b->GetNext();
if ( b->GetUserData() != NULL && b->GetType() == b2_dynamicBody)
{
yourCustomClass *t = (yourCustomClass *)b->GetUserData();
if ( t->isDead )
{
world->DestroyBody(b); // remove physical body
[self removeChild:t]; // remove node from super layer
}
}
b = baba ;
}
}

Detect initial collision of two box2d bodies without continuous collision

I have some simple box2d bodies setup with a contact listener like so:
#import "MyContactListener.h"
MyContactListener::MyContactListener() : _contacts() {
}
MyContactListener::~MyContactListener() {
}
void MyContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
b2Body *A = contact->GetFixtureA()->GetBody();
b2Body *B = contact->GetFixtureA()->GetBody();
NSLog(#"Collision detected!");
PLAYSOUND(COLLISION);
}
void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
And I need to play a sound when two bodies have collided. However this implementation detects continuous collisions so the sound is played when the bodies are touching. My knowledge of box2d and C++ is already very limited, is there a simple way to detect a new collision without detecting continuous collisions?
You have the right basic idea, but it needs some refinement.
In your BeginContact(...) call, you have:
PLAYSOUND(COLLISION);
Instead of playing the sound here, what you should do is enqueue some other system to play the sound for this particular pair. Set the userdata tag of your bodies to the pointer to the class (or some other ID to keep track of entities). Like so:
class EntityContactListener : public ContactListener
{
private:
GameWorld* _gameWorld;
EntityContactListener() {}
typedef struct
{
Entity* entA;
Entity* entB;
} CONTACT_PAIR_T;
vector<CONTACT_PAIR_T> _contactPairs;
public:
virtual ~EntityContactListener() {}
EntityContactListener(GameWorld* gameWorld) :
_gameWorld(gameWorld)
{
_contactPairs.reserve(128);
}
void NotifyCollisions()
{
Message* msg;
MessageManager& mm = GameManager::Instance().GetMessageMgr();
for(uint32 idx = 0; idx < _contactPairs.size(); idx++)
{
Entity* entA = _contactPairs[idx].entA;
Entity* entB = _contactPairs[idx].entB;
//DebugLogCPP("Contact Notification %s<->%s",entA->ToString().c_str(),entB->ToString().c_str());
msg = mm.CreateMessage();
msg->Init(entA->GetID(), entB->GetID(), Message::MESSAGE_COLLISION);
mm.EnqueueMessge(msg, 0);
msg = mm.CreateMessage();
msg->Init(entB->GetID(), entA->GetID(), Message::MESSAGE_COLLISION);
mm.EnqueueMessge(msg, 0);
}
_contactPairs.clear();
}
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
}
// BEWARE: You may get multiple calls for the same event.
void BeginContact(b2Contact* contact)
{
Entity* entA = (Entity*)contact->GetFixtureA()->GetBody()->GetUserData();
Entity* entB = (Entity*)contact->GetFixtureB()->GetBody()->GetUserData();
//DebugLogCPP("Begin Contact %s->%s",entA->ToString().c_str(),entB->ToString().c_str());
if(entA->GetGroupID() == entB->GetGroupID())
{ // Can't collide if they are in the same group.
return;
}
assert(entA != NULL);
assert(entB != NULL);
for(uint32 idx = 0; idx < _contactPairs.size(); idx++)
{
if(_contactPairs[idx].entA == entA && _contactPairs[idx].entB == entB)
return;
// Not sure if this is needed...
if(_contactPairs[idx].entA == entB && _contactPairs[idx].entA == entB)
return;
}
CONTACT_PAIR_T pair;
pair.entA = entA;
pair.entB = entB;
_contactPairs.push_back(pair);
}
// BEWARE: You may get multiple calls for the same event.
void EndContact(b2Contact* contact)
{
/*
Entity* entA = (Entity*)contact->GetFixtureA()->GetBody()->GetUserData();
Entity* entB = (Entity*)contact->GetFixtureB()->GetBody()->GetUserData();
DebugLogCPP("End Contact %s->%s",entA->ToString().c_str(),entB->ToString().c_str());
*/
}
};
The last part of this is to NOT play the sound again for a short time even if a collision occurs. You can do this by creating a stopwatch or counting down from a fixed time on the entity update cycles.
Was this helpful?
First set a timer like this..
[self schedule:#selector(check collision:)];
and in this method
- (void)tick:(ccTime) dt
{
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
{
//--------------My contact Listener Start-------------------------
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
// Here get your sprite and make their fixture and check...
if ((contact.fixtureA == tempballFixture && contact.fixtureB == _mainballFixture) ||
(contact.fixtureA == _mainballFixture && contact.fixtureB == tempballFixture))
{
if(mainDelegate.music_playing == TRUE)
{
[[SimpleAudioEngine sharedEngine] playEffect:#"Rock impact.mp3"];
}
//-------------collision count for update score value--------
}
}