Rope with pin joints and limits appear compressed, curling up - sprite-kit

I have a rope with pin joints between chain rings. It had worked perfectly.
I now want to simulate real chain, so I added limits for pin joints as such:
pinJoint.upperAngleLimit = CGFloat(M_PI / 6)
pinJoint.lowerAngleLimit = CGFloat(-M_PI / 6)
pinJoint.shouldEnableLimits = true
However, when I use a ball-like start of the chain, I get pictures 3-4 (shown below)
I tried to use constraints, but they weren't really helpful

Have you tried to use Matti's rope module? This worked pretty well for me. The simulation became very unstable based on the mass of the ball. Try reducing the mass of the rope links and the ball. Also, if the rope spins too fast the physics will also become unstable.
I think next time I will try using Verlet integration like this example. This also has the advantage of being able to attach on both ends.

Related

SpriteKit simulation speed / applyImpluse speed

I'm in the process of learning sprite kit and decided a catapult style game would be a good project to start with.
I am launching a projectile by using physicsBody?.applyImpulse(CGVector(dx: strength * dx, dy: strength * dy)) and this all works fine.
I'm currently trying to predict the trajectory of the projectile. I am doing this by applying the same impulse to a hidden SKNode and plotting the path it takes.
The problem is that the user has to wait until the impulse/physics has finished until they can see the full path. (see image below)
Path Image
(from the image above) I want the green path to either appear instantly or appear a lot faster.
I've tried to adjust the speed of the SKNode but it doesn't seem to have any effect on the simulation (it always runs at the same speed). I've also tried using SKAction.applyImpulse with the speed property being adjusted but it didn't have any effect either.
The only thing that has worked is setting physicsWorld.speed but I don't want to change the speed of the whole physics world, only the hidden trajectory node.
I was wondering if there was a way to run the physics simulation on a specific node instantly, or at least speed it up so it runs quicker?
Thanks in advance.
You can massively increase the gravity, and then factor this increase into the increase in the force used to project your object. This will result in the same arc of flight, but rising and falling much faster.

Unreal Engine 4 - Add offset to character movement

I just started (yesterday) using unreal engine and I need to simulate a drunk character using BPs.
I'm using two camera shakes (one for standing still and one for walking) but I want to add some "displacement" on charater when he's walking.
Basically I want to define a random float to be added to X axis location in order to make character wobble smoothly.
It will be acceptable even if there's a way to make the character move along with the camera when it's shaking.
What I tried until now is using AddActorLocalOffset and a timeline to lerp between actor's location and actor's location+offset, but both are very choppy to me.
Maybe it's a noob question but as I told I'm very new to this and need it for a quick work.
Any suggestion?
Thanks
If you are targetting physically correct model, you should use AddForce (UE Docs). But this approach would require implementation of a "drunk animation" where your character will modify it's movement animation to "compensate" this force by stepping aside etc.
Another (much more simple) approach is by using AddMovementInput. This example can be seen here: UE Aswers. In this case, you are basically simulate player's input by adding small amount of side force here and there.

How to make a sprite have gravity?

