Extending CAShapeLayer with physics attributes (or is there a better solution)? - cashapelayer

First disclamer, I'm a complete n00b in terms of iOS programming, but have over 15 years of other OOP experience.
I've done extensive search thus far on what I'm trying to accomplish, but have not come up with definitive answer.
I'm trying to create effect of drawing straight lines (simular in effect to fruit ninja cuting motion) where it would draw a line between point a (beginTouch) and b (endTouch) regardless of the angle.
I was able to achieve it pretty easily with use of CAShapeLayer. It also very usefull as it supports some animation options that I would like to be able to apply on end result line.
But what I also need is an ability to emulate physics behavior with resulting line shape, such as bouncing, possibly deforming the line to emulate stretching, and apply some sort of destruction animation (fading/breaking appart).
I'm not sure if the CAShapeLayer is the best option to achieve this effect and if it is where do I look next on extending it or combining it with something else.
I know this is not a specific code example type of question, but expert oppinion is appreciate.
Cheers
Alex

Related

Animating wind in Spritekit?

Is there a way to animate wind for certain game objects?
For example, branches of trees should gently move, like there's a breeze in game. Not gameplay, more like a special background effect.
If it's not possible in code, what would be the best way to create proper sprite images?
I see a few options to actually achieve that Wind Effect.
SKFieldNode, It allows you to actually apply physics effects to nodes. And if you want real tree branches that can move based on the physics, you should combine SKFieldNode with SKPhysicsJoint. When you combine those two you can actually create indepedent Branch Nodes to receive some kind of force to simulate a wind effect. To understand what you can do with SKPhysicsJoint, check out this guide. This solution can get really complex and hard to achieve with superficial understanding of SpriteKit Engine, but you can create an amazing effect using it. Personally, I would not recommend if you have deadlines to attend, physics always get buggy if you lose your grasp on what you are doing, you may invest a lot of time trying to achieve this using physics.
Create different animation of your tree responding to wind movement and control which animation frame you should use at that specif case. I Would highly recommend this one, because you will have control over what is going on with you tree and people that play games don't pay that much attention to what is going on with background, altho is a good thing to think about it from the game experience.
Create your tree textures and change the anchor point to be at the place you want the effect to be more effective and responsive. The farthest the coordinate in the node is from the anchor points coordinate, less effect from your SKAction it will get.
Sorry for my English, is not my native language.

Removing skew/distortion based on known dimensions of a shape

I have an idea for an app that takes a printed page with four squares in each corner and allows you to measure objects on the paper given at least two squares are visible. I want to be able to have a user take a picture from less than perfect angles and still have the objects be measured accurately.
I'm unable to figure out exactly how to find information on this subject due to my lack of knowledge in the area. I've been able to find examples of opencv code that does some interesting transforms and the like but I've yet to figure out what I'm asking in simpler terms.
Does anyone know of papers or mathematical concepts I can lookup to get further into this project?
I'm not quite sure how or who to ask other than people on this forum, sorry for the somewhat vague question.
What you describe is very reminiscent of augmented reality marker tracking. Maybe you can start by searching these words on a search engine of your choice.
A single marker, if done correctly, can be used to identify it without confusing it with other markers AND to determine how the surface is placed in 3D space in front of the camera.
But that's all very difficult and advanced stuff, I'd greatly advise to NOT try and implement something like this, it would take years of research... The only way you have is to use a ready-made open source library that outputs the data you need for your app.
It may even not exist. In that case you'll have to buy one. Given the niché of your problem that would be perfectly plausible.
Here I give you only the programming aspect and if you want you can find out about the mathematical aspect from those examples. Most of the functions you need can be done using OpenCV. Here are some examples in python:
To detect the printed paper, you can use cv2.findContours function. The most outer contour is possibly the paper, but you need to test on actual images. https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html
In case of sloping (not in perfect angle), you can find the angle by cv2.minAreaRect which return the angle of the contour you found above. https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html (part 7b).
If you want to rotate the paper, use cv2.warpAffine. https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html
To detect the object in the paper, there are some methods. The easiest way is using the contours above. If the objects are in certain colors, you can detect it by using color filter. https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html

2D from 3D animation/graphics

