How to make a moving object "stick" to a stationary object in box2D - cocos2d-iphone

I have been experimenting with the box2D sample project within cocos2D for the iPhone and am wondering if box2D is the appropriate engine to use to make a moving object "stick" to a stationary object when the moving object is finished moving in a certain direction.
Here is a simplification of what I am trying to achieve: I have MovingObject, a dynamic rigid body, that moves vertically against gravity when enough force is applied to it. As MovingObject moves, it may overlap a static object, StationaryObject. When gravity diminishes MovingObject's velocity to zero such that it is no longer moving, I would like to have MovingObject remain where it is ONLY if it overlaps StationaryObject. If the object's do not overlap, MovingObject should start to move back down towards the ground per the force of gravity. During that descent, if MovingObject at any time overlaps StationaryObject, it should stop its descent and remain in that location as if it is stuck on StationaryObject.
I am able to get MovingObject to move per the forces I am applying to it, but not really sure how to make it stop and stay there once it reaches the top of its ascent, assuming it is overlapping StationaryObject.
Currently, I am experimenting with simple square/box objects but eventually both MovingObject StationaryObject will be defined as very different complex polygon shapes.
Thanks in advance for any insights and/or suggestions for achieving this.

Sounds like you'll want to change the type of fixture used for "MovingObject" while it "ascending" and then change it when it is "descending" so that it reacts differently (to overlaps).
by "overlap" it sounds like you want to achieve something similar to "one sided platforms" in a platform game (ie; Mario Bros.) - I would recommend looking into the solutions for one-sided platforms for starters.

Related

Unreal Engine 4 - Any suggestions on how I can I prevent my actor from "flying" by standing on top of an object while lifting it?

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.

Game maker studio Visual bug

I have an issue where when I use the physics system to have collision between the character and the wall, the Sprite will SOMETIMES vibrate when you hold to move into the wall. As an additional thing as they may be related, if I turn up the player's speed value they're able to glitch through the walls. Right now the collision system I have is really basic, so there isn't much coding but here's related info.
Wall's density is set to 0 while player is .1
Room has physics enabled but has no set gravity
I have a drag and drop collision event with only a comment in it.
https://docs.yoyogames.com/source/dadiospice/001_advanced%20use/more%20about%20objects/physics.html
Restitution: In physics, restitution is defined as "the return of an object or system to its original state after elastic deformation", but as the fixtures in the GameMaker: Studio are really rigid bodies and cannot be deformed, restitution is really a way of saying how "bouncy" the fixture is. This setting will affect how much an object "bounces" when it collides with other objects and is co-dependant on other forces that act on the instance like gravity and friction.
That could be ansver to "vibrations". Else you may tru to create code which will check if object is trying to move towards obstacle and stop its movement to prevent built-in physics from causing any problems.
wall skipping: This is usual behavior of built-in game-maker collisions. main problem is that game-maker "teleports" objects "by speed in its direction", which means that when speed is greater than size of any object, game-maker collision system may fail.

Simulating black hole / whirpool behaviour for sprites

One of the powerups in my game is a vortex that attracts all coins. I know I have any cocos2d's moveto/bezierto methods available, but I don't know how to make them have tangential and radial speed.
The extra difficulty is that the vortex center can change in every step, so all movement has to be readjusted.
One way to achieve this without a physics engine is to use the rotation around point algorithm.
That covers the rotation around the vortex center. Once an object is rotation around the vortex, all you need to do is to reduce that object's distance from the center by a certain amount every frame. That way it will continue to move inwards.
The only tricky part then is to get the object from its initial position being "sucked into" the vortex. There's going to be a lot of tweaking needed. With a physics engine, that part would come natural from the physics itself and it would always look right.
This is not guaranteed for the manual solution and definitely not for actions, which aren't designed to track moving targets. For example, if you change a move action every frame by replacing the existing one with a new one, your object won't move at all. Every time you do that, there's a 1-frame delay before the new action does its work.

Prevent collisions applying impulse in Bullet physics

I am developing third-person shooting game using Bullet and Ogre. When the character model collides with an object in the world, such as a power-up, the collision applies a force to the character and causes them to spin. How can I prevent the collision applying a force to the character?
I have set a method for btDynamicsWorld::setInternalTickCallback and so I know which bodies are colliding and the btManifoldPoint.
Note that I applyTorque to the body in order to rotate them smoothly so I cannot simply prevent rotation.
Thanks for your help.
I am unfamiliar with the physics engines you mentioned, but I know a thing or two about real physics...
Basically if you draw a free body diagram and arrows that represent the forces, you can determine the net effect. Or if you know the desired net effect, you can figure out where you need to add forces or remove forces.
You could add an equal and opposite force/torque at the time of impact. This would make the net forces on your object zero.
Or you could take the elements that are causing the forces and make them massless. Force = mass * acceleration. If the mass is zero and your physics engine is based on real world physics, then it shouldn't cause any net forces on collision.
Hope that helps.

box2d ship-game beginner questions: applyForce, gravity + water

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