physicsBody rotation limitation - objective-c

I've got the following node set as follow.
box.physicsBody.allowsRotation = YES;
No mater what force applies to this box, I want it to never fall down or rotate more than a certain angle, just like a tree or a spring that might bend all the way down but has a tendency to go back to it's initial angle or position.
Is there a any trick or method to that?
I was thinking maybe joining an invisible un-rotatable physics body under the box and use a joint spring method for that which I have no Idea how that works!
Or, maybe there be a trick to play with the pivot. Any idea?

In the update: method you will have to continuously check the desired node's properties. In your case it is going to be the zRotation property of your node.
Add this code in your update: method:
if(yourNode.zRotation > 0.5)
yourNode.zRotation = 0.5;
if(yourNode.zRotation < -0.5)
yourNode.zRotation = -0.5;
Change the 0.5 value to your desired value or replace it with a static const float.

Related

How to make an SKNode continue in the same direction after finishing following a path

In my project I have an SKNode following a CGPathRef made with touchesMoved. What I want to have happen is once it reaches the end of the CGPathRef, it follows in the same direction that it ended in. I am using the SKAction followPath and tried to implement this by adding a line from the last point on the path (touchesEnded) all the way to the end of the scene using some previous points and a unit vector however this was not only complicated but also very inconsistent. Is there another method I can use to make it just continue on its path after it finishes the CGPath? Thanks!
I suggest storing your node's CGVector (dx, dy) which represents the direction of movement for your node. For example, if the last moving vector was 15,15 that translates to your node having moved up and to the right. You can use this value to continue moving. If the values are too low or too high, you can add a min/max filter.

axis orient / position with dimple.js

Is there a way to explicitly position/orient an axis using dimple.js? I know that the first x-axis is added to the bottom and the second is added to the top, so I can get the desired outcome like this:
var xAxis = myChart.addMeasureAxis("x", "dur");
xAxis.hidden = true;
xAxis = myChart.addMeasureAxis("x", "dur");
...but that seems a little hacked.
I don't think there's any way around that. Here's the relevant code : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L123
Because firstX is local to the draw function there isn't a way you could override it within the context of the loop.
You could possibly duplicate the logic used to position the top axis : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L226
And then only add one axis (which would be positioned at the bottom), then manually move your axis after the drawing, like :
chart.draw();
chart.axes[0].shapes.attr('transform', ...);
But you would need to do this after every draw or else they'll be repositioned. The hack you have seems easiest.
You could open an enhancement ticket for this though : https://github.com/PMSI-AlignAlytics/dimple/issues?q=is%3Aissue+label%3Aenhancement+is%3Aopen

Sprite-kit - Facing the hero? ( rotating to the direction of another node)

I have two spriteNodes hero and the enemy, both with rectangular physicsBody applied.
In the update, when the hero gets to the certain point, e.g hero.position.y <= 300 I want the enemy to rotate and face the the hero as it moves down.
the only sample code I found was the Adventure from Apple which has a faceTo class but I found it very complicated to use. I am looking for a nice and clean solution for it.
Thanks in advance.
The most basic implementation should look something like this:
- (void)rotateNode:(SKNode *)nodeA toFaceNode:(SKNode *)nodeB {
CGFloat angle = atan2f(nodeB.position.y - nodeA.position.y, nodeB.position.x - nodeA.position.x);
if (nodeA.zRotation < 0) {
nodeA.zRotation = nodeA.zRotation + M_PI * 2;
}
[nodeA runAction:[SKAction rotateToAngle:angle duration:0]];
}
It's important that you understand what is happening. Look up and read about atan2 (http://en.wikipedia.org/wiki/Atan2) and try to understand how the code above works.
You can use the xScale property of SKNode to make the sprite face the other way. This can be done by setting:
sprite.xScale = -1.0;
However, this has been proved to cause problems with the physicsBody, as can be seen from this question:
Flipped x-scale breaks collision handling (SpriteKit 7.1)
The solution for this problem is given there itself by JKallio.
He suggests keeping your sprite as a child of another SKNode (to which you apply the physicsBody) and changing the xScale property of the child node where needed. This is what I am using in my own project as well.

Dynamic resizing of the body (LibGDX)

I have a circle-shaped dynamic body and I need to resize it during the game (It appears like a point, then it grows to a circle and after that it starts moving). How should I do that?
I have an idea - it's to use some animation (Circle has the same radius, but due to animation it looks like the circle grows), but I'm not sure if it's right way or not. (Besides I don't know how to realize it)
For scaling circle, if you are using sprite just scale it sprite.setScale(float), if your sprite is attached to Box2d Circle-shape then get the Body's shape and set the radius
Shape shape = body.getFixture().getShape;
shape.setRadius(radiusValue);
and if you are using ShapeRenderer just multiply the points of ShapeRenderer.
I assume that you are talking about a Box2D body.
It is not possible to change a circle-shaped fixture with Box2D. Box2D is a rigid body simulator. What you would have to do is destroy the fixture and replace it with a smaller/bigger version of the circle. But this will cause a lot of problems, since you cannot destroy a fixture when there is still a contact for example.
It would be better to keep the circle the same size and just simulate a change in size with an animation of a texture on top.
If you cannot simulate that, then maybe try the following approach: Have several versions of that circle in different sizes and keep them on top of each other. Implement a ContactFilter which will only cause contacts for the one circle which is currently "active".
Inside any Object class with box2d, I use the following for dynamic resizing:
public void resize(float newradius) {
this.body.destroyFixture(this.fixture);
fixtureDef.density = (float) (this.mass/(Math.PI*newradius*newradius));
this.radius = newradius;
CircleShape circle = new CircleShape();
circle.setRadius(newradius);
this.fixtureDef.shape = circle;
circle.dispose();
this.fixture = body.createFixture(fixtureDef);
this.fixture.setUserData(this);
}
You can also see the following topic: How to change size after it has been created

Two NSTextFields with interdependent widths in autolayout

I’m trying to put together what seems to be a simple case of two NSTextFields with dynamic width and fixed spacing in between. I cannot figure out an effective way to do so though.
I’m looking to get something like this:
The blue boxes are the NSTextFields. When more text is entered into one, it should grow and thus make the other one shrink, maintaining the lead space, trailing space and the spacing in between the fields. The first one should take the priority if both of the fields have too much text. Each field will also clearly have a maximum and a minimum possible width it can reach.
How would I go around handling this, preferably utilising IB autolayout as much as possible?
It seems to me that all of constraints you mentioned directly translate into interface builder --
First view has width >= something.
First view has width <= something
Same for Second view.
Space between views is fixed.
Second view wants to be as small as possible (have its width at 0) but this has lower lower priority than the previous constraints and lower priority than inner content size constraints.
The code I had to add to my view controller, after applying the constraints as per the ilya’s answer:
In controlTextDidChange (_controlWidthConstraint refers to the fixed width constraint of the input; it’s probably 0 by default for the second input):
// Get the new width that fits
float oldWidth = textControl.frame.size.width;
[input sizeToFit];
float controlWidth = textControl.frame.size.width;
// Don’t let the sizeToFit method modify the frame though
NSRect controlRect = textControl.frame;
controlRect.size.width = oldWidth;
textControl.frame = controlRect;
_controlWidthConstraint.constant = controlWidth;
The key lies in invalidating the intrinsicContentSize for the text field when text is input.
You can check a sample project here, to get you on the right track.