I'm new with cocos2d-x. I was trying to use cocos2d-x 3.2 to build a simple physics game, but I got a problem.
At the beginning, I followed the tutorial and added these in HelloWorld.h:
private:
PhysicsWorld* m_world;
public:
void setPhyWorld(PhysicsWorld* world){ m_world = world; }
Then, I added these in HelloWorld.cpp:
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
auto layer = HelloWorld::create();
layer->setPhyWorld(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
Then, I tried to get the gravity value in function init() like this:
bool HelloWorld::init()
{
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}
Then, I ran it, but the program stopped at "Vect g=m_world->getGravity();"
It said that "0x00C53B44 had an Unhandled Exceptions", and I had to interrupt it.
Did anybody have the same problem before? I really appreciate if anyone can help me.
Please observe your code
auto layer = HelloWorld::create(); //Calling init Method of Hello World Layer
layer->setPhyWorld(scene->getPhysicsWorld()); // Setting PhysicsWorld instance to the layer
the init() method is called first and then you are setting setPhyWorld(scene->getPhysicsWorld()) so m_world = null.
If you really want physicsWorld instance in the init() method, you should customize the create & init method of HelloWorld layer and send physicsWorld instance with create() method.
//This code above header class of HelloWorld
#define CUSTOM_CREATE_FUNC(__TYPE__) \
static __TYPE__* create(PhysicsWorld* world) \
{ \
__TYPE__ *pRet = new __TYPE__(); \
if (pRet && pRet->init(world)) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
and then
CUSTOM_CREATE_FUNC(HelloWorld); // in the header class, instead of CREATE_FUNC(HelloWorld)
virtual bool init(PhysicsWorld* world);
and
auto layer = HelloWorld::create(scene->getPhysicsWorld()); // in the createScene() Method
and finally
bool HelloWorld::init(PhysicsWorld* world)
{
m_world = world;
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}
Related
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);
}
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
I wanted to be able to access my underlaying data structure when I pick a vtkActor. A class derived from vtkActor holding a ptr to my data structure seemed the easiest approach.
I get the subclass to compile just fine but the actor does not seem to be added to the renderer.
So, here's my class:
//.h
#include <vtkActor.h>
#include <vtkObjectFactory.h>
class Node;
struct Actor : public vtkActor {
static Actor* New();
vtkTypeMacro(Actor, vtkActor)
Node* holding_node;
};
//.cpp
#include "actor.h"
vtkStandardNewMacro(Actor)
In my rendering step: if I instantiate the actor with a vtkActor everything shows up as expected, picking works, etc...
vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
But no actor is added if I use my Actor class
vtkSmartPointer<Actor> sphereActor = vtkSmartPointer<Actor>::New();
Nothing else changes in the code. Any ideas of what's wrong?
So, it turns out that there are a bunch of functions that need to be overloaded and a couple touches of macro magic to get this to work.
I pasted below the example that's working for me now. It is mostly from the vtkFollower code (a derived class from vtkActor). Hope this helps!
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkObjectFactory.h>
#include <vtkRenderingCoreModule.h>
#include <vtkProperty.h>
class Node;
class VTKRENDERINGCORE_EXPORT NodeActor : public vtkActor {
public:
vtkTypeMacro(NodeActor, vtkActor);
static NodeActor *New();
virtual void ReleaseGraphicsResources(vtkWindow *window) {
this->Device->ReleaseGraphicsResources(window);
this->Superclass::ReleaseGraphicsResources(window);
}
virtual int RenderOpaqueGeometry(vtkViewport *viewport){
if ( ! this->Mapper ) {
return 0;
}
if (!this->Property) {
this->GetProperty();
}
if (this->GetIsOpaque()) {
vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
this->Render(ren);
return 1;
}
return 0;
}
virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport){
if ( ! this->Mapper ) {
return 0;
}
if (!this->Property) {
this->GetProperty();
}
if (!this->GetIsOpaque()) {
vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
this->Render(ren);
return 1;
}
return 0;
}
virtual void Render(vtkRenderer *ren){
this->Property->Render(this, ren);
this->Device->SetProperty (this->Property);
this->Property->Render(this, ren);
if (this->BackfaceProperty) {
this->BackfaceProperty->BackfaceRender(this, ren);
this->Device->SetBackfaceProperty(this->BackfaceProperty);
}
if (this->Texture) {
this->Texture->Render(ren);
}
this->ComputeMatrix();
this->Device->SetUserMatrix(this->Matrix);
this->Device->Render(ren,this->Mapper);
}
void ShallowCopy(vtkProp *prop) {
NodeActor *f = NodeActor::SafeDownCast(prop);
this->vtkActor::ShallowCopy(prop);
}
//****************************************//
// my member
//****************************************//
Node* node_i_represent{nullptr};
protected:
vtkActor* Device;
NodeActor() {
this -> Device = vtkActor::New();
}
~NodeActor() {
this -> Device -> Delete();
}
private:
};
vtkStandardNewMacro(NodeActor)
I have two classes:
/*Switch.h*/
class CSwitch : public CDeviceEntity {}
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity {}
but when I use:
CDeviceEntity* dev = NULL;
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL ) {}
"DYNAMIC_DOWNCAST" always returns not NULL while dev is kind of class CEndSystem or class CSwitch.
If use:
/*Switch.h*/
class CSwitch : public CDeviceEntity { DECLARE_DYNAMIC(CSwitch) }
and
/*Switch.cpp*/
IMPLEMENT_DYNAMIC(CSwitch, CDeviceEntity)
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity { DECLARE_DYNAMIC(CEndSystem) }
and
/*EndSystem.cpp*/
IMPLEMENT_DYNAMIC(CEndSystem, CDeviceEntity)
"DYNAMIC_DOWNCAST" returns NULL or not NULL according to class CEndSystem or class CSwitch.
Why "DECLARE_DYNAMIC" and "IMPLEMENT_DYNAMIC" are nessary for "DYNAMIC_DOWNCAST"?
/*Algorithm.h*/
static int getESNum();
/*Algorithm.cpp*/
int CAlgorithm::getESNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL )
{
count++;
}
}
return count;
}
/*Algorithm.h*/
static int getSWNum();
/*Algorithm.cpp*/
int CAlgorithm::getSWNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST(CSwitch, dev) != NULL )
{
count++;
}
}
return count;
}
And the functions are called in serialization when saving the document.
DYNAMIC_DOWNCAST is a throw back to how you used to have to do dynamic casting before RTTI information was available from the compiler. The casting information is created using the macros DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC which use the class CRuntimeClass to decide if the cast is valid.
DYNAMIC_DOWNCAST simply does this:
CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject)
{
if (pObject != NULL && pObject->IsKindOf(pClass))
return pObject;
else
return NULL;
}
The DECLARE_DYNAMIC macro adds this code:
#define DECLARE_DYNAMIC(class_name) \
protected: \
static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
static const CRuntimeClass class##class_name; \
static CRuntimeClass* PASCAL GetThisClass(); \
virtual CRuntimeClass* GetRuntimeClass() const; \
Add IMPLEMENT_DYNAMIC adds this code:
#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew, class_init) \
CRuntimeClass* PASCAL class_name::_GetBaseClass() \
{ return RUNTIME_CLASS(base_class_name); } \
AFX_COMDAT const CRuntimeClass class_name::class##class_name = { \
#class_name, sizeof(class class_name), wSchema, pfnNew, \
&class_name::_GetBaseClass, NULL, class_init }; \
CRuntimeClass* PASCAL class_name::GetThisClass() \
{ return _RUNTIME_CLASS(class_name); } \
CRuntimeClass* class_name::GetRuntimeClass() const \
{ return _RUNTIME_CLASS(class_name); }
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL, NULL)
I imagine few people still use this for new projects, instead preferring the C++ standard dynamic_cast<> call (along with static_cast and reinterpret_cast).
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--------
}
}