Detect initial collision of two box2d bodies without continuous collision - cocos2d-iphone

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

Related

Unreal engine c++ write delegate OnScreenshotCaptured

I am new to C++. I've wrote code in C# and PHP.Since I am using Unreal engine I am trying to learn C++. For my project I need to make a screenshot in-game and show it immediately so I want to get it as a texture.
I made a blueprint node which calls this function i've made:
void UMyBlueprintFunctionLibrary::TakeScreenshot()
{
FScreenshotRequest::RequestScreenshot(true);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Tried to take screenshot");
}
When I hover my mouse above RequestScreenshot I see the following pop-up:
"Screenshot can be read from memory by subscribing to the viewsport OnScreenshopCaptured delegate"
So that is what I try to do but I have no idea how I looked up this:
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameViewportClient/OnScreenshotCaptured/
Can someone tell me how to implement this and how you see/know how to implement it?
I have an alternative, no delegate, but FRenderTarget::ReadPixel() to some buffer you allocated, by implementing your own UGameViewportClient (inherit it), and overriding Draw() function.
I'll show the essential codes, but not complete.
void UMyGameViewportClient::Draw(FViewport* Viewport, FCanvas* SceneCanvas)
{
Super::Draw(Viewport, SceneCanvas);
if (any_condition_you_need) {
CaptureFrame();
}
}
void UMyGameViewportClient::CaptureFrame()
{
if (!Viewport) {
return;
}
if (ViewportSize.X == 0 || ViewportSize.Y == 0) {
return;
}
ColorBuffer.Empty(); // Declare this in header as TArray<FColor>
if (!Viewport->ReadPixels(ColorBuffer, FReadSurfaceDataFlags(),
FIntRect(0, 0, ViewportSize.X, ViewportSize.Y)))
{
return;
}
SaveThumbnailImage();
}
void UMyGameViewportClient::SaveThumbnailImage()
{
IImageWrapperModule& wrappermodule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
auto wrapper_ptr = wrappermodule.CreateImageWrapper(EImageFormat::PNG);
for (int i = 0; i < ColorBuffer.Num(); i++)
{
auto ptr = &ColorBuffer[i];
auto r = ptr->R;
auto b = ptr->B;
ptr->R = b;
ptr->B = r;
ptr->A = 255;
} // not necessary, if you like bgra, just change the following function argument to ERGBFormat::BGRA
wrapper_ptr->SetRaw(&ColorBuffer[0], ColorBuffer.Num() * 4,
ViewportSize.X, ViewportSize.Y, ERGBFormat::RGBA, 8);
FFileHelper::SaveArrayToFile(wrapper_ptr->GetCompressed(), *ThumbnailFile);
}

Manipulation iterator list + pointers

I'm trying to check all collisions of all entities. I do it like:
My parent Class "Entity" has a static std::list<Entity*> allEntities
The constructors of all child class have this sentence Entity::AddEntity((*this));
I have a property std::list< Entity*> m_collisionWith; it will take all pointers of entities with intersection
After, I have a static function in the Entity class :
for (std::list<Entity*>::iterator it = allEntities.begin(); it != allEntities.end(); it++) //to check everything...
{
for (std::list<Entity*>::iterator itSec = allEntities.begin(); itSec != allEntities.end(); itSec++) // ... with everything
{
if ((*it)->m_spriteCharacter[(*it)->m_currentFrameCharacter].getGlobalBounds().intersects((*itSec)->m_spriteCharacter[(*itSec)->m_currentFrameCharacter].getGlobalBounds()) && *itSec != (*it)) //to_check if we have an intersection between two objects
{
if ((*it)->m_collisionWith.empty())
{
(*it)->m_collisionWith.push_back((*itSec));
}
else
{
for (std::list<Entity*>::iterator itThr = (*it)->m_collisionWith.begin(); itThr != (*it)->m_collisionWith.end(); itThr++)
{
if ((*itThr) != (*itSec)) //to check if the second object is not here yet. itThr will be every objects in m_collisionWith
{
(*it)->m_collisionWith.push_back((*itSec));
}
}
}
}
}
}
So I would like to check if an entity has an intersection with an another entity and if it is, I would like not add it in the m_collision.
Normally, if I have 3 entities, m_collision.size() is equals 3 but it is growing up nonstop
for (std::list<Entity*>::iterator itThr = (*it)->m_collisionWith.begin(); itThr != (*it)->m_collisionWith.end(); itThr++)
{
if ((*itThr) != (*itSec)) //to check if the second object is not here yet. itThr will be every objects in m_collisionWith
{
(*it)->m_collisionWith.push_back((*itSec));
}
}
This pushes itSec at the back for every element in (*it)->m_collisionWith that is not equal to *itSec. It should be:
bool exists = false;
for (std::list<Entity*>::iterator itThr = (*it)->m_collisionWith.begin(); itThr != (*it)->m_collisionWith.end(); itThr++)
{
if ((*itThr) != (*itSec)) //to check if the second object is not here yet. itThr will be every objects in m_collisionWith
{
exists = true;
break;
}
}
if (!exists)
{
(*it)->m_collisionWith.push_back((*itSec));
}

