Is it possible for b2Body which was created in one layer to have its properties (position, angle) to be modified in another layer?
Of course. All you need for modification is a pointer to the body. Just pass this pointer wherever you want and modify the body state from there
Related
Lets take for example a class that projects and unprojects between 2D and 3D.
class Projector
{
Point2D projectPoint(const Point3D& worldPoint) const;
Point3D unProjectPoint(const Point2D& screenPoint, const Plane& plane) const;
};
Both methods need to use a Camera object. What would be the preferred way to implement it:
A. pass Camera in Projector constructor
B. pass Camera in each of the class methods.
C. pass Camera in a setter method before using the class methods.
Option A breaks DI because Camera is a newable. Option B means that the same Camera instance is passed in every call. In interfaces with 5-6 methods, passing the same instance to each method will result in less prettier code. Option C will work, however it means that the user of this class need to know that he should set Camera - there is a risk that he will forget to do this and will not understand why the code does not work.
What is the preferred way to implement it?
I would pass the camera in the constructor and have the camera and projector(s) that use it being instances within that scope. It sounds like the scope is defined by the 'point of view'.
Perhaps the camera is the point of view and the projector is a singleton of the camera?
By constructor is definitely, to me, the best. It may be a question of how you get the projector and/or camera. Your inferring a domain constraint, so code the constraint. May be the constraint is that a 'Point of view' has one camera and .... projector(s).
I have two dynamic bodies with a Contact Listener and lets call the bodies A and B.
When body (A.tag == Car) collides with body (B.tag == Cat) I don't want the car to be pushed back, I want the cat to be pushed but not push back on the car (Causing the car to stop too!)
I feel like I'm missing something simple but can someone please explain how I can manipulate the b2Contact so that one object does not change is velocity but the other does?
Thanks!
You can use PreSolve and PostSolve callbacks.
In PreSolve save the car velocity, angular velocity, angle and position somewhere - for example, as instance variables on car's userData object. In the PostSolve restore those values back.
PreSolve called just before those bodies collide and PostSolve is called right after the collision - when velocities are changed. Doesn't tried it myself, but should work.
How can I make a b2Body move in a arc-like or curved path? I have heard about cocos2d Bezier curve function (ccBezier) but this is for moving cocos2d sprites. Or can it be modified to move box2d bodies? Any help is appreciated. Thank you.
Don't think that you can modify it to move physical bodies. In case of CCNode subclasses this function uses setPosition: method. If you change position of your body every tick with SetTransform method of b2Body object, it will ignore all possible collisions.
You can try to set linear velocity to physical body. In this case you need to change(rotate) velosity vector as you want.
So with Tiled, I can set Tile Properties directly on a tile before placing it on a map like so:
This is how I have done collision checking, by setting the collision property to 'true' and then checking the tile properties when moving a sprite.
However, I would like to add a 'teleport' tile. When the player walks on a specific tile, it takes them to a separate location.
The problem I am running into, is that when you set a property on a tile, you only get to set it once, and not on the tile instance. Meaning every tile would have the same teleport location.
Am I overlooking something? Is there a better way to go about doing this in Cocos2d in general?
You can use the object layer for this. Add an "object" (that's just a rectangle or point in Tiled) to the teleporter tile and use the object's properties to connect two locations together.
When you load the map you could walk over all objects to find the connecting objects. Then you know the two tile locations of the teleporter end points which you could store in a teleportation array. Every time your player moves to a new tile, check the teleportation array to see if the player is on one of the teleportation fields, and if he is, move him to the other teleportation tile.
Of course you could also check intersection with the object (rectangle) but since there's a chance that you might accidentally create an object (rectangle) that spans multiple tiles it seems more reliable to check these objects before the game starts.
Well this probably is the best way but it's what I've done. You could create a meta layer and have separate tiles for each teleporting pad. So when you check if the player is on teleportingpad1 you set the players location to receiverPad1 (which could be another tile, object in tiled, or just a point you set when you check for collisions). And you would just make another one e.g. teleportingpad2, teleportingpad3, etc. for more pads.
How to make some body move in specific trajectory?
I need some body to move up and down continuously ignoring gravity. Is there any way to do this with some body, or should i make it manually creating a new body in specific position in every iteration?
You must write the code for where it should be and just set its position before each simulation step. Also set its velocity if you want other objects that might bounce off it to behave correctly. Also give it infinite mass so it is a fixed object and won't be moved by things hitting it.
Set infinite mass like this when you create the body:
bodyDef.type = b2_staticBody;