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.
Related
I'm developing a Dungeon Crawler style game and set a bunch of position objects as a "grid" for the player walk and stored them in a parent object, like a list. So I wanted to use that same list for the enemy moviment but after watching many A* Pathfinding tutorials for weeks I didn't managed to have any ideas of how to solve it since I'm not using a regular 2D grid. Here's a reference.
I took a look at your image, and you do in-fact have a grid. Just because you placed your objects does not mean you can calculate the "grid" for them from the list.
I would suggest you create a function that you call when your level starts.. and it iterates through the children of that object and generates a data structure that allows you to use A* to calculate pathing. You can use the transform.position of each object to determine it's relative placement/adjacency.
There is many ways you can do this. One suggestion would be creating an invisible collider with a trigger (can be on an empty GameObject) in different areas on your grid. Give the enemy a default velocity and attach a collider to the enemy and select the checkbox for trigger so when the enemy moves onto the trigger, you can call OnTriggerEnter (from a script attached to the enemy or the invisible collider GameObject) and make it change its movement. How you want to implement the AI is up to you.
edit as per comment
put invisible game objects positioned in your scene to how you would envision them being in an array from a birds eye view. In a script assign them to an array. Get the enemy to lerp between points in the array based on your ai algorithm. https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
I have a code that changes the position of an object when grabbed to 100 units in front of the camera, this allows the player to stand on top of an object and pick it up causing him to fly towards wherever the camera is pointing.
Example: In this picture, I stood on top of a movable red rock. While on top I picked it up and moved my camera upwards which caused the rock to move to that position while carrying the actor. Because of this, I can quickly move to anywhere on the map by standing on top of an object and picking it up.
The skeletal mesh is unrelated to the grab function and the range of the grab is set to keep movable objects close to the skeletal mesh (like lifting this pebble) if that helps.
Any suggestions on how to fix this issue? Thanks in advance!
Assuming that your objects have some sort of toggle-able state that determines whether they can be picked up or not. One solution to the problem you are facing would be to cast a ray from the bottom of the player and if that ray hits an object that can be picked up, temporarily disable the ability to pick the object up. This introduces a new problem of stacking up two objects and moving the bottom object however. Alternatively you could also change objects so that when you are holding them, the collider is disabled on the object. There are many more ways you could go about solving this issue, but without knowing in detail what you are trying to accomplish by holding objects, it would be hard for anyone but yourself to choose the appropriate solution.
I am trying to make an object get attached on a collision point to a circle that is rotating, but the player needs to get attached with a constant point on the player. For example the player is moving back and forth and when the user touches the screen and the player jumps up but what I need is that when the player collides with the circle it attaches it's legs to it and continues rotating with the circle. So I wanted to know how to make this kind of collision joint in cocos2d box2d?
When the collision between the user and the circle is detected, you could run an action that calls a callback function (perhaps a CCCallFuncND). The callback function could be passed pointers to both bodies (wrapped in NSValue valueWithPointer) and create an arbitrary joint between them. This would be done by first instantiating a b2JointDef of the desired type, defining bodyA and bodyB for that joint as the two bodies you'd like "stuck together", and then calling b2World->CreateJoint().
In terms of attaching the player body to a specific point on the circle body, you'd have to do so via some parameter of the joint def like anchorPoint. For example b2PrismaticJoints have anchorPoint which defines the center of the joint's range of motion.
I have played a little with the particle system in cocos2d-iphone and ParticleDesigner, and I use the CCParticleBatchNode to get good performance as I have many of the same emitter. This works great for explosions, but now I would like to have a exhaust smoke on my ships, but here I am stuck.
I can not add the CCParticleBatchNode to my CCSprite's as they can only be added once and it is reused for all exhausts, but how do I then make my CCParticleBatchNode follow or stick to my CCSprite?
Also the exhaust particle system I have made with ParticleDesigner has a gravity/direction, but when my CCSprite's always are moving then is this wrong, should they just emit particles in one place and the movement of the emitter will create the trail, or?
How is this done guys?
of course you cannot add the ParticleBatchNode to the sprite (the same way you cannot add a particle if the sprite were used in a SpriteBatchNode)
to solve similar problems i use to inherit a class from ccsprite, say MySprite, and then override the position setter
-(void) setPosition:(CGPoint) position {
myParticleEmitter.position = position; //+ offsets if needed
[super setPosition:position];
}
You need a pointer to the emitter, i usually keep a weak reference to the emitter from MySprite, and automatically every time i set and change a position the emitter follows.
I didnt quite get the second part of your question, but i think you can just update the gravity direction of the emitter when your sprites moves / rotate, overriding also setRotation if needed
I'm just starting with cocos2d + box2d.
I would love to create a simple 2d ship game where you look from top(from the sky) down at the sea with the ships.
Could someone give me a very very basic example, how to apply wind to my world? Do I have to applyForce to each ship body?
And what about the gravity? Since it goes straight down, should I set it to 0? gravity.Set(0.0f, 0.0f)? It actually would pull down the ship..
Should I somehow deal with the water? e.g with density of it?
you would probably have to detect where the ships are and then apply force on them somehow.
and yes set gravity to 0.
and water wouldn't have to be an object at all. just like a background image.
If you are looking from the top and want to apply wind force to all your ships you can use gravity vector. In this case wind force will be applied to your objects automatically.
If there are other dynamic objects on the scene then it's a good idea to keep the list (std::list or NSMutableArray) of pointers to all your ships. Notice, that by default box2D is clearing all the forces each simulation step. You can disable this property or apply forces manually each simulation step