Can't increment integer in Cocos2d-x V 3.0

I need to increment an integer.I have this code here :
Utils.h :
class Utils{
static HUD* hudLayer();
static Layer* layerWithTag(int tag);
};
Utils.cpp :
HUD* Utils::hudLayer(){
return (HUD*)Utils::layerWithTag(TAG_HUD);
}
Layer* Utils::layerWithTag(int tag)
{
Scene *sc = Director::getInstance()->getRunningScene();
if (sc->getTag() == TAG_GAME_SCENE) {
Layer *layer = (Layer *)sc->getChildByTag(tag);
return layer;
}
return NULL;
}
HUD.h
class HUD : public Layer{
public:
int score1;
Label* scoreLabel1;
virtual bool init();
void didScore();
CREATE_FUNC(HUD);
};
HUD.cpp :
bool HUD::init(){
if(!Layer::init()){return false;}
score1 = 0;
scoreLabel1 = Label::createWithSystemFont(CCString::createWithFormat("Score : %d",score1)->getCString(), “Arial“, 64);
scoreLabel1->setAnchorPoint(Point(0.0f, 1.0f));
scoreLabel1->setPosition(Point(20, Utils::s().height-10));
this->addChild(scoreLabel1);
return true;
}
void HUD::didScore(){
score1+=10; // Error HERE after coming from onTouchBegan (says parentis null)
scoreLabel1->setString(CCString::createWithFormat("Score : %d",score1)->getCString());
}
In GameScene.h now i have this in onTouchBegan method :
bool GameScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
Point location = touch->getLocationInView();
location = Director::getInstance()->convertToGL(location);
if(location.x < 300.0f){
Utils::hudLayer()->didScore();
}
return true;
}
What am i doing wrong here ?
I can increment an integer in V 2.x the same way but not in V 3.0 . WHY ?
In cocos2d-x v3 is a new listener system for events (touches, accelerometr etc.), you need to create listener for getting touches. In GameScene::init you should put something like that
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener1, this);
Look at:
http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism

strange contact between bodies

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.

Inherited variables are not reading correctly when using bitwise comparisons

