b2DistanceJointDef anchor point - ios

I just started doing Box2d programming. I'm trying to create a platform game. When the player (dynamic body) jump and landed on the moving platform (kinematic body), I need to make the player stay and move together with the platform. So i try using distance joint to "stick" the player to the platform. However, I've set the anchor point-Y so that the player will stay above the platform but I don't want to set a fix X. What approach should i use?
eg:
A.When player jump from right to left, it will land on left.
B. When player jump from left to right, it will land on right.
So when the player jump from any position, the X will be based on the player except Y is fix. Thank you.
b2DistanceJointDef jointDef;
b2Vec2 playerAnchor = curPlayerBody->GetWorldPoint(b2Vec2( 1, -0.8));
jointDef.Initialize( curPlayerBody, curPlatformBody, playerAnchor, curPlatformBody->GetWorldCenter() );
jointDef.length = 0;
jointDef.dampingRatio = 0;
jointDef.frequencyHz = 0;
jointDef.collideConnected = false;
playerPlatformJoint = (b2DistanceJoint*)world->CreateJoint(&jointDef);

I am assuming that you don't want to fix X because you want the player to move along the X axis on the platform.
I don't think the distance joint is a good solution for this.
One way to do what you want could be to set the friction of the player and platform fixture to a level that makes them "stick" together when the platform is moving. You should also have a look at the Friction Joint which let's you set friction in a more dynamic way. You can learn more about friction and the friction joint in the Fixture and Joint chapters of the Box2D manual.
Another way to do what you want would be to synchronize the movement of your player and the platform. But that depends a bit on how you move the platform and the player. If you are doing it by impulse for example, you could apply the same impulse that you apply to the platform to your player additionally to whatever you already apply to the player. You'll have to take properties like mass into account though.
Also take care on how you move the platform. If you are manually setting the position of the platform instead of using impulses, velocity or forces it will cause problems with the mentioned solutions.

I managed to find the answer. The idea is using weld joint and manually move the player.
Referenced from here

Related

Ideas on how to manage movement of SKNode related to other, moving down but maintaining the same alignment with the other SKNode