xcode 5 iOS7 sprite kit.
My wish is to make a sprite that has its own gravity.
Like a planet. If another sprite comes within a certain range of it, it will slowly pull the other sprite closer.
I have two sprites. One moving and one stationary. When the moving sprite gets in a given distance of the stationary sprite the stationary sprite gravity should slowly pull the other sprite towards it. This way the moving sprite would change its path in a soft curve.
My idea would be to calculate the distance from the stationary object to any other object and if close enough start pulling and if the moving object gets out of range ageing, then stop pulling.
Would probably need to research some vector calculation.
Thats my immediate thoughts.
Is this possible and how? Does it already exist?
A friend of mine did this for his physics dissertation. multibody gravity simulation. So yeah you can but you need to be willing to learn some maths. Apparently there is a clever optimisation to make it run decently nlog(n) rather than n^2). you probably want to ask this on the physics version of stack overflow to get a good answer. I have the code at home ... will post it later but you will want an explanation - i used it in an xna app. Its badass once you get it working - although if you want naturally orbiting objects then you will want to code them using parametric equations for easy and cool orbits. Simply because its hard to solve and with time even using double will result in some errors (the good implementations also work out the error and adjust - again will post later). But the real problem is solving for stable orbits. You then use the algorithm for free moving bodies such and player objects / npcs. Although solving accurate movement for npc in a changing field is v hard.
you want to look at this question: Jon Purdys answer is the one you want
multi body physics - gravity
and this one (which is linked from above) ...
https://gamedev.stackexchange.com/a/19404
There is not a dead-simple way of doing that in any platform as far as I know maybe except for some game engines/platforms that export for different platforms (cocos2d, construct 2 etc).
What you need is a physics engine whether you write one (which is a complicated but fun thing to do) or use an available one.
Luckily I've found a link describing the usage of the famous 2D physics engine "Box2D" on iOS.
This one explains how you can include Box2D in an openGL application (but you can apply this to other environments (sprite kit) I think altough I'm not an iOS guy);
http://www.iforce2d.net/b2dtut/setup-ios
Anyways, you now know what to look for...
For iOS 8.0+ : you have SKFieldNode : radialGravityField()
For iOS 8.0- : one solution could be : 3 to 9
add your sprite ( planet ) with its physics
add an invisible SKNode ( the gravity zone ) with its own physics, as a child of your sprite, but with a radius much more than your sprite's one
you have a little explanation about invisible node here : https://developer.apple.com/documentation/spritekit/skphysicsworld
both, your sprite and the invisible node are centered in a zero gravity world
we look for contact and not collision ( set correctly your bit masks )
when any object get in contact with the invisible node ( gravity zone ), first you remove any moving action or impulse from it and then we add to this object an SKAction to move it toward the center of your sprite ( planet )
at second contact between this object and your sprite ( planet ), you remove all actions again from the object with a velocity = zero

How to make Hinge Joint more rigid

I'm trying to create a chain in unity3d. A player should be capable of grabing one side of it and pull it to different location. So i created some grids and connect them together. It all works fine, the problem is only when user pulls a bit faster, then I got some spaces bettwen seperate grids. Is there anyway to set max distance on that?
Btw. I'm doing these in 2d so i have 2d Rigidbody and 2d Hinge joint.
Thanks!
The solverIterationCount of rigidbodies affects the smoothness of physics when they are moving fast. You should try increasing it or dynamically adjusting it according to the speed of the rigidbodies to increase stabilty
http://docs.unity3d.com/Documentation/ScriptReference/Physics-solverIterationCount.html
There a number of ways to tackle this issue, all with the up sides and down sides:
increasing accuracy:: this is usually the first place new game designers go, and crank up accuracy to the max. But then end up playing the price later when performance plummets. So be gentle, try to find a good balance, and if it does not cut it make up the rest with other tricks.
The main ways to increase accuracy are, increasing fixed times interval, and increasing ridged body Iteration count
.
Increasing restrictions::This often requires the most time, but cutting corners allows for smoother more predictable physics and can increase performance
small example:: the top link of the chain only needs to simulate rotation on the ridged body
.
Cheating:: find any way to make things easier, fake it
for example:: does the image really need to match the physics? Why not make sure the sprites stick together, but alow the physics to have small gaps
playing with the same concept more or less this week, experimented some with 2D Unity joints, I think the issue you are having is identical to one I had yesterday, the force you are applying 'breaks' joints for some frames, hence that almost 'spring' joint effect, make sure the mass and/or force applied are not too heavy, also maybe increase the chain parts mass, makes the joints more solid too.
In Editor, Hinge Joint -> Use Spring = true; Spring/Damper/Target Position = 0.

Rope breaks using Revolute joints

i am working on rope physics. i have implemented one rope using Revolute joints ..
I'm making rope connecting dynamic bodies (via b2RevoluteJoints).i successfully created it.Now i also attach one dynamic body at the end of rope . & i m releasing my this rope with this body downwards , now somehow my joints between my dynamic rope bodies are stretched & then breaks , I want my rope looks smoother.
my joints are like this
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = referenceBody;//provided by testbed
revoluteJointDef.bodyB = lastLink;
revoluteJointDef.localAnchorA = startPos;//world coords, because m_groundBody is at (0,0)
revoluteJointDef.localAnchorB.Set(0,0);//center of circle
world->CreateJoint( &revoluteJointDef );
revoluteJointDef.bodyA = lastLink;//the last added link of the chain
revoluteJointDef.bodyB = chainBase;
revoluteJointDef.localAnchorA.Set(0,linkWidth);//the regular position for chain link joints
revoluteJointDef.localAnchorB.Set(0,linkWidth);//a little in from the edge of the circle
world->CreateJoint( &revoluteJointDef );
Is there any way to make strong joints ??
can we achieve this by working on Density of bodies & gravity of world ??
please help .....
I had a similar problem (but with the centrifugal force of swinging a larger object at the end of the rope). To solve the problem of the breaking chain/rope you need to make the larger object less dense, but this then introduces a knock on problem - now your "larger" dynamic object gets held correctly by the rope but does not interact with the rest of the world as it should.
To solve this problem I did something a bit hacky and implemented my own collision handler which would apply a collision impulse to the thing that my larger object collided with as if it were heavier. So...
make your object at the end of the rope very low density so that the rope holds
implement some supplemental collision handlers for your object to make it seem like your object is more dense to other objects (sensor = true may help with this as well).
It is not strictly in the spirit of the physics simulator, as we should be able to set our parameters and let the rules take care of everything - basically it would be nice if the revolute joints were unbreakable.