accessing b2Fixture - cocos2d-iphone

I have two classes for sprites and would like to access the b2Fixture of the sprites in the HelloWorldLayer.mm. What is the most appropriate way to access these properties? I plan to create a weld joint between them on contact. Please Help.

You need to create one or more fixtures on the bodies at the time you create them. Then you can use
b2Body *body = world->GetBodyList();
b2Fixture *fixture = body->GetFixtureList();
You might also want to know what type of body you're dealing with, for which you can use the body->GetUserData() method. Refer to the manual for more info. http://www.box2d.org/manual.html#_Toc258082972

Related

Unreal - How to avoid hardcoded path when creating an object in cpp

The following code has been bothering me for a bit:
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Engine/BasicShapes/Sphere"));
sphere->SetupAttachment(RootComponent);
if (SphereVisualAsset.Succeeded()) {
sphere->SetStaticMesh(SphereVisualAsset.Object);
}
}
Namely that I am hard coding a path. What if I decided I wanted to use a different object down the road? Imagine I had multiple pawns all hardcoded to use this asset. In cpp there is no easy way to change all these references. I had the bright idea to expose my mesh choice to blueprints to leverage Unreal's reference handling like so:
UPROPERTY(EditDefaultsOnly, Category = "References")
UStaticMesh * m_staticMesh;
However the following does not create a mesh when I inherit from this class with a blueprint and set the default mesh (m_staticMesh):
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
sphere->SetupAttachment(RootComponent);
if (m_staticMesh) {
sphere->SetStaticMesh(m_staticMesh);
}
}
I'm using Unreal 4.21.2 both methods compile, but the latter fails to work.
Any suggestions on how to make the above function properly are appreciated. If you know of a better way to avoid hardcoding paths please let me know.
Using Static Constructor Helpers is generally not recommended at all.
Composing an Actor of Components and utilizing Unreals Reflection system to expose those Components and their properties to the Editor to be modified in Blueprints is the ideal pattern.
/* Weapon Mesh. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Weapons")
UStaticMeshComponent* Mesh;
Marking a Component that is created during construction with VisibleAnywhere will cause it to appear in the Component Hierarchy within the Actors Component view after creating a child Blueprint of that Actor.
You will then have access to modifying its properties such as the StaticMesh.
It is useful to know what the valid Property Specifiers are when working with UPROPERTY as well as all the Function and Class Specifiers and their MetaData.
Property Specifiers

Is it possible to inject a component that doesn't have a prefab/GameObject associated with it?

I have a game object (a cube, let's say) which exists in the scene, and I want it to have an injectable component. I want to be able to say, for example: My cube has an IShotFirer member, which can resolve to either a BoomShotFirer or a BangShotFirer MonoBehavior component, both of which implement IShotFirer. When binding happens, I want this component to be added to the cube object.
public class CubeBehavior : MonoBehaviour
{
[Inject]
private IShotFirer shotFirer;
}
Is it possible to do this without 1) needing an existing prefab which contains one of these Bang/Boom components, or 2) needing an existing scene object which has one of these components attached?
In other words, I want to be able to dynamically add the component to my game object depending on the bindings, and not relying on anything other than the script files which define either BoomShotFirer or BangShotFirer. But the docs seem to imply that I need to find an existing game object or prefab (e.g. using .FromComponentsInChildren(), etc.)
Is it possible to do this without 1) needing an existing prefab which
contains one of these Bang/Boom components, or 2) needing an existing
scene object which has one of these components attached?
Yes, it is.
Zenject provides a host of helpers that create a new components and bind them -- quoting the docs:
FromNewComponentOnRoot - Instantiate the given component on the root of the current context. This is most often used with GameObjectContext.
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOnRoot();
FromNewComponentOn - Instantiate a new component of the given type on the given game object
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOn(someGameObject);
FromNewComponentOnNewGameObject - Create a new game object at the root of the scene and add the Foo MonoBehaviour to it
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOnNewGameObject();
For bindings like this one that create new game objects, there are also extra bind methods you can chain:
WithGameObjectName = The name to give the new Game Object associated with this binding.
UnderTransformGroup(string) = The name of the transform group to place the new game object under.
UnderTransform(Transform) = The actual transform to place the new game object under.
UnderTransform(Method) = A method to provide the transform to use.
That list is not even exhaustive, be sure to check the readme and the cheatsheet (from both of which I have extracted the info above).
Also understand that, as usual, you can append .AsSingle(), .AsTransient() and .AsCached() to achieve the desired result.

Creating a contact listener when sprites from different classes meet

I want to create a contact listener in such a way that I'll be able to create a joint when sprites from different classes meet. I found a useful question and answer which has helped me - Getting the world's contactListener in Box2D partly. The following code and insrtuctions where recommended:
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
//in BeginContact
thingsThatTouched.push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
//after the time step
for (int i = 0; i < thingsThatTouched.size(); i++) {
b2Fixture* fixtureA = thingsThatTouched[i].first;
b2Fixture* fixtureB = thingsThatTouched[i].second;
// ... do something clever ...
}
thingsThatTouched.clear(); //important!!
For this to work you'll need to make the thingsThatTouched list visible from the contact listener function, so it could either be a global variable, or you could set a pointer to it in the contact listener class, or maybe have a global function that returns a pointer to the list.
I am using Cocos2d and don't know much C++. How can I "make the thingsThatTouched list visible from the contact listener function" in Cocos2d? Should
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
be in the ContactListener.h file? How will it differ in Cocos2d? Thanks.
Put this in a header file:
typedef std::pair<b2Fixture*, b2Fixture*> fixturePair;
typedef std::vector<fixturePair> fixturePairVector;
extern fixturePairVector g_touchingFixtures;
Then include the header wherever you need to use the list. You will also need to have this in a source file (.mm or .cpp) somewhere, just once:
fixturePairVector g_touchingFixtures;
Of cause, the typedefs are not necessary but they may help if you don't like looking at too many of the wrong kind of brackets.
You could store this list in a singleton class, then you can access it from anywhere, even C++ code. Something like that:
NSArray* things = [SomeSingleton sharedSingleton].thingsThatTouched;

ID3DX10Mesh::CloneMesh

I'm trying to copy mesh in DirectX10. I wrote this:
HR(mesh->CloneMesh(mesh->GetFlags(),data.GetPosSemantic(),data.GetInputElementDesc(),
data.GetDescCount(),&mMesh));
but when i try to render the mesh nothing appers on the screen.
when i write
mMesh = mesh;
There are no problems with the rendering(unless when I release "mesh" ).
Thanks in advance.
If you want to duplicate the mesh without any changes, you should use the second approach and call then mMesh->AddRef() to declare the data is owned by two pointers (better idea is to use some kind of smart pointers - COM or boost::shared_ptr adapted to COM-like objects).
But the first case should work too - what is the data object about?

Draw a multiple lines set with VTK

Can somebody point me in the right direction of how to draw a multiple lines that seem connected? I found vtkLine and its SetPoint1 and SetPoint2 functions. Then I found vtkPolyLine, but there doesn't seem to be any add, insert or set function for this. Same for vtkPolyVertex.
Is there a basic function that allows me to just push some point at the end of its internal data and the simply render it? Or if there's no such function/object, what is the way to go here?
On a related topic: I don't like vtk too much. Is there a visualization toolkit, maybe with limited functionality, that is easier to use?
Thanks in advance
For drawing multiple lines, you should first create a vtkPoints class that contains all the points, and then add in connectivity info for the points you would like connected into lines through either vtkPolyData or vtkUnstructuredGrid (which is your vtkDataSet class; a vtkDataSet class contains vtkPoints as well as the connectivity information for these points). Once your vtkDataSet is constructued, you can take the normal route to render it (mapper->actor->renderer...)
For example:
vtkPoints *pts = vtkPoints::New();
pts->InsertNextPoint(1,1,1);
...
pts->InsertNextPoint(5,5,5);
vtkPolyData *polydata = vtkPolyData::New();
polydata->Allocate();
vtkIdType connectivity[2];
connectivity[0] = 0;
connectivity[1] = 3;
polydata->InsertNextCell(VTK_LINE,2,connectivity); //Connects the first and fourth point we inserted into a line
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput(polydata);
// And so on, need actor and renderer now
There are plenty of examples on the documentation site for all the classes
Here is vtkPoints : http://www.vtk.org/doc/release/5.4/html/a01250.html
If you click on the vtkPoints (Tests) link, you can see the tests associated with the class. It provides a bunch of different sample code.
Also, the vtk mailing list is probably going to be much more useful than stack overflow.