Random CGPoint(s) - math

How could I get a random CGPoint that is outside the screen boundaries (frame)?
Also, given that point, how could I find a symmetrical point to it to the middle of the screen- e.g. say I have the point (width+1,height+1). Now the symmetrical point is (-1,-1). Say I have (-1,height +1)- symmetrical would be (width+1,-1).
Hope this is clear, and thanks!

If I understand your question correctly, you can use the following method:
- (CGPoint) randomPointIn:(CGRect)inrect outsideOf:(CGRect)outrect
{
CGPoint p;
do {
p.x = inrect.origin.x + inrect.size.width * (float)arc4random()/(float)UINT32_MAX;
p.y = inrect.origin.y + inrect.size.height * (float)arc4random()/(float)UINT32_MAX;
} while (CGRectContainsPoint(outrect, p));
return p;
}
It returns a random point that is inside inrect, but outside of outrect.
(I have assumed that inrect is "considerably larger" than outrect,
otherwise many loop iterations might be necessary to find a valid point.)
In your case, you would use outrect = CGRectMake(0, 0, width, height),
and inrect would specify the allowed domain.
And the point symmetrical to (x, y) with respect to the middle of the screen
with size (width, height) is (width - x, height - y).
UPDATE: As I just found here: http://openradar.appspot.com/7684419,
CGRectContainsPoint will return false if you provide it a point that is on the boundary of the CGRect. That means that the above method returns a point that is outside of
or on the boundary of the given rectangle outrect. If that is not desired,
additional checks can be added.

I believe this should work.
//To get a random point
- (CGPoint)randomPointOutside:(CGRect)rect
{
// arc4random()%(int)rect.size.width
// This gets a random number within the width of the rectangle
//
// (arc4random()%2) ? rect.size.width : 0)
// This has a 50:50 to put the point in the q1 quadrant relative to the top right point of the rect
//
// q4 q1
// _____ +
// | |
// | q3 | q2
// |_____|
//
float x = arc4random()%(int)rect.size.width + ((arc4random()%2) ? rect.size.width : 0);
float y = arc4random()%(int)rect.size.height + ((arc4random()%2) ? rect.size.height : 0);
return CGPointMake(x, y);
}
//To get the symmetrical point
- (CGPoint)symmetricalPoint:(CGPoint)p around:(CGRect)rect
{
return CGPointMake((p.x-rect.size.width) * -1, (p.y-rect.size.height) * -1);
}

Related

LibGDX Own 3D effect set visible lines after getting the distance

Hey sorry for the title
I wanted to create my own 3D effect and it worked well but now i want so visualize my gotten distance but there was too much math for me...
I will put the funktion here that should get the distance and draw the lines.
Thanks for help 🙂
Ask if you need some code please!
// fov is how much is in the vision
// count is how many test lines are drawn in fov
// speed is how far the tester should move on one frame
public void test(float fov, float count, int speed) {
int size = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
for (float i = 1; i <= count; i++) {
// Distance.test2D() will send a sensor from the roration of "rotation" and if it touches something is returns how many pixels were between that objects. The maxValue will return if it doesnt touch anything for 500 pixels
float distance = Distance.test2D(position, rotation - (fov) + i * (fov / (count / 2)), 500, speed);
// int drawAt = where to draw the line
int drawAt = // where the line should be drawn on the x axes
// will draw a strate line from the buttom to a specefic height
shapeRenderer.line(drawAt, 0, drawAt, (200 - distance * 2));
}
}

Three.js - get screen position with no camera

