Cannon js distance constraint not moving second body - cannon.js

I am experimenting with Cannon JS. I got two bodies created and placed in the world. Then I created a distance constraint with the default of these two body's distance as distance. I put applyLocalImpulse of the body one and was expecting the second one will move alone with the first one like a train. However, the first one moved on its own and the second body stay stationary. Am I getting the idea wrong about the constraint ?

Sorry. I forgot to put the constraint into the world. After that it worked

Related

Unity character flies easily

I have been using Unity for a while and still Im not sure about some things, I know there are like 5 different ways to move our character (not using prefabs like for 1st or 3rd person controller I mean just code from scratch) so far for moving a character on a terrain I think setting the speed of the rigidbody works overal for me, just setting the x and leaving the y component as what it was before assigning that, so that gravity effect is kept, overal it works, I manage to collide with wall and other objects, manage to go on terrain and could add jump later in same fashion, but the trouble I find is that my character flies when it steps over any small object like a stone on ground, if he goes over it he starts flying, not a big distance but definetly looks like walking on the moon maybe, also I see that if I have a bridge that is lower in the middle part of it (like U shape) well the character goes like flying from the begining almost until the middle or so, as said works like being on the moon, however gravity seems ok on other objects, I know I can change the gravity value to be higher but Im affraid I could be breaking all that so my question is, is there a better way to move a character on ground that will work better without changing the gravity value? I guess that it can work right without affecting this value, I used other method to change velocity and adding a certain value to the y component but again I see it not right as an apparent correct value for small stones and small objects makes it hard to go over slopes too, any help is greatly welcome =).

SpriteKit SKPhysicsJointFixed odd behaviour

I am trying to connect together two SKNodes which both have equal size circular physics bodies. They are positioned so as to be touching each other, and I want them to be locked together. From the documentation, it sounds like I want a SKPhysicsJointFixed, and I have tried creating one with the anchor point being the midpoint between the two nodes - like this:
let fixedJoint = SKPhysicsJointFixed.joint(withBodyA: atom1.physicsBody!, bodyB: atom2.physicsBody!, anchor:midPoint)
but this causes an odd behaviour where after the joint is made the top node falls through the bottom node - where before the joint existed the physicsbodies rested against each other.
If I use a pin joint instead with the same code - it works as expected ie:
let pinJoint = SKPhysicsJointPin.joint(withBodyA: atom1.physicsBody!, bodyB: atom2.physicsBody!, anchor:midPoint)
locks the bodies together as I want them to be. I guess this is a perfectly fine solution - but I'm confused something about what is going on. Why does my pin joint do what I thought the fixed joint would do, and why does the fixed joint not do what I thought it would?
Perhaps you were running into the same problem I ran into. I've found that SKPhysicsJointFixed behaves very strangely if the SKNode.zRotation property in the two nodes you're joining is different. Here's the behavior I was trying to get, having nodes of certain types stick together when they collide. Note how, after the collision, they rotate around their common center of mass.
But I was often getting this sort of thing, and often even stranger than this. Notice that not only does it not rotate as expected, it starts to wiggle as it approaches the wall.
As you can see, the difference between the two scenarios is that the zRotations are equal in the first case, unequal in the second case. Seems like a bug in SpriteKit, if you ask me.
One workaround is simply to explicitly set the two zRotations equal before you create the joint, but this was unacceptable for my purposes. Fortunately, as you already found, you can consistently get the expected behavior from SKPhysicsJointPin. Of course, if you want the pin joint to behave like a fixed joint, you'll have to set the joint's shouldEnableLimits property to true.

Velocity field in SpriteKit

