How to fix an imageView to another and move both together? - iphone

I'm developing a game in which I have a bow and arrow. I have to move both bow and arrow imageViews on touch through different angles for which I'm applying CGAffineTransform.
The problem is this that after rotation, bow and arrow change their positions, so the arrow does not stay at the center of the bow. I want to fix this arrow at the center point of bow and move along with the bow.
I'm applying the same angles to both bow and arrow, but it seems that the bow is changing its center after transform. While detecting touch, I'm needed to provide the touch position to arrow throgh its center, so I even cant use the center property of arrow to fix it at a certain point.
Can anybody please help me? It's really urgent.

I would place both UIImageViews in a container UIView and rotate that instead. This will keep the bow and arrow together. I hope I'm understanding your requirements correctly. You can also look into the CALayer anchorPoint property which will affect which point the view is rotated around. Changing it will move the view though, so you do need to compensate for this.

Related

How to make a perfect pinch zoom (Unity 3D)

I am searching for a solution to how to make a perfect pinch zoom in Unity by moving the camera along the forward:
Set up:
Horizontal plane centred at the origin with all Game objects.
Perspective camera with FOV 10, offset at (10,10,10) looking down at a 45 degrees angle, so that it looks at the origin (there is also a rotation of 45 degrees around the axis pointing up, to achieve this).
What I need:
When I place two fingers on the screen I am touching two GameObjects with them - so the screen coordinates under the fingers correspond to certain world coordinates. When I make a pinch movement (with moving two fingers or only one) I want the new screen coordinates to correspond to the same world coordinates that were under the fingers at the beginning of the whole interaction.
So to simplify even further - whenever I touch the screen with two fingers, I want the world coordinates corresponding to the screen coordinates under my fingers to always stay under the fingers (allowing a very small margin of error).
An example of this perfect zoom for which I am looking for you can see in the mobile game Boom Beach from Supercell.
I already tried to move the camera along its forward vector and to reposition it and I get pretty good results, but pretty much always the GameObjects underneath ‘slip’ away from under my fingers, that is at some points are no longer underneath them. It would be great if there was a mathematical solution to this, but if it’s necessary to compute the answer (through some search for example) then this is totally fine.
If the setup/scenario is not clear, I could provide some sketches to clarify it a bit more.
Hope someone can help me! :)
I would set up a system that detects when the user is zooming in and out if you are using GameObjects to pinpoint where the fingers are that is easy to do with Vector3.distance. After that, I would make a function that moves the camera closer to your desired zoom level with Vector3.MoveTowards(camera position, desired position, the speed of movement) where I would set "speed of movement" as a mathf.sqrt(vector3.distance(Camera position, Desired position));
as for the "desired position" I would set that Vector3(position) as a fraction of a line between two game objects that represent your maximum and minimum zoom level.
EDIT: with that, you should have a very nice camera system

To follow a ball character like monkey kick off game

I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.

How do I detect which direction a UIImageview is animating?

What is the best way to detect the direction of a uiimageview?
I want to detect when a uiimageview is 'falling' - this is because it follows an arc animation for jumping purposes, i only want to detect when it is 'falling' and intersecting a floor, rather than when it jumps and intersects the floor.
I'm guessing there is no built in method to do this.
Thanks in advance!
The best thing to do, I found, was to draw a path and test it for collision with any above 'floors' or platforms. Then if it does collide, draw another path with an end point matching the y position of the collided platform.

Cocos2d - Top down camera view with rotation

I'm trying to create a top down car game where the camera follows both the player and the player's rotation. I can get CCFollow to work easily, but I have had no success with CCCamera. I assume that I need the camera in order to make rotation follow the player (i.e. have the player facing up at all times) but I have had no luck on google.
Can anyone either provide a code snippet or a tutorial on how to create a rotation-following top down camera?
Cheers!
My suggestion: don't use the CCCamera.
Your game design requires the car to move over a track. In programming terms this is often much easier accomplished by keeping the car static, and instead moving the background underneath.
Assume your car is at the center of the screen. It's supposed to move from left to right. Instead of moving the car or the camera, move the background layer - just in reverse: move the background layer from right to left to make it seem like the car is moving from left to right.
The same is true for rotation. If you want the car to turn left, rotate the background in clockwise direction.
This is a lot easier and can be accomplished simply by changing the position and direction properties of the background layer. Note that you do not need to do this for each object in the background layer, it's sufficient to add all objects to the background layer in the appropriate positions and then just change the background layer properties. The layer's children will follow accordingly.

Creating a Body - Cocos2d/Box2d

I have a ball and another sprite. When the ball collides with the sprite it simulates falling.
My only problem is the other sprite is just on big image and the ball is on top of it, but there are spaces on the sprite and a lot of corners. I need to determine if the sprite has touch one of the corners. I know this is hard to understand.
So, my question is, is it possible to make a body without
b2PolygonShape blockShape;
and
blockShapeDef.shape = &blockShape;
OR
is there an alternative I can use? I cannot set the image as a box and it would take way to long to set edges because there are so many corners.
I have already set up the collision detection.
I really need help with this.
Thanks!
If you want it to react properly, you have to make a polygon using every single corner coordinate.
But don't be lazy about it. You can use SpriteHelper for creating *b2PolygonShape*s out of your sprites.
Or another alternative: VertexHelper