I need to calculate screen position outside of Three, with with no camera present.
I know the object position, camera position and camera target
I've seen lots of instructions such as three.js Vector3 to 2D screen coordinate with rotated scene
vector.project(camera);
vector.x = Math.round( ( vector.x + 1 ) * w / 2 );
vector.y = Math.round( ( - vector.y + 1 ) * h / 2 );
I understand Vector.project camera takes into account the camera settings, FOV etc I'd assume?
Vector3 has projectOnVector(), does this do the same thing as vector3.project(camera) ?
Is there a way to calculate where the object would be on screen without being able to access the camera?
Yes, the Vector3.project takes into account the camera settings...
You need to calculate a projection matrix as you are trying to transform a position from world space to view space. This is a great little animation describing the journey that point will make: https://jsantell.com/model-view-projection/mvp.webm (lifted from this useful page: https://jsantell.com/model-view-projection/).
If you look in the three source code it will show you everything you need to do this. Vector3.project is just applying the two matrices from the camera:
return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
So how do you get these matrices? The project matrix is generated here.
You can ignore the view, skew and zoom, so you just need near, far, aspect and fov.
updateProjectionMatrix() {
const near = this.near;
let top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
let height = 2 * top;
let width = this.aspect * height;
let left = - 0.5 * width;
this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );
}
If you need makePerspective it is here
The matrixWorldInverse is just that... take your world matrix and inverse it. Three.js does it here.
This gives you your view matrix. So, view matrix multiplied with the projection gives you your screen space position... just like in a shader i.e:
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
I'm assuming your point is in world space so you can ignore the model part as this just takes you from model to world.

Extend 2d polygon by vector

