Box2D, Contact Listener: Never calling BeginContact - c++

I've been trying to implement Box2D into this code, and I've been extremely stuck on this. I've searched high and low for an answer but could not find anything that would fix this problem. I looked at guides and tried fixes that helped other users such as doing b2World->SetAllowSleeping(false).
I start by creating the world and setting the contact listener. The World class contains the b2World, therefore I get the world and set the contact listener. To add, both pointers are stored within a scene class.
m_World = new World();
m_ContactListener = new ContactListener(m_World);
m_World->GetB2World()->SetContactListener(m_ContactListener);
I return the pointer and store it in my scene just in case it will be necessary later on.
My Contact Listener class is basically just the basics that every tutorial has said. I put a few lines of code into the BeginContact() to put a break point and try to see if it's working but so far it has not been working.
Here is the ContactListener.h:
#pragma once
class ContactListener : public b2ContactListener
{
private:
World* m_World;
public:
ContactListener(World* aWorld);
~ContactListener();`
virtual void BeginContact(b2Contact* aContact);
virtual void EndContact(b2Contact* aContact);
void SetWorld(World* aWorld) { m_World = aWorld; }
World* GetWorld() { return m_World; }
};
And here is the ContactListener.cpp file:
#include "pch.h"
ContactListener::ContactListener(World* aWorld)
{
m_World = aWorld;
}
ContactListener::~ContactListener()
{
}
void ContactListener::BeginContact(b2Contact* aContact)
{
b2Fixture* fixtureA = aContact->GetFixtureA();
b2Fixture* fixtureB = aContact->GetFixtureB();
void* userDataA = fixtureA->GetUserData();
std::string nameA = ((GameObject*)userDataA)->GetName();
void* userDataB = fixtureB->GetUserData();
std::string nameB = ((GameObject*)userDataB)->GetName();
}
void ContactListener::EndContact(b2Contact* aContact)
{
}
Like I said previously I added lines of code just to try and put a break point. The guides I read said to simply override the functions and everything should work. Any help with this issue would be appreciated and if you need anymore info I will gladly add any.
For the bodies, I have created two. One static and one dynamic.
Here's the function I use to create the bodies:
b2Body* World::CreateBody(b2Vec2 aPosition, float aRotation, BodyType aType, GameObject* aOwner)
{
b2BodyDef bodyDef;
bodyDef.position = aPosition;
bodyDef.angle = aRotation;
if (aType == KinematicBody)
{
bodyDef.type = b2_kinematicBody;
}
else if (aType == DynamicBody)
{
bodyDef.type = b2_dynamicBody;
}
else
{
bodyDef.type = b2_staticBody;
}
bodyDef.userData = aOwner;
return m_b2World->CreateBody(&bodyDef);
}
Then after I create the two bodies I was previously talking about:
((PlayerObject*)m_pGameObjects["Floor"])->AddBody(m_World->CreateBody(aPosBlock, aRotationBlock, World::BodyType::StaticBody, m_pGameObjects["Floor"]));
((PlayerObject*)m_pGameObjects["Player"])->AddBody(m_World->CreateBody(aPos, aRotation, World::BodyType::DynamicBody, m_pGameObjects["Player"]));

I took a look at my code once again and the tutorials and have figured out that my issue was missing fixtures. Just goes to prove that if you try to rush your code you'll always miss something. Thanks to everyone who had tried to help me with this issue.

Related

Unreal Engine 4: C++ Delegate not being called

I've been working on converting some blueprint logic over to C++. One of the things I have is a button. The button can be pressed in VR and has a delegate that is called to notify any registered functions that the button press occurred. Here is how the delegate is declared in the AButtonItem.h class.
#pragma once
#include "BaseItem.h"
#include "ButtonItem.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FButtonItemPressedSignatrue);
UCLASS()
class AButtonItem : public ABaseItem
{
GENERATED_BODY()
protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Touch)
float myMaxButtonPress;
public:
UPROPERTY(EditAnywhere, Category = Callback)
FButtonItemPressedSignatrue ButtonItem_OnPressed;
};
The delegate's broadcast function is then being called when the button is pressed like so:
ButtonItem_OnPressed.Broadcast();
(This function should defiantly be called because I have a debug statement that prints right before the call. Its also important to note this was all working when it was blueprint logic.)
Here is where I try to register with the delegate and how I declared the function that will be called:
WeaponMaker.h:
UFUNCTION()
void OnNextBladeButtonPressed();
WeaponMaker.cpp:
void AWeaponMaker::BeginPlay()
{
Super::BeginPlay();
TArray<USceneComponent*> weaponMakerComponents;
this->GetRootComponent()->GetChildrenComponents(true, weaponMakerComponents);
for (int componentIndex = 0; componentIndex < weaponMakerComponents.Num(); componentIndex++)
{
if (weaponMakerComponents[componentIndex]->GetName().Equals("NextBladeButton") == true)
{
myNextBladeButton = (AButtonItem*)weaponMakerComponents[componentIndex];
break;
}
}
if (myNextBladeButton != NULL)
{
myNextBladeButton->ButtonItem_OnPressed.AddDynamic(this, &AWeaponMaker::OnNextBladeButtonPressed);
}
}
I put a breakpoint and a print statement in the function OnNextBladeButtonPressed so I should immediately know when it works but its never happening. I also re-created the blueprint itself from scratch but still no luck. Sometimes on compile I get a crash due to the InvocationList being invalid but I haven't found much info on that issue either. Bottom line is, OnNextBladeButtonPressed is not getting called when it should be.
Edit: Here is where I call the broadcast function in my AButtonItem code. It seems to be getting called since i see the UE_LOG output in the console:
void AButtonItem::Tick(float deltaTime)
{
FTransform buttonWorldTransform;
FVector buttonLocalSpacePos;
FVector ownerLocalSpacePos;
FVector localDiff;
float buttonPressAmount;
if (myHasStarted == true)
{
Super::Tick(deltaTime);
if (myButtonComponent != NULL)
{
if (myPrimaryHand != NULL)
{
//Get the world space location of the button.
buttonWorldTransform = myButtonComponent->GetComponentTransform();
//Convert the location of the button and the location of the hand to local space.
buttonLocalSpacePos = buttonWorldTransform.InverseTransformPosition(myInitialOverlapPosition);
ownerLocalSpacePos = buttonWorldTransform.InverseTransformPosition(myPrimaryHand->GetControllerLocation() + (myPrimaryHand->GetControllerRotation().Vector() * myPrimaryHand->GetReachDistance()));
//Vector distance between button and hand in local space.
localDiff = ownerLocalSpacePos - buttonLocalSpacePos;
//Only interested in the z value difference.
buttonPressAmount = FMath::Clamp(FMath::Abs(localDiff.Z), 0.0f, myMaxButtonPress);
localDiff.Set(0.0f, 0.0f, buttonPressAmount);
//Set the new relative position of button based on the hand and the start button position.
myButtonComponent->SetRelativeLocation(myButtonInitialPosition - localDiff);
//UE_LOG(LogTemp, Error, TEXT("buttonPressAmount:%f"), buttonPressAmount);
if (buttonPressAmount >= myMaxButtonPress)
{
if (myHasBeenTouchedOnce == false)
{
//Fire button pressed delegate
if (ButtonItem_OnPressed.IsBound() == true)
{
ButtonItem_OnPressed.Broadcast();
AsyncTask(ENamedThreads::GameThread, [=]()
{
ButtonItem_OnPressed.Broadcast();
});
}
myHasBeenTouchedOnce = true;
myButtonComponent->SetScalarParameterValueOnMaterials("State", 1.0f);
Super::VibrateTouchingHands(EVibrationType::VE_TOUCH);
}
}
}
else
{
//Slowly reset the button position back to the initial position when not being touched.
FVector newPosition = FMath::VInterpTo(myButtonComponent->GetRelativeTransform().GetLocation(), myButtonInitialPosition, deltaTime, 10.0f);
myButtonComponent->SetRelativeLocation(newPosition);
}
}
}
}
First of all:
UPROPERTY(EditAnywhere, Category = Callback)
FButtonItemPressedSignatrue ButtonItem_OnPressed;
This should be:
UPROPERTY(BlueprintAssignable, Category = Callback)
FButtonItemPressedSignatrue ButtonItem_OnPressed;
For convenience.
Secondly the tick function may be called before begin play is executed for a number of reasons. Your even't won't be broadcasted if the game hasn't begin play yet. So to avoid just add a check in your tick function.
if(bHasBegunPlay)
{
// .. your logics ...
}
Sometimes on compile I get a crash due to the InvocationList being invalid but I haven't found much info on that issue either. Bottom line is, OnNextBladeButtonPressed is not getting called when it should be.
I don't see any issue in the code from the question. At my glance, the issue could be in different location. I would suspect that AWeaponMaker had been deleted at moment of broadcasting.

Box2D Contact Listener Collision Only Works Intermittently

I have a dynamic body colliding with a dynamic body and a simple contact listener class shown below:
void myContactListener::BeginContact(b2Contact* contact)
{
void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData();
if (bodyUserData)
{
static_cast<EnemyEntity*>(bodyUserData)->startContact();
}
bodyUserData = contact->GetFixtureB()->GetBody()->GetUserData();
if (bodyUserData)
{
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
}
void myContactListener::EndContact(b2Contact* contact)
{
void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData();
if (bodyUserData)
static_cast<EnemyEntity*>(bodyUserData)->endContact();
bodyUserData = contact->GetFixtureB()->GetBody()->GetUserData();
if (bodyUserData)
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
Whenever I move either of the dynamic bodies into each other, a collision is always detected the first time as well as a stop of collision. However, if I try to collide them once again, it often doesn't detect any collision after the first time. It does sometimes though. What can I do to fix this? Nothing else in my code touches any Box2D code.
Here is my contact listener header file:
class myContactListener : public b2ContactListener
{
private:
public:
// Contact listener methods
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
And the EnemyEntity Box2D method:
void EnemyEntity::createStarBox2DCollision(b2World *world)
{
enemyType = EnemyType::Star;
def.type = b2_dynamicBody;
def.position.Set(1000, 1000);
body = world->CreateBody(&def);
box.SetAsBox(rectShapeSize.x / 2, rectShapeSize.y / 2);
fixtureDef.shape = &box;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.3;
body->CreateFixture(&fixtureDef);
body->SetUserData(this);
}
It looks like you should have
void myContactListener::BeginContact(b2Contact* contact)
{
...
static_cast<EnemyEntity*>(bodyUserData)->startContact();
}
}
rather than
void myContactListener::BeginContact(b2Contact* contact)
{
...
static_cast<EnemyEntity*>(bodyUserData)->endContact();
}
}
for both bodies, rather than only for A

Moving player using keyboard listeners

I've got a player class, having MoveUp, MoveLeft and MoveRight functions.
At MainScene.cpp (my only scene so far), I've got a listener
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = CC_CALLBACK_2(MainScene::keyPressed, this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(MainScene::keyReleased, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this);
Also, I've got a pointer player, defined by
this->player = rootNode->getChildByName<Player*>("Player1");
Also, I've got a function keyPressed and keyReleased in MainScene.
In keyPressed function, I was scheduling functions with if's:
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_D) { schedule(schedule_selector(MainScene::MoveRight)); }
But there was a problem, when I tried to do the same thing with objects instead of sprites and with functions located in separated class, not within the same file.
If I try to run code like this in MainScene::keyPressed:
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_W) { this->player->MoveLeft(5); }
, player moves only once per key press (I want it to move till I release that key), and if I try to schedule it or do something like this, it doesn't work or there are errors.
I tried to make something with CallFunc and CCCallFunc, but nothing seems to work.
Could you please help me? :)
Why not create a method startMoving() and stopMoving() in Player class?
Something like this:
void Player::init(){
scheduleUpdate()
}
void Play::startMoving(){
isMoving = true;
}
void Player::stopMoving(){
isMoving = false;
}
void Player::update(float delta){
if(isMoving){
//move player here
sprite->setPositionX(sprite->getPositionX() + speed * delta);
}
}
and then call them from keyPressed/keyReleased?

replaceScene() messes up a public variable

I am porting a game from cocos2d-iphone 2.x to cocos2d-x 3.x.
Have to solve a few problems, including a major crash - the subject of this post.
It has been determined that the crash happens because SOMETIMES, my replaceScene call results in a messed-up important public variable.
My class:
class Player : public cocos2d::Sprite
{
public:
....
cocos2d::Vec2 desiredPosition;
....
My Layer methods:
Scene* GameLevelLayer::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = GameLevelLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool GameLevelLayer::init()
{
// super init first
if ( !Layer::init() )
{
return false;
}
....
player = (Player*) cocos2d::Sprite::create("sprite_idle_right#2x.png");
player->setPosition(Vec2(100, 50));
player->desiredPosition = player->getPosition();
....
this->schedule(schedule_selector(GameLevelLayer::update), 1.0/60.0);
....
return true;
}
void GameLevelLayer::endGame(bool won) {
....
MenuItem* display;
if (currentLevel < lastLevel && won) {
++currentLevel;
display = MenuItemImage::create("next.png" ,"next.png" ,"next.png",
CC_CALLBACK_1(GameLevelLayer::replaceSceneCallback, this));
} else {
// Lost the game
currentLevel = 1;
display = MenuItemImage::create("replay.png", "replay.png", "replay.png",
CC_CALLBACK_1(GameLevelLayer::replaceSceneCallback, this));
}
....
}
void GameLevelLayer::replaceSceneCallback(Ref* sender) {
Director::getInstance()->replaceScene(this->createScene());
}
The member being messed is the desiredPosition. It is changed inside update() method. The problem is that update() gets an already messed-up desired position. It is only messed-up after a scene was being replaced. The problem happens once in 10 runs, or so. It even appears that when update() is called first time after the scene has been replaced, desiredPosition set to some garbage. is I was unable to learn more.
My Player class does not have a separate constructor.
Please advise.
I forgot to initialize another instance variable. That instance variable is used to calculate the desiredPosition.

How can I get other layer in the same scene in cocos2d-x?

I have several layers in the scene, like TLayer,HLayer,TouchLayer. How can I get HLayer in HLayer? The solution I take is that I pass Layer to other Layer. However I met some problems recently. I push the Scene and pop Scene while TouchLayer still exists. So my problem is that is it right to pass HLayer to TouchLayer. Or is there a better way to do it in Cocos2d-x?
In the init() function in Scene:
this->setbackgroundLayer(BackgroundLayer::create());
CC_BREAK_IF(!backgroundLayer);
this->addChild(backgroundLayer);
this->setTLayer(TcharacterLayer::create(backgroundLayer->tianzige));
CC_BREAK_IF(!TLayer);
this->addChild(TLayer);
this->setHLayer(HcharacterLayer::create(testCharacter,backgroundLayer->tianzige_draw));
CC_BREAK_IF(!HLayer);
this->addChild(HLayer);
this->settouchLayer(TouchLayer::create(TLayer,HLayer));
CC_BREAK_IF(!touchLayer);
this->addChild(touchLayer);
I made my own create function:
TouchLayer* TouchLayer::create(TcharacterLayer* t,HcharacterLayer* h){
TouchLayer* pRet = new TouchLayer();
if (pRet && pRet->init(t,h))
{
pRet->autorelease();
return pRet;
}else
{
delete pRet;
pRet = NULL;
return NULL;
}
}
Here's how we've done this in the past. Define a set of tags for our layers.
typedef enum {
kBgLayerTag,
kHLayerTag,
kTLayerTag,
kTouchLayerTag
} MyLayerTags
Then while creating the layers set layer specific tags:
this->setTLayer(TcharacterLayer::create(backgroundLayer->tianzige));
CC_BREAK_IF(!TLayer);
TLayer->setTag(kTLayerTag);
this->addChild(TLayer);
And in TouchLayer access the TLayer and others like:
TcharacterLayer* myTLayer = this->getParent()->getChildByTag(kTLayerTag);
Sure, there is nothing wrong in adding a layer as a child. I would do that this way :
BackgroundLayer has all the necessary layers as children (remember to add TouchLayer as the last one and then pass Touch to others) and then you only add BackgroundLayer to your scene. There is even more simple way (probably better): to make BackgroundLayer inherit all the previous ones - but that depends on how much scenes you are going to make.
Edit:
1)
BackgroundLayer * bglayer = BackgroundLayer::create();
TcharacterLayer * tcharlayer = TcharacterLayer::create();
HcharacterLayer * hcharlayer = HcharacterLayer::create();
TouchLayer * tlayer = TouchLayer::create();
bglayer->addChild(tcharlayer);
bglayer->addChild(hcharlayer);
bglayer->addChild(tlayer);
this->addChild(bglayer);
(assuming this as a CCScene)
2)
class BackgroundLayer : public TcharacterLayer, HcharacterLayer, TouchLayer
{
...
}
In the first case you can give each layer a specific tag and then get the layer by getChildByTag(int tag) or just create fields in the BackgroundLayer class for each layer.