I'd like to know whether I can create a rectangle using a top-left and bottom-right CLLocationCoordinate2D, and afterwards check whether a coordinate is part of this rectangle by checking (topLeftCoordinate.latitude < coordinate.latitude && bottomRightCoordinate.latitude > coordinate.latitude) && (topLeftCoordinate.longitude < coordinate.longitude && bottomRightCoordinate.longitude > coordinate.longitude).
I thought a coordinate would represent a point on a sphere, and then this wouldn't work. But I get confused due to the 2D at the end of CLLocationCoordinate2D. Can someone clarify this?
Thanks :)
You will be able to create a MKPolygon from your coordinates and use this function to determine whether a coordinate is inside that polygon:
func isPoint(point: MKMapPoint, insidePolygon poly: MKPolygon) -> Bool {
let polygonVerticies = poly.points()
var isInsidePolygon = false
for i in 0..<poly.pointCount {
let vertex = polygonVerticies[i]
let nextVertex = polygonVerticies[(i + 1) % poly.pointCount]
// The vertices of the edge we are checking.
let xp0 = vertex.x
let yp0 = vertex.y
let xp1 = nextVertex.x
let yp1 = nextVertex.y
if ((yp0 <= point.y) && (yp1 > point.y) || (yp1 <= point.y) && (yp0 > point.y))
{
// If so, get the point where it crosses that line. This is a simple solution
// to a linear equation. Note that we can't get a division by zero here -
// if yp1 == yp0 then the above if be false.
let cross = (xp1 - xp0) * (point.y - yp0) / (yp1 - yp0) + xp0
// Finally check if it crosses to the left of our test point. You could equally
// do right and it should give the same result.
if cross < point.x {
isInsidePolygon = !isInsidePolygon
}
}
}
return isInsidePolygon
}
Related
I wonder how this to be done . Please do not close this question I need suggestion or any hint to implement it.
Source https://www.youtube.com/watch?v=4uHyHRKmxZk
I have created A node which is SCNCylinder and drawn from target to the centre of the screen from updateAtTime method
Firstly I thought it was drawn with TextNode. SO I tried following
Inside that class I have method help to draw a node at each Unit I pass to UnitLength
func drawOtherBaseUnit(height:Float,intoThe unitType:UnitLength,toTheNode zAxisNode:SCNNode) {
print("---------------------------------------------------------------------")
let distanceInTarget = Converter.convert(value: Double(height), to: unitType).output + 1
print("DISTANCE",distanceInTarget)
print("---------------------------------------------------------------------")
var text = ""
switch unitType {
case .inches:
text = "INCH"
case .centimeters:
text = "CM"
default:
return
}
if !distanceInTarget.isNaN {
var distance = Int(distanceInTarget)
var i = 0
while distance > 0 {
print("distance ",distance)
let node = UnitNode(withPosition: SCNVector3Make(0, 0, 0), radius: CGFloat(Converter.convert(value: 0.1, from: unitType, to: UnitLength.meters).output),text:"\(text) \(i)",forType:unitType)
let valueIncreaseOnAsPerTarget = Int(distanceInTarget) - distance
let valueToIncreaseMeter = Converter.convert(value: Double(valueIncreaseOnAsPerTarget), from: unitType, to: UnitLength.meters).output
node.position.y = -Float(valueToIncreaseMeter)
node.position.x = 0
zAxisNode.addChildNode(node)
distance -= 1
i += 1
}
}
}
The UnitNode class is draw SCNText and some other nodes.
This is working fine. I can see node each provided unit.
But it if I use drawOtherBaseUnit for Inch as well as CM UI is Lagging it is not smooth.
Is it correct way to implement desired output ?
How can i check if two SKSpriteNodes are near each other? like in a radius of 100. i am using the gamescene.swift and gamescene.sks.
SKSpriteNode has a position property with the (x, y).
Distance between two positions is sqrt((x1-x2)^2 + (y1-y2)^2)
So:
let dist = sqrt(pow(sk1.position.x - sk2.position.x, 2.0) + pow(sk1.position.y - sk2.position.y, 2.0))
if dist < 100 {
// they are close
}
This is center to center.
Based on #MartinR's comment, you could also
let dist = hypot(sk1.position.x - sk2.position.x, sk1.position.y - sk2.position.y)
Which does the distance function for you.
If you want to use built in SKPhysicsBody, then just set the body to a circle with radius of 100, then you can use the didBeginContact method when a contact occurs:
func setup()
{
let physicsBody1 = SKPhysicsBody(circleOfRadius:100.0)
physicsBody1.categoryBitMask = 1
physicsBody1.collisionBitMask = 0
physicsBody1.contactTestBitMask = 2
sprite1.physicsBody = physicsBody1
let physicsBody2 = SKPhysicsBody(circleOfRadius:100.0)
physicsBody2.categoryBitMask = 2
physicsBody2.collisionBitMask = 0
physicsBody2.contactTestBitMask = 1
sprite2.physicsBody = physicsBody2
}
func didBeginContact(contact:SKPhysicsContact)
{
//find some tutorials to your liking, and do your contact code here
}
I track the users location in my app to a database with all the coordinates. I then do some stuff to select a range of coordinates in a time frame, but when I save it to the server is takes a long time due to the big amount of data. (15 minutes is 900 CLCoordinate2D's and that is quite a bit).
What I wanna do is to remove the coordinate which are intersected by the preceding and following coordinate. Using overly simple coordinates for illustration purposes, but imagine this being done on real coordinates in an array of a couple thousand objects.
Example:
0,0 //Keep
1,1 //Drop
2,2 //Drop
3,3 //Keep
3,4 //Keep
4,4 //Keep
5,3 //Keep
Or, shitty visualized:
I know I should probably use some vector stuff, but I am not good at maths.
How can I reduce this array to remove the obsolete points?
You could try something like this...
var coordTimes:[(coord: CLLocationCoordinate2D, time: Double)] = []
// ...
func appendCoord(newCoord: CLLocationCoordinate2D, newTime: Double) {
guard coordTimes.count > 1 else {
coordTimes.append((newCoord, newTime))
return
}
let n = coordTimes.count
// So there are at least two already in the array
let c0 = coordTimes[n - 2].coord
let t0 = coordTimes[n - 2].time
let c1 = coordTimes[n - 1].coord
let t1 = coordTimes[n - 1].time
let dt = t1 - t0
let dtNew = newTime - t0
guard (dtNew > 0) && (dt > 0) else {
// decide what to do if zero time intervals. Shouldn't happen
return
}
// Scale the deltas by the time interval...
let dLat = (c1.latitude - c0.latitude) / dt
let dLon = (c1.longitude - c0.longitude) / dt
let dLatNew = (newCoord.latitude - c0.latitude) / dtNew
let dLonNew = (newCoord.longitude - c0.longitude) / dtNew
let tolerance = 0.00001 // arbitrary - choose your own
if (abs(dLat - dLatNew) <= tolerance) && (abs(dLon - dLonNew) <= tolerance) {
// Can be interpolated - replace the last one
coordTimes[n - 1] = (newCoord, newTime)
} else {
// Can't be interpolated, append new point
coordTimes.append((newCoord, newTime))
}
}
The tolerance is important, as you are very unlikely to get exactly matching intervals. Also, for the geodesists amongst you, there is no need to convert into map coordinates or calculate true distances, as the OP simply wants to know if the coordinates can be interpolated.
I'm using Metal and I want the user to be able to tap certain objects onscreen. I'm using a tap gesture recognizer, and so I have a CGPoint, but I don't know how to find the object at that point. Is there either a way to get the onscreen coordinates for metal, or a way to transform the CGPoint into the metal coordinate space?
I ended up doing this with a raytracing-type technique. I used a bounding sphere for each object and calculated whether or not the ray intersected it and how far from the ray's origin the intersection was, with the ray being from the camera through the onscreen point of clicking. The code is as follows:
var location = gestureRecognizer.locationInView(self.view)
var proj_matrix = projectionMatrix.copy()
var view_matrix = worldMatrix.copy()
var x = (2.0 * location.x) / self.view.bounds.size.width - 1.0;
var y = 1.0 - (2.0 * location.y) / self.view.bounds.size.height;
var newLocation = simpleVertex(x: Float(x), y: Float(y), z: Float(-1.0))
var projSpaceLoc = GLKMatrix4MultiplyVector4(GLKMatrix4Invert(proj_matrix.matrix(), nil), GLKVector4Make(newLocation.x, newLocation.y, newLocation.z, 1.0))
projSpaceLoc = GLKVector4Make(projSpaceLoc.x, projSpaceLoc.y, -1.0, 0.0)
var viewSpaceLoc = GLKMatrix4MultiplyVector4(GLKMatrix4Invert(view_matrix.matrix(), nil), projSpaceLoc)
GLKVector4Normalize(viewSpaceLoc)
//tappable is an array of nodes that can be tapped
for node in tappable {
var r: Float = node.r //this is the radius of the bounding sphere for that node
var c: GLKVector4 = node.origin //this is the origin of the bounding sphere
var little_c = GLKVector4DotProduct(c, c) - powf(r, 2)
var b = GLKVector4DotProduct(viewSpaceLoc, c)
var discriminant = powf(b, 2) - little_c
//If the discriminant is positive, their are two points of intersection, if it is 0, one point, and if it is negative, no points.
if (discriminant >= 0) {
var t_1 = (Float(-1.0)*b) + sqrtf(discriminant)
var t_2 = (Float(-1.0)*b) - sqrtf(discriminant)
if (intersect != nil) {
if (min(t_1, t_2) < intersect.intersect_point) {
intersect = (node, min(t_1, t_2))
}
} else {
intersect = (node, min(t_1, t_2))
}
}
}
I've got a player sprite that moves by rotation and its rotation is constantly changing but I also need to work out if a target is to the left or right of it and not within 45 degrees of the front or rear rotation.
I've written this code which I think should work but it seems only occasional pick up one side and slightly more on the other.
public void GrappleCheck(AsteroidSprite target)
{
float targetTragectory = (float)Math.Atan2(Position.Y - target.Position.Y, Position.X - target.Position.X);
if (targetTragectory < 0)
targetTragectory += (float)(Math.PI * 2);
if (Rotation < 0)
Rotation += (float)(Math.PI * 2);
if ((targetTragectory > Rotation + (float)(MathHelper.PiOver4 / 2)) && (targetTragectory < Rotation + (float)(Math.PI - (MathHelper.PiOver4 / 2))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (RightTarget != null)
{
if (RightTarget.Distance > target.Distance)
{
RightTarget.isTarget = false;
RightTarget = target;
RightTarget.ColorTint = Color.Blue;
RightTarget.isTarget = true;
}
}
else
{
RightTarget = target;
RightTarget.ColorTint = Color.Blue;
RightTarget.isTarget = true;
}
}
else if ((targetTragectory < Rotation - (float)(MathHelper.PiOver4 / 2)) && (targetTragectory > Rotation - (float)(Math.PI - (MathHelper.PiOver4 / 2))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (LeftTarget != null)
{
if (LeftTarget.Distance > target.Distance)
{
LeftTarget.isTarget = false;
LeftTarget = target;
LeftTarget.ColorTint = Color.Red;
LeftTarget.isTarget = true;
}
}
else
{
LeftTarget = target;
LeftTarget.ColorTint = Color.Red;
LeftTarget.isTarget = true;
}
}
else
{
target.isTarget = false;
}
if (controlInput.IsHeld(Keys.X))
{
Speed = Speed;
}
Working with angles can be quite annoying. Here are some ways to solve your problems without using angles:
First, we need the direction to the target and the movement direction:
var targetDirection = target.Positon - Position;
// Update this to match the actual direction. The following line assumes that
// a rotation of 0 results in the right direction.
var movementDirection = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));
The first problem you want to solve is determining, if the target is within a 45° cone. You can calculate the actual angle with the following formula:
var dot = Vector2.Dot(myDirection, targetDirection);
//if dot is negative, then target is behind me, so just use the absolute value
var cos = Math.Abs(dot) / myDirection.Length() / targetDirection.Length();
var angle = Math.Acos(cos);
if(angle < MathHelper.PiOver4 / 2) //45° opening angle
; // within cone
else
; // outside cone
Your second problem is determining, if the target is on the left or right side. We use a vector that is ortogonal to myDirection and points to the left for this:
//assuming that +x is the right axis and +y is the down axis
var normal = new Vector2(myDirection.Y, -myDirection.X);
dot = Vector2.Dot(normal, targetDirection);
if(dot > 0)
; // target is on the left side
else
; // target is on the right side
I hope that makes cleaning up your code a bit easier and more comprehensible. You should consider extracting some code in separate methods to make it more readable.
Okay I've solved it, the player rotation can be from 0 to 2 x PI + or -, to keep it + though I put in
if (Rotation < 0)
Rotation += (float)Math.PI * 2;
the rotation to the target can be 0-PI or 0 - Negative PI depending on the way you declare the atan2 and where the player is.
//This works out the difference from the targets rotation to the players rotation.
RotationDif = TargetRotation - PlayerRotation;
//If the difference is greater than PI then when we check to see if its is within
//the range 0-PI or 0-Negative PI it will be missed whilst still possibly being on
//either side of the player, adding PI * 2 to the targets rotation basically spins
//it past the player so the Difference will be the shortest angle.
if(Math.Abs(RotationDif) > Math.PI)
RotationDif = TargetRotation + (float)(Math.PI * 2) - PlayerRotation;
//Next we check to see if the target is left(negative) or
//the right(positive), the negative/positive assignment will depend
//on which way round you did the atan2 to get the target rotation.
if ((RotationDif > 0) && (RotationDif < (float)Math.PI))
//Insert right target code here
else if ((RotationDif < 0) && (RotationDif > -(float)Math.PI))
//Insert left target code here
else
//Insert no target code here to cover all basis
And that's it, I've made the If (RotationDif > 0) etc differently so the a 45 degree angle front and back is ignored by making it
If ((RotationDif > (float)(Math.PI / 8) &&
(RotationDif < (float)(Math.PI - (Math.PI / 8)))
and the opposite for the other side, hope this helps someone else as it took me nearly 2 sodding weeks to work out :/