I have convex polygons, and I want to extend them by projecting along a vector like so:
(Original polygon and vector on left, desired result on right.)
My polygons are stored as a series of points with counter-clockwise winding. What I want to find are the "starting" and "stopping" point that I need to project from, as in the circled vertices below.
(The green arrows are to indicate the polygon's winding, giving the "direction" of each edge.)
My original plan was to determine which points to use by projecting a ray with the vector's direction from each point, and finding the first and last points whose ray doesn't intersect an edge. However, that seems expensive.
Is there a way I can use the edge directions vs the vector direction, or a similar trick, to determine which points to extend from?
Look at points where the direction of the vector falls between the directions of the edges.
In other words, take three vectors:
of the edge leading out of the vertex
of the translation vector
opposite to the edge leading to the vertex
If they are in this order when going CCW, i.e. if the second vector is between the first and the third, this is an "inside" point.
In order to determine whether a vector lies between two other vectors, use cross product as described e.g. here.
Yes you can. You want to project along x, y. So the normal is y, -x. Now rotate by that (atan2, or you can you it directly if you understand rotation matrices). The points to project from and now the minimum and maximum x, You can also speed up the projection by always doing it along an axis then rotating back.
n.m. answered the question as I asked and pictured it, but upon programming I soon noticed that there was a common case where all vertices would be "outside" vertices (this can be easily seen on triangles, and can occur for other polygons too).
The text explanation.
The solution I used was to look at the normal vectors of the edges leading into and exiting each vertex. The vertices we want to extend are vertices that have at least one edge normal with a minimum angle of less than 90 degrees to the delta vector we are extending by.
The outward-facing edge normals on a counterclockwise-wound polygon can be found by:
normal = (currentVertex.y - nextVertex.y, nextVertex.x - currentVertex.x)
Note that since we don't care about the exact angle, we don't need to normalize (make a unit vector of) the normal, which saves a square root.
To compare it to the delta vector, we use the dot product:
dot = edgeNormal.dot(deltaVector)
If the result is greater than zero, the minimum angle is acute (less than 90). If the result is exactly zero, the vectors are perpendicular. If the result is less than zero, the minimum angle is obtuse. It is worth noting when the vectors are perpendicular, since it lets us avoid adding extra vertices to the extended polygon.
If you want to visualize how the angle works with the dot product, like I did, just look at a graph of arc cosine (normally you get the angle via acos(dot)).
Now we can find the vertices that have one acute and one not-acute minimum angle between its edge normals and the delta vector. Everything on the "acute side" of these vertices has the delta vector added to it, and everything on the "obtuse side" stays the same. The two boarder vertices themselves are duplicated, having one extended and one staying the same, unless the "obtuse side" is exactly perpendicular to the delta vector (in this case we only need to extend the vertex, since otherwise we would have two vertices on the same line).
Here is the C++ code for this solution.
It may look a little long, but it is actually quite straightforward and has many comments so it hopefully shouldn't be hard to follow.
It is part of my Polygon class, which has a std::vector of counterclockwise-wound vertices. units::Coordinate are floats, and units::Coordinate2D is a vector class that I feel should be self-explanatory.
// Compute the normal of an edge of a polygon with counterclockwise winding, without normalizing it to a unit vector.
inline units::Coordinate2D _get_non_normalized_normal(units::Coordinate2D first, units::Coordinate2D second) {
return units::Coordinate2D(first.y - second.y, second.x - first.x);
}
enum AngleResult {
ACUTE,
PERPENDICULAR,
OBTUSE
};
// Avoid accumulative floating point errors.
// Choosing a good epsilon is extra important, since we don't normalize our vectors (so it is scale dependent).
const units::Coordinate eps = 0.001;
// Check what kind of angle the minimum angle between two vectors is.
inline AngleResult _check_min_angle(units::Coordinate2D vec1, units::Coordinate2D vec2) {
const units::Coordinate dot = vec1.dot(vec2);
if (std::abs(dot) <= eps)
return PERPENDICULAR;
if ((dot + eps) > 0)
return ACUTE;
return OBTUSE;
}
Polygon Polygon::extend(units::Coordinate2D delta) const {
if (delta.isZero()) { // Isn't being moved. Just return the current polygon.
return Polygon(*this);
}
const std::size_t numVerts = vertices_.size();
if (numVerts < 3) {
std::cerr << "Error: Cannot extend polygon (polygon invalid; must have at least three vertices).\n";
return Polygon();
}
// We are interested in extending from vertices that have at least one edge normal with a minimum angle acute to the delta.
// With a convex polygon, there will form a single contiguous range of such vertices.
// The first and last vertex in that range may need to be duplicated, and then the vertices within the range
// are projected along the delta to form the new polygon.
// The first and last vertices are defined by the vertices that have only one acute edge normal.
// Whether the minimum angle of the normal of the edge made from the last and first vertices is acute with delta.
const AngleResult firstEdge = _check_min_angle(_get_non_normalized_normal(vertices_[numVerts-1], vertices_[0]), delta);
const bool isFirstEdgeAcute = firstEdge == ACUTE;
AngleResult prevEdge = firstEdge;
AngleResult currEdge;
bool found = false;
std::size_t vertexInRegion;
for (std::size_t i = 0; i < numVerts - 1; ++i) {
currEdge = _check_min_angle(_get_non_normalized_normal(vertices_[i], vertices_[i+1]), delta);
if (isFirstEdgeAcute != (currEdge == ACUTE)) {
// Either crossed from inside to outside the region, or vice versa.
// (One side of the vertex has an edge normal that is acute, the other side obtuse.)
found = true;
vertexInRegion = i;
break;
}
prevEdge = currEdge;
}
if (!found) {
// A valid polygon has two points that define where the region starts and ends.
// If we didn't find one in the loop, the polygon is invalid.
std::cerr << "Error: Polygon can not be extended (invalid polygon).\n";
return Polygon();
}
found = false;
std::size_t first, last;
// If an edge being extended is perpendicular to the delta, there is no need to duplicate that vertex.
bool shouldDuplicateFirst, shouldDuplicateLast;
// We found either the first or last vertex for the region.
if (isFirstEdgeAcute) {
// It is the last vertex in the region.
last = vertexInRegion;
shouldDuplicateLast = currEdge != PERPENDICULAR; // currEdge is either perpendicular or obtuse.
// Loop backwards from the end to find the first vertex.
for (std::size_t i = numVerts - 1; i > 0; --i) {
currEdge = _check_min_angle(_get_non_normalized_normal(vertices_[i-1], vertices_[i]), delta);
if (currEdge != ACUTE) {
first = i;
shouldDuplicateFirst = currEdge != PERPENDICULAR;
found = true;
break;
}
}
if (!found) {
std::cerr << "Error: Polygon can not be extended (invalid polygon).\n";
return Polygon();
}
} else {
// It is the first vertex in the region.
first = vertexInRegion;
shouldDuplicateFirst = prevEdge != PERPENDICULAR; // prevEdge is either perpendicular or obtuse.
// Loop forwards from the first vertex to find where it ends.
for (std::size_t i = vertexInRegion + 1; i < numVerts - 1; ++i) {
currEdge = _check_min_angle(_get_non_normalized_normal(vertices_[i], vertices_[i+1]), delta);
if (currEdge != ACUTE) {
last = i;
shouldDuplicateLast = currEdge != PERPENDICULAR;
found = true;
break;
}
}
if (!found) {
// The edge normal between the last and first vertex is the only non-acute edge normal.
last = numVerts - 1;
shouldDuplicateLast = firstEdge != PERPENDICULAR;
}
}
// Create the new polygon.
std::vector<units::Coordinate2D> newVertices;
newVertices.reserve(numVerts + (shouldDuplicateFirst ? 1 : 0) + (shouldDuplicateLast ? 1 : 0) );
for (std::size_t i = 0; i < numVerts; ++i) {
// Extend vertices in the region first-to-last inclusive. Duplicate first/last vertices if required.
if (i == first && shouldDuplicateFirst) {
newVertices.push_back(vertices_[i]);
newVertices.push_back(vertices_[i] + delta);
} else if (i == last && shouldDuplicateLast) {
newVertices.push_back(vertices_[i] + delta);
newVertices.push_back(vertices_[i]);
} else {
newVertices.push_back( isFirstEdgeAcute ? // Determine which range to use.
( (i <= last || i >= first) ? vertices_[i] + delta : vertices_[i] ) : // Range overlaps start/end of the array.
( (i <= last && i >= first) ? vertices_[i] + delta : vertices_[i] )); // Range is somewhere in the middle of the array.
}
}
return Polygon(newVertices);
}
So far I tested this code with triangles, rectangles, approximated circles, and arbitrary convex polygons made by extending the approximated circles sequentially by many different delta vectors.
Please note that this solution is still only valid for convex polygons.

Multi direction gravity 2D Platform Game - trigonometry issue

So, like the title said, I'm making a 2D platform game with multi direction physics, but my lack of trigonometry skills are killing me.
When my character hits something, it returns a Vector2 hitPoint.
If this was a one-way gravity platform game, I would do the following:
// considering the player's origin point in the middle
public void HitGround(Vector2 hitPoint)
{
this.position.y = hitPoint.y + height /2;
}
But since this have multi gravity physics, this simple method can't be done. So I've worked with relative positions, suppose the gravity is (-3, -3), the player would fall southwest.
// considering the player's origin point in the middle
public void HitGround(Vector2 hitPoint)
{
this.position = hitPoint + GetRelativeYPos(height / 2);
}
// returns the relative Y position based on its rotation
public Vector2 GetRelativeYPos(float offset)
{
// rotation is a Quaternion
return this.rotation * new Vector2(0, 1) * offset;
}
So far, so good. It works perfectly!!! But wait... this is only working when the CENTER of the player hits the ground. In fact, the player has 3 raycasts when he is falling. If the center raycast hits the ground, the code WORKS in EVERY direction he falls.
But when the EDGE RAYCASTS hits the ground. The Player tilts to the center, because the code do not allow him to move right. Now, my last code idea was born:
public void HitGround(Vector2 hitPoint, Vector2 raycastOrigin)
{
// Considering that the raycastOrigin is in the same "relative X" as the player's origin
float signedDistance = SignedDistance(hitPoint, raycastOrigin);
this.position = hitPoint + GetRelativeYPos(height / 2) + GetRelativeXPos(signedDistance);
}
// returns the relative Y position based on its rotation
public Vector2 GetRelativeYPos(float offset)
{
return this.rotation * new Vector2(0, 1) * offset;
}
// returns the relative X position based on its rotation
public Vector2 GetRelativeXPos(float offset)
{
return this.rotation * new Vector2(1, 0) * offset;
}
public float SignedDistance(Vector2 p1, Vector2 p2)
{
float distance = Distance(p1, p2);
float angle = Atan2(p2.y, p2.x) + Atan2(p1.y, p1.x);
// Returns the magnitude AND direction of the vector
return distance * Sign(angle);
}
And there you go!
When the code doesn't work, its just because of the sign of the angle, that, for some reason, it's being calculated wrong, When the angle has to be positive, it returns negative, and vice versa, but it only returns the wrong value when the "player's relative north position" is below 0.
For example:
if the player is falling down, when the Y position is higher than 0, it works, when it is lower, it doesn't.
If the player is falling up, when the Y position is lower than 0, it works, when it is higher, it doesn't.
If the player is falling right, when the X position is lower than 0, it works, when it is
higher, it doesn't.
If the player is falling left, when the X position is greater than 0, it works, when it is lower, it doesn't.
Just one more:
If the player is falling southeast, when the Y position is higher than 0 AND the X position is lower than 0, it works, otherwise, it doesn't
The following method is the responsible for calculating the "signed distance" of two points. I mean, the magnitude of the resulting vector and its direction
public float SignedDistance(Vector2 p1, Vector2 p2)
{
float distance = Distance(p1, p2);
// THIS LINE HERE!!!
float angle = Atan2(p2.y, p2.x) + Atan2(p1.y, p1.x);
return distance * Sign(angle);
}
There is something wrong with that line that I CAN'T FIGURE IT OUT. Is there someone kind enough to answer this question?
I hope I was clear. Bye

Detect when a 2D moving object crosses its own path?

How can I detect when a 2D moving object has crossed its own path?
I store the path as an array of points based on the plane's previous positions.
Pseudo-code or any programming language can be used to describe a solution.
Here's my code I've tried already - it detects a full 360 loop. I think I need a different approach.
CGFloat angleDiff = angleCurr - lastAngleRecorded;
lastAngleRecorded = angleCurr;
// Ensure -180 < angleDiff < 180
angleDiff = angleDiff > M_PI ? angleDiff - (M_PI*2) : angleDiff;
angleDiff = angleDiff < -M_PI ? angleDiff + (M_PI*2) : angleDiff;
// Reset tracking of the loop of the plane's angle exceeds (turns too sharply) or falls below the limits
if(fabsf(angleDiff) < angleDiffMinAllowed || fabsf(angleDiff) > angleDiffMaxAllowed) {
if(++ringFaultCount >= ringFaultCountMax) {
[self resetTracking];
return;
}
}
ringFaultCount = 0;
// Add plane position to ring polygon
[ringPoints addObject:[NSValue valueWithCGPoint: ccp(targetPlane.position.x, targetPlane.position.y)]];
// Add angleDiff to angleTotal
angleTotal += angleDiff;
// Completed loop?
if(fabsf(angleTotal) >= M_PI * 2.f) {
[self resetTracking];
if(isRingJoined){
CCLOG(#"%# RING COMPLETE", NSStringFromSelector(_cmd));
}
return;
}
I also had the problem, I solved it by making a straight line in a coordinate system:
y = mx+q±­tolerance
Let me explain:
The line is the tangent of the curve at the point you check if there is a collision this is the line the "aircraft" followed in that point.
The tolerance will make the line move a litlle bit up and also one a little bit down.
so you get 2 parralel lines witch can be seen as boundarys.
you also have to make a tolerance on the x-axis
The m is the direction of the line, its: tan(angle), the angle is the angle with the x-axis.
If all that is setup than you have to do this:
if(y_point < mx+q+tolerance && y_point> mx+q-tolerance && x_points > x-tolerance && x_point< x+tolerance
{
// some code
}

Resources