How to get midpoint value in OpenGLES in iPhone? - iphone

I am new to game development. Currently I am working in simple game application, using GL_Line to draw a line and GL_Triangle strip to draw a (ball)image. Normally ball images are moving from left to right direction. When the user tab on a screen, then the line draw (top to bottom) inside the screen and to touch a ball image. So I tried to get the midpoint values from line and ball image, but i cannot know, how to start this?

If you have a line that goes from (ax,ay) to (bx, by), the mid-point of the line is just ((ax + bx) / 2.0, (ay + by) / 2.0)).

Related

How to align tilemap, grid and sprites to center? (Unity 2D Tilemap)

I am making a game similar to Rimworld and I have issues aligning the tilemap, grid and sprites so that they all work as intended functionally and visually.
The default offset for the tile anchor is 0.5, 0.5, 0 and this works entirely until I drop a sprite in the center of the tile, where it appears to be positioned bottom-left (with the sprite's pivot set to Center).
This is a problem because I'd have to manually set an offset (0.5, 0.5, 0) for every single object I instantiate to match the visuals of the tilemap. I tried setting the tile anchor to 0 but then the tilemap moves while the grid position stays the same which messes up click detection. (if only I could move the grid back by (-0.5, -0.5, 0)?
Is there an elegant and easy solution for all of this?
To get a perfect custom alignment for a tilemap or sprite the easiest solution is to take a empty gameobject as spawner , take the position of that empty spawner object and and then instantiate those tile or other prefab at the exact location where you kept those spawner objects by your choice.
something a friend of mine told me, is that instead of trying to align it to the grid so it always moves in the center, just hold ctrl while using the move tool on the sprite that you are moving. It can get annoying, but it works :)
basicly if you change the sprite pivot for top-left or something different of center should work, for me works perfectly.
I have the same problem with my 2d game. when I want to put the tiles, they place with some offset.
You can change the pivot value in the sprite editor for any sprite ( tiles, character, ...) to a custom pivot like (0,0,0), I suggest center value for the pivot.
You can change Tile Ancher for any Tilemap objects too.
this helps you to put anything to map how you are comfortable.

How to find the center in unity 2d?

I want to create 2d game. The game is similar to the Scale Puzzle. I've already created nearly all the functionality. Only the last remains.
.
This is example.
And that's how I draw shapes.
.
I click inside a white square, and after 1 seconds a square is drawn regardless of the position of the ball. (x,y).
Square is created programmatically, and it is added to the parent element "SquaresList" with name New Game Object.
How can i do, so that the violet field becomes larger, and in the middle of the screen.
I made it so that for every 3 clicks, "SquaresList" increases Scale by 0.25f, and get negative position of the ball. Example:
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
This does not work correctly.
What options can be? (intersections, find the max/min point, math formulas) ?
Hi NoName :) Your method
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
would work perfectly if your object does not inherit other objects and there positions. To fix this make sure that you reset the position of all other game objects that you use as containers.

Inner Shadow Effect for line drawn to screen?

I've made an iOS program that allows the user to draw a line on the screen with their finger. I used the touchesBegan, touchesMoved, and touchesEnded methods, along with creating a CGContext and drawing my line that way. I want the line to look as though it is beveled into the screen, almost as if it was carved. How would this be possible?
You can achieve a simple bevel by stroking your lines three times:
first, with a color brighter than the background at points p(x-1, y-1) relative to the actual line
then, your line color at the actual line position, points p(x, y)
then, brighter than the line color, but darker than the background at p(x+1, y+1)
You can think of this as a light shining onto your lines from above and to the left, making the lower coordinates brighter, passing over the bevel and having a little shadow cast on the higher coordinates.
Once you get the hang of thinking through the pseudo-3D geometry this way, you can create prettier bevels, including details inside the line. Those will take more strokes.

Cocos2D iPhone - screen coordinates x sprite's internal coordinates

I am new to Cocos2D on iPhone. I see that Cocos2D uses a coordinate axis where 0,0 is at the bottom left corner and X positives are to the right and Y positives up.
Now I have created a sprite and added several sprites as subsprites of this one. For my surprise the subsprites appear mirrored in Y!!! The point 10,10 will be on the upper left corner of the sprite!!!
I can simply flip the sprite in Y to make it follow the same screen coordinate, but then the content will be reversed.
Is this a bug or what?
thanks.
Without seeing any example code is a shot in the dark, but I think you need to use Anchor points.
Each sprite has an anchor point of x, y.
ccp(0.5f, 0.5f) would be the center of the sprite.
(0,0) is the bottom left....(1.0f,1.0f) is top right etc.... Over 1.0 goes outside the sprite.
Child nodes (CCSprite) will use their anchor point on the parent node coordinates.
MySprite.anchorPoint = ccp(0.5f,0.5f);

Getting Position of a sprite in cocos2d

I am trying to get a each position of a sprite(curve),not only the center point,from starting point to end point.My sprite will rotate by 30deg when i tap on the screen.so i can't use sprite.position.x-sprite.contentSize.width/2 or some thing like that.Is there any way to get the position of a sprite or is there any other way to do this.
My need is i have different curve path.when i join those curve path,the new sprite should move along the curve path.
I think you are looking for convertToWorldSpace function of CCNode.
If I correctly understood you, you want to get sprite coord(not center, bottom left corner) rotated by 30 degrees? For x axis use formula: newX = X + cos(30) * pathLong. For y use the same but replace cos by sin and x by y of course :).