Anyone got Velocity Field in SpriteKit working?
Granted, I'm not sure I understand how it's supposed to work, but I'm wiring it up inside the Xcode UI, not code. My intention is to have dynamic spritenodes be pulled to a direction if they enter the area of that velocity field. Seeing as the velocity field node does not have a size of its own, I just assume I have to add it as a child of another spritenode (calling that areaofvelocityfield) which is not physical, but has a size, so that other dynamic spritenodes can enter into it and get affected by the force. So selecting the Velocity Field node, I select areaofvelocityfield.
Now, whatever I do, any dynamic node is affected by that force, whether or not they are inside areaofvelocityfield or in any vicinity of it. Whatever distance they are from the node containing the velocity field, and the velocity field itself, they are always affected and pulled in the vector of the field.
iOS 9.2.1. Xcode 7.2.
So it seems that .region must be set to the SKFieldNode. This cannot be done from Xcode. At least it cannot be done like this, which is my current code which works well:
SKSpriteNode* belt = (SKSpriteNode*)[self childNodeWithName:#"belt"];
SKFieldNode* myfield = (SKFieldNode*)[self childNodeWithName:#"myfield"];
myfield.region = [[SKRegion alloc] initWithSize:CGSizeMake(belt.size.width * 0.5, 10)];
Note that I am trying to make something like a "conveyor belt". The belt sprite is just a rectangle. The myfield is positioned at the same position as the belt. All nodes are placed directly onto the world, rootnode, no parenting is needed. Placing a field inside another node doesn't really have any effect as to the region it affects (or it might have, not sure here). For some reason I had to set the force field's width to half of the visible belt, or it would keep affecting whatever dynamic node that fell down onto the belt for too long.
Other values I used for the field are: strength 1, falloff 10, min. radius 1. Not sure what really made a difference, as this cannot be easily tested in the Xcode interface. A lot of trial and error is needed to figure out how this all works.
Regarding the size of a region: as the docs states, the size protrudes from the origin in all directions which means the width and height will actually be twice what you set.

Scrolling Clone Sprites

I'm attempting to make a code in which I have a sprite act as the main terrain sprite (aka a tile) and have clones of that sprite stack on to the end of it, while maintaining the scroll code, which allows the x positions of the main sprite and the clones to change as the player pushes down on the "a" and "d" Key, While maintaining their proper positions in line. The issue I am having is that for some reason the third costume in my terrain doesn't seem to appear when its clone is created to act as the last tile in line.
I think the issue is that it's already created all the clones, but the first terrain block it clones off of spawns at the same time as the new ones.
By the way, Scrollnum determines the position in the line.
When your clone starts, it goes to the next costume, but since the base spirte's costume is always the first, the clones' will always be the second. You need to set the costume according to the clone ID. That variable (scrollnum) should be "for this sprite only", by the way.
I have had a similar problem, and it's possible that you are not using the right costume number. Try going one costume number down.
I completely forgot about this question but I did manage to figure it out in the and I thought I should post the answer considering that it may be of help to others.
Let me explain this code a bit, as shown in the image this uses a block instead of the repeat loop I attempted to use mainly due to the ability to use it more often as well as condense my code. The CloneX variable is refers to the tiles X positioning as a multiple in reference to the screen size. The equation when used looks like this: (CloneX * 480) + ScrollX. The TileX variable refers to the amount in which you want cloned.
This is how I ended up calling it. I ended up setting the costume to the one I needed for the level in order to start the generation of tiles. Then I initialized for the variables in the block]2

Unity 2D game Shooting Target Problems

so basically i have a target like the type for archery the 3 rings (bullseye, inner circle and outer circle)
now i basically used a cylinder to create these and then added to them rigidbody2D and a circleCollider 2D , now my problem is because the rings are essentially on top of each other i have them layered out on the z axis a little to make them all visible but when it comes to doing a raycast2d on the target it isnt picking correct ones up for example it goes from outer circle straight to bullseye and skips out inner circle yet all have colliders set up the same way
i cant figure out a way to overcome this and if not ill have to change to a different target where nothing overlaps in order to get it to work but i would really like the archery type targets
Thanks
You could just vary the distance from the camera for each ring so that the ones over the top are hit first.
Alternatively you could add tags to the three rings, use raycast all, and check the tags of all hit colliders to decide which one was hit first. For example, if all 3 register a hit, then you know the center was hit, and if the outer 2 register then you know it's the inner ring, and so on.
http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html