I have a few classes set up for a game, with XMapObject as the base, and XEntity, XEnviron, and XItem inheriting it.
MapObjects have a number of flags, one of them being MAPOBJECT_SOLID. My problem is that XEntity is the only class that correctly detects MAPOBJECT_SOLID. Both Items are Environs are always considered solid by the game, regardless of the flag's state. What is important is that Environs and Item should almost never be solid.
Each class has a very basic preliminary constructor, just initializing all varibles to zero or NULL. During the CreateX() phase, Objects are linked into the map, set into a linked linked list.
Both XItem and XEnviron are a tad sloppy. They are both new, and in the middle or my debugging attempts.
Here are the relevent code samples:
XMapObject:
#define MAPOBJECT_ACTIVE 1
#define MAPOBJECT_RENDER 2
#define MAPOBJECT_SOLID 4
class XMapObject : public XObject
{
public:
Uint8 MapObjectType,Location[2],MapObjectFlags;
XMapObject *NextMapObject,*PrevMapObject;
XMapObject();
void CreateMapObject(Uint8 MapObjectType);
void SpawnMapObject(Uint8 MapObjectLocation[2]);
void RemoveMapObject();
void DeleteMapObject();
void MapObjectSetLocation(Uint8 Y,Uint8 X);
void MapObjectMapLink();
void MapObjectMapUnlink();
};
XMapObject::XMapObject()
{
MapObjectType = 0;
Location[0] = 0;
Location[1] = 1;
NextMapObject = NULL;
PrevMapObject = NULL;
}
void XMapObject::CreateMapObject(Uint8 Type)
{
MapObjectType = Type;
}
void XMapObject::SpawnMapObject(Uint8 MapObjectLocation[2])
{
if(!(MapObjectFlags & MAPOBJECT_ACTIVE)) { MapObjectFlags += MAPOBJECT_ACTIVE; }
Location[0] = MapObjectLocation[0];
Location[1] = MapObjectLocation[1];
MapObjectMapLink();
}
XEntity:
XEntity *StartEntity = NULL,*EndEntity = NULL;
class XEntity : public XMapObject
{
public:
Uint8 Health,EntityFlags;
float Speed,Time;
XEntity *NextEntity,*PrevEntity;
XItem *IventoryList;
XEntity();
void CreateEntity(Uint8 EntityType,Uint8 EntityLocation[2]);
void DeleteEntity();
void EntityLink();
void EntityUnlink();
Uint8 MoveEntity(Uint8 YOffset,Uint8 XOffset);
};
XEntity::XEntity()
{
Health = 0;
Speed = 0;
Time = 1.0;
EntityFlags = 0;
NextEntity = NULL;
PrevEntity = NULL;
IventoryList = NULL;
}
void XEntity::CreateEntity(Uint8 EntityType,Uint8 EntityLocation[2])
{
CreateMapObject(EntityType);
SpawnMapObject(EntityLocation);
if(!(MapObjectFlags & MAPOBJECT_SOLID) { MapObjectFlags += MAPOBJECT_SOLID; }
EntityFlags = ENTITY_CLIPPING;
Time = 1.0;
Speed = 1.0;
EntityLink();
}
void XEntity::EntityLink()
{
if(StartEntity == NULL)
{
StartEntity = this;
PrevEntity = NULL;
NextEntity = NULL;
}
else
{
EndEntity->NextEntity = this;
}
EndEntity = this;
}
XEnviron:
class XEnviron : public XMapObject
{
public:
Uint8 Effect,TimeOut;
void CreateEnviron(Uint8 Type,Uint8 Y,Uint8 X,Uint8 TimeOut);
};
void XEnviron::CreateEnviron(Uint8 EnvironType,Uint8 Y,Uint8 X,Uint8 TimeOut)
{
CreateMapObject(EnvironType);
Location[0] = Y;
Location[1] = X;
SpawnMapObject(Location);
XTile *Tile = GetTile(Y,X);
Tile->Environ = this;
MapObjectFlags = MAPOBJECT_ACTIVE + MAPOBJECT_SOLID;
printf("%i\n",MapObjectFlags);
}
XItem:
class XItem : public XMapObject
{
public:
void CreateItem(Uint8 Type,Uint8 Y,Uint8 X);
};
void XItem::CreateItem(Uint8 Type,Uint8 Y,Uint8 X)
{
CreateMapObject(Type);
Location[0] = Y;
Location[1] = X;
SpawnMapObject(Location);
}
And lastly, the entity move code. Only entities are capable of moving themselves.
Uint8 XEntity::MoveEntity(Uint8 YOffset,Uint8 XOffset)
{
Uint8
NewY = Location[0] + YOffset,
NewX = Location[1] + XOffset;
if((NewY >= 0 && NewY < MAPY) && (NewX >= 0 && NewX < MAPX))
{
XTile *Tile = GetTile(NewY,NewX);
if(Tile->MapList != NULL)
{
XMapObject *MapObject = Tile->MapList;
while(MapObject != NULL)
{
if(MapObject->MapObjectFlags & MAPOBJECT_SOLID)
{
printf("solid\n");
return 0;
}
MapObject = MapObject->NextMapObject;
}
}
if(Tile->Flags & TILE_SOLID && EntityFlags & ENTITY_CLIPPING)
{
return 0;
}
this->MapObjectSetLocation(NewY,NewX);
return 1;
}
return 0;
}
What is wierd, is that the bitwise operator always returns true when the MapObject is an Environ or an Item, but it works correctly for Entities. For debug I am using the printf "Solid", and also a printf containing the value of the flag for both Environs and Items.
Any help is greatly appreciated, as this is a major bug for the small game I am working on. I am also very new at Object Oriented programming, anything tips, suggestions and/or criticism are also welcome.
Your problem appears to be that you never initialize MapObjectFlags in any classes other than XEnviron so, as a basic type, it will have an unspecified value in XItem, XEntity and other XMapObject derived objects. I suggest that, as a member of XMapObject you explicitly initialize it to a known value.
As a rule, it is generally a good idea to ensure that all members of basic type are explicitly initialized in the initializer list of every constructor that you define.
e.g.
XMapObject()
: MapObjectFlags(0)
, // ... other initializers
{
// Other initializations
}
You can't (legally) be calling XEntity::MoveEntity on a MapObject or Environ because they don't have such a method. If you're using static_cast to change your object pointer into an XEntity so you can call MoveEntity on it, then you really have no guarantees about how the bit operation will work. In some implementations, things may appear to work in MoveEntity, but what's actually happening is it's interpreting the other object's memory as an XEntity. When it tries to access the offset where it believes MapObjectFlags exists, it's not actually there and always has that bit set to 1.
I figured out the problem earlier today - It didn't have any relation to OO programming, inheritance, or bitwise; it was a simple scope error.
The problem was in the fact that during my quick test to get an Environ in game, I declared the new variable inside of the control switch sequence, so the next time any control was used, the Environ would act in unpredictable ways.
switch(Event.key.keysym.sym)
{
...
case SDLK_c: { XEnviron Environ; Environ.InitEnviron(...); }
...
}