Draw a straight line on the screen in Cocos2d? - iphone

I am making a game in which you shoot a ball. I want there to be a line that follows your finger when you touch the screen (showing you the path the ball will travel). It needs to be straight, and also a little side question:
Once the line is drawn, is there a way to detect if it collides with a sprite? So, if the line hits a wall, it won't go through it? Any help is appreciated, Thanks!

for drawing only a line you can use ccDrawLine function and you can change line color using glcolor4f you can also change line width using glLineWidth but iprefer to use these functions for debug purpose. it's better if you try some sprites drawing for accual game.
for collision testing I suggest you use box2D and create body(and fixture) for both line and moving sprites. you can edit fixtures you created when ball changes position.

Related

How do I efficiently render a pixelated line?

I am working in Unity2D and have a character who can shoot in all directions. The game has mobile controls and the player will choose his shooting direction with a joystick on the right side.After testing I found it rather difficult to correctly determin where the shot is going to land at. To convey this to the player I wanted to add a sort of "shooting line of sight" to the player. It is moving around the player according to the joysticks position. (As seen in the picture)
Because I want the line to be pixelated (to match the design theme of the game) I cant use the standard LineRenderer component as it doesn't support such pixelated lines. So I started looking for solutions to this problem and stumbled upon Bresenham's line algorithm. After implementing it into the game I knew which x, y coordinates I had to fill with a pixel. Currently every single white pixel in this line is a GameObject with its own SpriteRenderer and a single white pixel as a sprite. In my test scenario up to 400 pixels where rendered at once. I am already using an object pooling system to minimize the performance drag. Moving this line around drops me from around 1100 Fps down to like 200 Fps in the editor. I know it will be slightly better in the build game but I am sure it will take a toll on older mobile devices when enemys, animations etc. are present in the level.
So my question is: Is there a better or more efficient way to render the sprites? Or (preferably) do you have a better idea on how to render this line at all without creating ~300+ GameObjects. (e.g. Shaders (Zero experience), drawing on a texture)
I am grateful for any ideas that lead me into the right direction.

Unity3D - Line Renderer Cannot See Distance With Camera

I'm trying to use a line renderer to act like a grappling cable for a vehicle of mine in my game. It seems, however, from the camera view the line is only visible up to a certain distance far away. Is there anything I can do to make the camera see more of the line?
EDIT: I should clarify, I know how to extend the length of the line, I have that script written. What I'm saying is that the line is hard to see from the camera when looking from behind. I want to give the player a good idea of how far the hook is going, even when it's over 30 units away on the forward z axis?
FURTHER:
When I turn my car to the side while moving with the line extended a distance, I can see much more of the line since I start to see the line from the side???
line renderer is a line between two points.
you just need to adjust the values under 'Positions'.
you can also click on the three dot icon at the top to 'edit points in scene view' and drag the further point outward, you can also add points with the plus icon beside it.
you could also use raycast for your grappling hook
I'm not super experienced with unity's linerenderer. However it looks like the linerenderer stops at your pink light barrier. I'm not sure if the linerenderer works like a raycast however.
EDIT:
Ok. Now I get what you want to achive. My guess is that unity does not render the line because it becomes smaller than a pixel and the lineRenderer is not a 3D element. I had some succes by using a different curve. Try to increase the width of the line the further away it is.

Breaking up a sprite in cocos2d

I'm making a 2D plane fighting game for the iPhone in cocos2d. I'm trying to make it so when you shoot an enemy plane, it breaks into a couple of separated pieces that will fall out of sight. What is generally the best practice for breaking one sprite up into many? should I create new images for each separate piece, or treat the initial image like a sprite sheet, and make a new sprite from segments?
Please look at this tutorial
It makes a grid of points then moves the internal (not-edge) ones around randomly a bit so it's not all perfect triangles. Then each update it moves/rotates the triangles separately--then draws them all at once.
You treat the whole thing as a sprite, so can run any of the usual actions on it. This example uses CCMoveBy to move the whole group down off the bottom.

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.

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