I am sorry if it's a duplicate, but most of questions ask an opposite thing: 3D from 2D or sometimes they ask for a tool '2D from 3D' or: what to choose 2D or 3D. My case is different, I don't want any software/tool(s), I have already chosen the dimensions and I want 2D from 3D. Also I would like to give it a try writing myself, that's why I don't want any software.
I do understand that it will be a long term project, especially for such an amateur as me, but I find it interesting, that's why I want to attempt this task.
I will probably take 3D objects from the Xcode's Scene Kit and I would like to write a code, which will convert them into 2D.
I don't know what to start from and what to read. So any help will be very much appreciated. Also if there are some code examples (no matter which language) it will be awesome, because it's good to see how to assemble things/code etc.
//Also, please, have a look at the tags. I am not quite sure whether I choose them correctly. I will add Swift just in case. Because in my case it will be Swift, actually. However, the question is sort of general, so some people who won't use Swift may probably find prospective answers useful as well.

How much do I NEED to know to make a 2D game engine?

I rendered a sprite. After the object is rendered, I can manipulate the points in the object to add animation. Adding a large amount of variables will tell how the rendered object interacts with other objects. A 2d game running with sprites would not need lighting or textures, only the sprites and variables. Is this really all I need to know to make a game engine? Is manipulating various variables, and rendering sprites all I need to know? I feel like it is too easy that way. And I also don't know where and how can I learn everything else about Java?
It depends on what you consider an "engine." Virtually any boiler-plate code or core framework of a game can be called an engine in the most literal, technical sense. What you described could very well be a suitable engine for the purposes you need it for - until you find out that it isn't.
It is likely that these fundamental skills will allow you to make a simple game, and it will be a great learning experience for you in expanding your knowledge. However, you will undoubtedly find that there is more to learn as you progress.
So, in summary, it will probably not be enough for a very complex game, but it is nonetheless where you should start anyway, and use it to learn in baby steps.
Manipulating variables is all you need for any code. Even rendering sprites is loading and modifying variables. The thing you need to know is which variables you want to add/manipulate.
To make a game engine you need to first consider what you want it to do, and plan out what variables you need and functions you want it to perform. Once you have this you can begin implementing it.
There are quite a few game engines around however, and re-inventing the wheel is never advised. If you want a 2d game engine to look at, Slick2D is fairly good for java and will have all the stuff you want for a basic 2D game.

Drawing lines in win32/GDI with a custom pen style?

I have a need to do some drawing using win32/GDI (Native, not .NET), and I've run into the following problem:
I need to draw lines that are "styled." For example, in the attached image, the line marked "A" is a straight line as far as my application data is concerned, it just needs to be drawn with the additional zigzag as a style. Of course, this is easy to do programatically, but it gets more complicated when the line can be at any angle ("B") or even a bezier curve ("C").
Now, I could do this all programatically, painstakingly doing the math to put a zigzag around each line possibility, but that will take a lot of time and, more importantly, be rather error prone.
Is it possible to just give windows/GDI a "style" to apply to the line, perhaps a bitmap like the one marked "D", and have it use that as a pen to draw the lines? If not, is there a more flexible and less error-prone way of doing this than writing a bunch of specific drawing code for each of the "styled" lines?
*Apparently first timers can't post images. Examples can be found at http://i.imgur.com/IC0T2.png
This is not possible in Win32 GDI. You will need to do the math yourself.
It should be noted however, that you can obtain the points used to make up the line or curve which should make it substantially easier.
See this "Hit-Testing" tutorial for an example.
http://msdn.microsoft.com/en-us/library/ms969920.aspx
For a bezier curve you would use Path Functions:
BeginPath
PolyBezier
EndPath
FlattenPath
For straight lines you could use:
LineDDA
As far as I know there's nothing in GDI or even GDI+ that would support this. The only line options you have are dash-patterns, compound-pens, dash caps, end caps, and fill brushes.
You might be able to trick one of those functions into drawing something vaguely akin to your wiggles for straight splines, but it definitely won't work for curved splines.
It shouldn't be too hard to do this however. Sure, it will take a day or so, but all you have to do is write a line and bezier interpolator, divide the curves into equal segments, find the tangents at all those segments and alternate left and right. You'll end up with an array of points which can be drawn very quickly as a polyline.
There's nothing that'll do this automatically. You'll have to write some code. You might want to look at the LineDDA API in GDI. It might simplify the math your code will need.
ExtCreatePen(), maybe? I don't know for a fact if it supports zigzagging...

Resources