Rubymotion MapKit Path Overlay - ios

Does anyone know of any code examples that display paths on a MKMapView using RubyMotion?
I've found some useful examples that display a point or pin but none that display a path.
Thanks

For the record I was able to draw a line on the map like this. Hopefully this is useful to someone as I could find no examples drawing lines on a map using RubyMotion:
arr = [CLLocationCoordinate2D.new(49.7414435, -123.08), CLLocationCoordinate2D.new(50.7414435, -123.0)]
ptr = Pointer.new(CLLocationCoordinate2D.type, arr.length)
ptr[0] = arr[0]
ptr[1] = arr[1]
pl = MKPolyline.polylineWithCoordinates(ptr, count:2)
view.addOverlay(pl)
With the help of this discussion:
https://groups.google.com/forum/?fromgroups=#!topic/rubymotion/F5CH780lu7c

To expand upon this (which was very helpful), you also need to create a viewForOverlay method, like so:
def mapView(map_view, viewForOverlay:overlay)
if overlay.class == MKPolyline
overlayView = MKPolylineView.alloc.initWithPolyline(overlay)
overlayView.strokeColor = UIColor.systemRedColor
overlayView.lineWidth = 2
overlayView
end
end

Related

Getting data from a table

Using Tiled I generated a Lua file which contains a table. So I figured that I'd write a for loop which cycles through the table gets the tile id and checks if collision is true and add collision if it was. But, I've been unable to get the tile id's or check they're properties. But it returned a error saying that I tried to index nil value tileData.
Here is the Map file
return {
version = "1.1",
luaversion = "5.1",
-- more misc. data
tilesets = {
{
name = "Tileset1",
firstgid = 1,
tilewidth = 16,
tileheight = 16,
tiles = {
{
id = 0,
properties = {
["Collision"] = false
}
},
}
}
layers = {
{
type = "tilelayer",
name = "Tile Layer 1"
data = {
-- array of tile id's
}
}
}
}
And here is the for loop I wrote to cycle through the table
require("Protyping")
local map = love.filesystem.load("Protyping.lua")()
local tileset1 = map.tilesets
local tileData = tileset1.tiles
local colision_layer = map.layers[1].data
for y=1,16 do
for x=1,16 do
if tileData[colision_layer[x*y]].properties["Colision"] == true then
world:add("collider "..x*y,x*map.tilewidth, y*tileheight,tilewidth,tileheight)
end
end
end
Try this:
tileset1 = map.tilesets[1]
instead of
tileset1 = map.tilesets
lhf's answer (map.tilesets[1] instead of map.tilesets) fixes the error you were getting, but there are at least two other things you'll need to fix for your code to work.
The first is consistent spelling: you have a Collision property in your map data and a Colision check in your code.
The second thing you'll need to fix is the way that the individual tiles are being referenced. Tiled's layer data is made of 2-dimensional tile data laid out in a 1-dimensional array from left-to-right, starting at the top, so the index numbers look like this:
You would think you could just do x * y to get the index, but if you look closely, you'll see that this doesn't work. Instead, you have to do x + (y - 1) * width.
Or if you use zero-based x and y, it looks like this:
Personally, I prefer 0-based x and y (but as I get more comfortable with Lua, that may change, as Lua has 1-based arrays). If you do go with 0-based x and y, then the formula is x + 1 + y * width.
I happen to have just written a tutorial this morning that goes over the Tiled format and has some helper functions that do exactly this (using the 0-based formula). You may find it helpful: https://github.com/prust/sti-pg-example.
The tutorial uses Simple Tiled Implementation, which is a very nice library for working with Tiled lua files. Since you're trying to do collision, I should mention that STI has a plugins for both the bump collision library and the box2d (physics) collision library.

Check whether GMSPolygon is inside other GMSPolygon or not

I have Many GMSPolygoninside my google map. Now i want to check for one specific polygon that is it inside(fully inside) any of the other polygons. Also need to find out that which other polygons are intersecting with boundaries to this polygon, and other polygon which are neither intersecting, neither inside given polygon nor covering this polygon.
Can anyone have idea how to do this?
EDIT:
I got the library/code to do the above same thing for MKPolygon, you can see it over here: https://github.com/geeksweep/MKPolygon-GSPolygonIntersections
Now, I am thinking that i should convert whole GMSPolygon into MKPolygon and apply this library's code to get the required result. But I think this is not proper way for doing this. Do anyone have any idea to do this in very simple manner.
After searching lot things, i found one solution, i think that is not that much proper, but still better than other 3-4 solutions that i found. If anyone find better solution that this, tell me, if i find them better and proper, i will accept that one and will change in my code too. Rightnow, i have used following code to do this.
GMSPath *path1=polygon1.path, *path2=polygon2.path;
BOOL flag1= NO;
BOOL flag2= NO;
for (int i=0; i<path1.count; i++)
{
if (GMSGeometryContainsLocation([path1 coordinateAtIndex:i], path2, YES)==false)
{
flag1 = true;
}
if (GMSGeometryIsLocationOnPath([path1 coordinateAtIndex:i], path2, YES)==true)
{
flag2 = true;
}
}
if (flag1==false)
{
NSLog(#"polygon1 is fully inside polygon2");
}
else if (flag2 == true)
{
NSLog(#"polygon1 intersects with polygon2");
}
else
{
//Do the above procedure again just by switching path1 and path2
//and at end you can find that is polygon2 is inside polygon1 or not,
//and if it is not, then this means both polygon1 and polygon2 are distinct
//then neither intersects, nor inside each other
}
You can use this library:
http://sourceforge.net/projects/polyclipping/
to see which other polygons are intersecting with boundaries to your polygon

V3: Make two bodies of same collisionType collide with each other

I am a newbie developer in cocos2d, so please be patient to me. Thanks.
What I am trying to implement is two bodies of the same collision type (They are also not in a custom class, just CCSprite) colliding only with each other and the ground (e.g. debris) and not with anything else. What I don't understand is how to format the ccPhysicsCollisionBegin method. If I format it like this:
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair debris:(CCSprite *)debris debris:(CCSprite *)debris2
and return YES it will still affect other unwanted bodies, and after a couple of CCLOGs I found out it's never called anyway. So what is the correct way to achieve what I want? Hope what I have requested is possible.
Code used:
debris.physicsBody = [CCPhysicsBody bodyWithRect:[debris boundingBox] cornerRadius:0];
debris.name = #"Debris";
debris.physicsBody.collisionType = #"debris";
[physicsNode addChild:debris];
You need to set-up the collisionMask, which defines what the body can collide with. You should also probably set-up the collisionCategories as well:
debris.physicsBody = [CCPhysicsBody bodyWithRect:[debris boundingBox] cornerRadius:0];
debris.name = #"Debris";
debris.physicsBody.collisionType = #"debris";
debris.physicsBody.collisionCategories = #[ #"debris" ];
debris.physicsBody.collisionMask = #[ #"debris" ];
[physicsNode addChild:debris];

Object1.position.y = Object2.position.y?

I'm trying to have an object1 (SKSpriteNode) create another object (object2), when it is at a certain height (y-coordinate). I've got all the code I need, except I can't figure out how to properly write:
object1.position.y = object2.position.y
Position may be a structure.
Try:
CGPoint point = CGPointMake(object1.position.x, object2.position.y);
object1.position = point;

Using UIBezierPath do draw in Rubymotion

I am trying to setup a view allow you to draw a signature. All my selectors and UIGestureRecognizors seem to be working based on what i'm getting in my logs but nothing is showing on my screen after movement. Does anyone see something i'm missing that is not allowing the stroke to show up with my gesture? Any help would be very appreaciated
class SignatureController < UIViewController
def viewDidLoad
self.title = "Please Sign"
self.view.backgroundColor = UIColor.whiteColor
#path = UIBezierPath.bezierPath
#path.setLineWidth(2.0)
pan = UIPanGestureRecognizer.alloc.initWithTarget(self, action: 'pan:')
pan.maximumNumberOfTouches = pan.minimumNumberOfTouches = 1
self.view.addGestureRecognizer(pan)
super
end
def pan(pan)
currentPoint = pan.translationInView(self.view)
case pan.state
when UIGestureRecognizerStateBegan
#path.moveToPoint(currentPoint)
when UIGestureRecognizerStateChanged
#path.addLineToPoint(currentPoint)
NSLog("#{currentPoint}")
else
p "SwipeGesture unrecognized"
end
self.view.setNeedsDisplay
end
def drawRect(rect)
UIColor.blackColor.setStroke
#path.stroke
end
end
The RubyMotion samples repo on github, has the code to do what you want to do. It uses a custom UIView, called PaintView, which has all of the event listeners and methods for drawing code on touches.
Check it out here:
https://github.com/HipByte/RubyMotionSamples/tree/master/ios/Paint
The issue came from using UIViewController vs. UIView. The UIBezierPath only runs under the UIView call. After using this my code worked perfect. Thanks.

Resources