i'm new in the development of swift ios game using spirtekit and i need help. I want to develop a vertical 2d endless run, where with a swipe you are able to move the player in direction and if there is an obstacle, it is created in a position calculate with the position of the player. Once create the obstacle i would like to make it falling down but alway with the same relation with the player (this means that it has to move only on y axis).
Can you suggest me some ideas on how to manage this?
This book teaches you exactly how to make what you are looking for:
https://www.amazon.com/Getting-Started-SpriteKit-Jorge-Jordan-ebook/dp/B01891X7TM
This is the end product:
https://itunes.apple.com/lr/app/inside-the-hat/id1076967804?mt=8
You can get it for free here (with trial):
https://www.packtpub.com/application-development/getting-started-spritekit
The source code for this is around somewhere on the web, but I forgot where :( I bought the book a year ago and can't find it now.
Basically, you have a long background that is .moveTo .. in .update you check the position of the background, then reset it based on its XY position (it basically just goes all the way down, warps to the top position, then falls)
Also in .update you check for score, or a timer, or just a random check, then spawn an enemy, obstacle, etc. You give the enemy / obstacle SKActions to move towards the player, and let .didBegin(contact:) handle scoring, sound effects, death, etc.

Spritekit collision to platform while player is falling

I am making a spritekit platformer game with the help of spritekit objective c. I am using spritekit's physics engine for that, every thing is going well except that I haven't found a way to implement a plaformer style collision of the player with the platform.
What I want is my player should collide with the platform while he is falling and not while jumping. Like in following image. Here the player is jumping so he must not collide with the platform
and as In this image the player is falling so he must stand on the platform.
I tried to remove collision of the platform with player in didBeginContact method, but that didn't help as my platform is not dynamic type. Adding and removing collision does work with player but not with platform.
Any help will be greatly appreciated.
Edit: Here it is, example of what will happen if I change player's collision bitmask on contact with two adjacent platforms.
player will fall as soon as it will react with another platform.
Edit: aramusss's second solution is good, But it does create one more problem for me. As I have enemies in my game standing on platforms, if I remove platform's physics body the enemies will fall standing on it.
You can save the platforms in an array, and then check the player Y position. If player.position.y - (player.size.height/2) < platform.position.y - (platform.size.height/2) you deactivate collisions for this platform (means player is below the platform and we don't want it to collide). You should use:
// You should set collision bit mask to avoid collisioning between player and platforms, but not between other objects:
player.physicsBody.collisionBitMask = 1;
player.physicsBody.categoryBitMask = playerCategory; // An int constant, for example 101
player.physicsBody.contactTestBitMask = platformCategory; //Another constant, for example 102
platform.physicsBody.collisionBitMask = 1;
platform.physicsBody.categoryBitMask = platformCategory;
platform.physicsBody.contactTestBitMask = playerCategory;
Setting the values like this will make both bodies not collide, and changing them will make them collide again.
EDIT
Another solution would be to use a NSTimer that will call a method each 0,5 seconds (for example) and this method will check if there is a platform near the player. If there is and it is behind the player, it will create a physicsBody the same size as the platform. If there is a body created that is no more near to the player, or the player is below this platform, this function will delete it.

move one dynamic body relatively to other dynamic body

I faced with some problem which I can't find solution for. I have swinging platform (body1) and hero (body2) standing on it. Platform is swinging so hero moving right and left on the platform.
In the beginning I want hero to just stand on the platform without changing it's position relatively to platform. I achieved that by adding SKPhysicsJointFixed joining the platform and the hero.
Then in some moment in the game I want hero to run along the platform which means that Y should remain the same relatively to platform and X should grow. The only solution that I found is to make hero body static and change it's position in the didSimulatePhysics. The problem with that approach is that position not changing accurately. Hero position lags to one frame in comparison with platform position.
After hero reaches the right end of the platform he should stop moving and again just stand on the platform. So I make hero body static and add fixed joint again. And because hero position changes with lags hero position relative to platform is not correct. Sometimes it stops some pixels below the surface of the platform and sometimes some pixels above the surface.
So is there some other approach to move the hero along the platform, hero body should remain dynamic all the time.
I have a game with moving floating platforms. The simplest and most dynamic approach to achieve correct relative velocity is to simply make adjustments to the velocity. Here is a simplified version of what I do to achieve this.
In my game loop I have the following:
if heroOnBridge {
hero.extraMotion = floatingBridge.physicsBody!.velocity
} else {
hero.extraMotion = CGVector(dx:0,dy:0)
}
Here what I do is constantly look to see if my hero is on a moving bridge, if so then I set my hero's "extra motion" property to the velocity of the bridge that I am on. If I am not on a bridge then I don't add on any extra velocity. Note that I don't show how heroOnBridge is calculated because it really depends on how you define whether you are on a platform. In my case I assume you are on a platform if you intersect the platform frame and are on the ground.
Then in the area where I calculate my hero's velocity depending on his direction (left, right, jumping etc), I simply add on the extraMotion to the hero's velocity.
If you don't already I suggest you have an area in the game loop for processing the motion of you characters. It will allow you to make changes to the velocity of your hero depending on certain factors (in the air, jumping, moving left/right, on a platform, etc.)
If you need more help with calculating the velocity part let me know and I will post more of my code. You may also want to read my answer here for smoothly and dynamically setting the velocity.
Remember, for the most part you never want to set the position of anything directly, always try to set the necessary velocity to achieve that position over some period of time. This prevents very jittery and unpredictable motion.

SceneKit: PhysicsWorld relative to body

I have a ship that everything is centered around and everything moves relative to it. It is a first person shooter. Right now when I fire, and the ship speeds up, it catches up to the bullets. I would like the physics world to move relative to the ship's speed so that the bullet is essentially unaffected by ship speed.
The physics engine is doing the right thing, but not the right thing for my game.
I have other elements that move relative to the ship as it moves, which move correctly now, so don't want make ship stationary and move everything else in world around it. I don't see a direct way to do this maybe there is an indirect way? Perhaps I can manually take over the positioning of the bullets. I would like to use the other parts of the physics engine for doing collisions etc so don't want to completely manually do it, but will if that is the only option.
Anyone else have any suggestions?
It sounds like your bullets are receiving air friction. This is controlled by the physics bodies "damping" property. A value of 1.0 will make it static without movement. A value of 0 will allow it to move continuously without ever stoping. The default value is 0.1 as per the Apple documents. Assign your node like so to remove the damping(air friction)...
yourNode.physicsBody.damping = 0

Cocos2D Realistic Gravity?

I have tried many different techniques of applying a realistic looking gravity feature in my game but have had no luck so far. I plan on offering a 100 point bounty on this for someone who can show me or (share) some code that applies gravity to a CCSprite in Cocos2D.
What I have done so far has just been ugly or unrealistic and I have asked in many different places on what the best approach is but I just have not found any good looking gravity techniques yet.
Anyway, can anyone offer some tips/ideas or their approach only applying gravity to a CCSprite in Cocos2D without using a physics engine?
Thanks!
A effective approach without having to explicitly use any physics engine is to step the velocity and position of your sprite manually in your update loop. This is essentially Euler Integration.
// define your gravity value
#define GRAVITY -0.1
// define a velocity variable in the header of your Game class/CCSprite Subclass (neater)
float velocity_y;
-(void) update: (ccTime) dt
{
// Step the y-velocity by your acceleration value (gravity value in this case)
velocity_y += GRAVITY *dt; // drop the dt if you don't want to use it
// Step the position values and update the sprite position accordingly
sprite.position.y += velocity_y* dt; // same here
}
In the code snippet above, I defined a velocity_y variable to keep track of my sprite's current velocity along the y-direction. Remember to initialize this value to 0 in your init method.
Next comes the crux of Euler. At every time step:
Advance your velocity by your acceleration (which is your gravity) multiplied by dt to find your new velocity.
Advance your position by your newly computed velocity value multiplied by dt to find your new position.
You can experiment whether using delta-time or not (see LearnCocos2D's excellent comment on the cons of using it) works for your game. While multiplying by delta-time allows your object motion to more accurately take into account varying framerates, as LearnCocos2d pointed out, it might be wise to avoid using it in a real-time game where system interrupts (new mail, low battery, ads pop-out) can occur and subsequently cause the game simulation to jump forward in an attempt to make up.
So if you are dropping the dt, remember to tweak (scale down) your GRAVITY value accordingly to retain the right feel, since this value is no longer multiplied by the value of delta-time (which is ~ 1/60).
Aditional Tip: To apply an impulse to move the sprite (say via a swipe), you can affect the velocities directly by changing the values of velocity_x (if you have this) and velocity_y.
I have used this approach in my games, and it works very well. hope it helps.
This isn't trivial matter, you should try to see other posts. I'm sure other poeple already had this issue. Try to look at :
How to simulate a gravity for one sprite in cocos2d without a physics engine?
Cocos2D Gravity?
Or try our good friend google :
http://www.gamedev.net/page/resources/ -> got to Math and Physics and then A Verlet based approach for 2D game physics
http://www.rodedev.com/tutorials/gamephysics/
In any case here are some tips :
Step 1, calculate the effective direction vectors
Step 2, calculate velocity
Step 3, project this velocity onto the two effective directions.
Step 4, generate an equal and opposite force for the two direction and call this the “response force”.

Resources