Box2d - mouse joint set to a specific point on an object - objective-c

I have a Box2d iphone app (using cocos2d) that has a mouse joint in it, and whenever you use the mouse joint, it applies the force to the object at whatever point you clicked. I was wondering if there is a way to always have the force be applied to a certain point on the object, for example the center of it. The reason is that when my shape is a box, for instance, when I pick it up using the mouse joint, it spins so that wherever I clicked ends up toward the top. I would like to always pick up the object from, say the center of the object. I've tried amping up the inertial dampening, but wasn't quite getting the effect I wanted. Here is my current code:
b2Vec2 locationWorld = b2Vec2(newtouch.x/PTM_RATIO, newtouch.y/PTM_RATIO);
b2MouseJointDef md;
md.bodyA = physicsLayer.groundBody;
md.bodyB = touchedObject.body;
md.target = locationWorld;
md.maxForce = 2000;
b2World* w = [physicsLayer getWorld];
mouseJoint = (b2MouseJoint*)w->CreateJoint(&md);
touchedObject is the object I'm moving and it stores it's body as a property. This code works fine as is, it just has the nasty spinning.

You could change the target point to be the center of mass of the box:
md.target = touchedObject.body->GetWorldCenter();
This will make the joint pull the center of the box to where your finger is, instead of the point you touched. It will not stop it from spinning if it's already spinning though, so you might still want to use some angular damping on the body to make it feel a bit more like the user is actually touching it, rather than holding a pin stuck through the center of it :)

Related

Unity2d; How to raise(lift) camera during game mode at a fixed y axis minimum

I may be making this harder than what it really is, but I am also pretty new to developing games. Currently I am making a practice scene to get back used to the unity engine as I have not had time to use it since last summer. My issue is that I can not figure out how to lift the camera in game mode. Notice my photo below, and how much of the "underground" is showing. I want to raise the camera to keep it at the very least a specific y axis value, so that I can make less of the ground visible, and more of the background visible. If I am over complicating this, please also let me know. Thank you
If main camera is still then just lift the camera in scene view you can see changes in game view.
Or if camera moves with respect to player then you have to use a script and attach it to camera and get a reference of player transform in the script and according to the player position change position of the camera. Add an offset value in y component of the camera.

Taking Control of ARCamera on Unity3D with Vuforia

The position and rotation of Vuforia's ARcam are determined by their algorithms to give the AR effect. I'd like to know how to do the following:
When some object is clicked the camera goes to some specific position (for that I need to somehow take control of the camera).
Also have to possibility to yield control of the camera back to Vuforia.
You'll want to use a second camera in this instance. Position the second camera at the AR Camera position, then adjust the depths to make the new camera the view that you see. Then you can tween its position to the predefined one you have.
Do the reverse to get control back to the AR Camera.

Spritekit Simulating fluid currents

Is there an easy way to simulate currents within a scene? I know they offer gravity, mass, density, drag and other attributes within spritekit, but what I am not seeing, is how would I simulate a current affecting objects on the scene?
IE say I have an object dropping using gravity and density, from the top of the screen to the bottom as though it was falling through watter, setting this up should be fairly straight forward. However, lets say I want to add an current flowing left to right through the middle of the scene... so as the object falls it begins to encounter the influence of the current and the fall begins to move with the current, and as it gets closer to the center of the current it increases its left/right movement and as it passes through the center of the current and moves away from its center the left/right movement slows down.
Is this possible? Do I need to create invisibe/transparent nodes moving left to right with gravity to simulate this? Or is there a different approach i should use?
This can be handled in your -update method. You can check whether a certain node is located within a certain region and apply a force on it, as if it were being affected by a current.
EDIT: I have posted a way you can differentiate the force based on the center of the current.
-(void)update:(CFTimeInterval)currentTime
{
for (SKNode *node in self.children)
{
if (node.position.y > 300 && node.position.y < 500) //Let's say the current is between these values, modify to your situation.
{
float diff = ABS (node.position.y - 400);//Difference from center of current.
CGVector force = CGVectorMake(2*(100 - diff), 0);//Variable force
[node.physicsBody applyForce:force];
}
}
}

Animating a marker pin on Bing Maps for Metro apps

