[英]SpriteKit : calculate distance between two texture masks
Place an SKPhysicsBody that is in a shape of a line at the center of your ship with a width of 1 and the height of your scene, then in the didBeginContact method, grab the 2 contact points. 将SKPhysicsBody放置在船舶中心的一条线形状中,宽度为1,场景高度,然后在didBeginContact方法中,抓住2个接触点。 You now know 2 points, just use the distance formula (in this case it is just y2-y1) and you have your answer
你现在知道2分,只需使用距离公式(在这种情况下它只是y2-y1),你有答案
I found a different way to solve my problem, but I think that KnightOfDragon's one is conceptually better (although I did not manage to make it work). 我找到了一种不同的方法来解决我的问题,但我认为KnightOfDragon的一个在概念上更好(尽管我没有设法让它工作)。
The terrain's texture is essentially a bitmap with opaque and transparent pixels. terrain的纹理本质上是一个具有不透明和透明像素的位图。 So I decided to parse these pixels, storing the highest opaque pixel for each column, building a "radar altitude map".
所以我决定解析这些像素,为每列存储最高的不透明像素,构建一个“雷达高度图”。 So I just have to calculate the difference between the bottom of the ship and the altitude of the column right beneath its center:
所以我只需要计算船底和中心正下方柱的高度之间的差异:
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(terrain.texture.CGImage));
const UInt32 *pixels = (const UInt32*)CFDataGetBytePtr(imageData);
NSMutableArray *radar = [NSMutableArray new];
for (long col = 0; col < terrain.size.width; col++)
[radar addObject:@(0)];
for (long ind = 0; ind < (terrain.size.height * terrain.size.width); ind++)
{
if (pixels[ind] & 0xff000000) // non-transparent pixel
{
long line = ind/terrain.size.width;
long col = ind - (line*terrain.size.width);
if ([radar[col]integerValue] <terrain.size.height-line) radar[col] = @(terrain.size.height-line);
}
}
This solution could be optimized, of course. 当然,这个解决方案可以进行优化。 It's just the basic idea.
这只是基本的想法。
I've added an image to show the original texture, its representation as opaque/transparent pixels, and a test by putting little white nodes to check where the "surface" was. 我添加了一个图像来显示原始纹理,它表示为不透明/透明像素,并通过放置小白色节点来检查“表面”的位置进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.