I have a Bing Map control and I want to animate the location of a pin drawn on the map.
Ideally I'd want to write code like this:
// Image myPin = some Image on the map layer;
var sb = new Storyboard();
var duration = DurationHelper.FromTimeSpan(System.TimeSpan.FromMilliseconds(1000));
sb.Duration = duration;
LocationAnimation ani = new LocationAnimation();
ani.To = new Location(33.3, 11.1); // destination lat, lng
ani.Duration = duration;
sb.Children.Add(ani);
Storyboard.SetTarget(ani, myPin);
Storyboard.SetTargetProperty(ani, "MapLayer.Position");
Resources.Add("moveMyPin", sb);
sb.Begin();
Of course there is no LocationAnimation class in the framework. There are a couple similar: PointAnimation works with the Point type, while DoubleAnimation works with individual double values.
I assumed I have to implement my Timeline-derived class but the documentation I found so far is not helpful in this regard and I don't know where to start.
How to animate a property for which there is no *Animation class ready? Am I missing something?
Update: I think I can do something like
// let aniX and aniY be two DoubleAnimation objects
StoryBoard.SetTargetProperty(aniX, "(MapLayer.Position).Latitude")
StoryBoard.SetTargetProperty(aniY, "(MapLayer.Position).Longitude")
but now I have another problem as I don't see how to animate the attached property and always get an exception like "Cannot resolve TargetProperty (MapLayer.Position).Latitude on specified object."
So I guess the question now becomes: how to specify a property path for an attached property?
Further update: I may be on a wrong lead with the "SetTargetProperty" thing as that would make a dependent animation (see link in comments). Could there be a better way to animate a marker pin to a coordinate destination?
Update 3: as Jared pointed out I could try to animate RenderTransform, or use a transition to achieve my result. I had already gone this way and it doesn't seem to be my solution. This is what I found out:
Using RepositionThemeAnimation, it seems I have to give the new pixel position of the pin. This is problematic for a couple reasons: first, I know the lat/lng destination of the pin, but would have to work out the projection (as far as I can tell there is no public interface to work with projection). Secondly the projection itself can change as the map zooms/pans. The pixel destination at the time of the end of the animation could not be the same as the initial pixel destination. In my scenario it's a major problem as it's very likely the map is moving while the animation is occurring.
Using RepositionThemeTransition all the weird pixel/projection problems went away. It's the only way so far I was able to see the animation I expected. I just set the transition and set the new lat/lng position of the tag (via MapLayer.SetPosition). It has two major problems though. I can't set the duration (nor the easing functions) for the animation. And most importantly I can see no way to execute code when the pin gets to its final position.
Attached properties can be animated, but you must enclose their names in parenthesis. See an example here:
http://www.charlespetzold.com/blog/2007/09/080231.html
However, I believe this only works for setting the whole value (Location) and potentially not sub properties (Latitude and Longitude).
I'm wondering if you can provide more information about what you're trying to do. Is this a temporary animation? i.e. are you wanting the pin to animate in on entrance or departure? If so you can just do an animation on the RenderTransform X and Y instead. You won't actually be changing the location of the pin but you can use the RenderTransform to temporarily change where it's being drawn.
Even better, you might be able to get away with the free animations now built into the framework. You don't even need a storyboard to use them, you just set them right on the element like this:
<Button Content="Transitioning Button">
<Button.Transitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Button.Transitions>
</Button>
For more examples of the animation library and even video previews for many of the animations, see:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh452703.aspx
Okay, man, your problem isn't related to Bing Maps. It's related to dependency properties that can't be animated UNLESS you specify that they can. Here's how: http://blog.jerrynixon.com/2012/06/windows-8-animated-pie-slice.html
That link in the other answer with Charles' site is not up to date.

How to create the land (hills) like iOS game 'Contre Jour'?

How to create the land (hills) like iOS game Contre Jour? (Using Box2d and OpenGL)
My ideas:
Physics (Box2d)
I think we have array of bodies or fixture.
When we to touch screen, determine touch location.
If the touch location is not far from land, we begin to scan the array of bodies, and are looking for a body with coordinates closest to touch Location.
When case a touch Move, move the right body to a new coordinate (body->SetTransform(...)).
What do you think, efficient to use a large number of bodies? And find for the right body by coordinates?
Graphics (OpenGL)
There is an array of vertices and triangles created by drawing the land (hills)?
Is this true?
You can use the function b2World::QueryAABB to get a list of the fixtures in a given area, then check those for the best option. The Box2D testbed does this to find out which fixture to grab with the mouse so you could check out that source code. See also: http://www.iforce2d.net/b2dtut/world-querying
To move the body you can indeed use SetTransform, which would be good if the object does not need to interact with anything along the way. Another option might be to SetLinearVelocity to a velocity that will move the body to the dragged-to point in one time step. This is a better method if you want a continuous drag with the object being able to bump into things as it moves, because it does not teleport the body instantly to the finger position. If the body is a bullet body then it also prevents the user from dragging things through other objects, eg a static wall. Remember to set the velocity to zero when the finger is lifted :)