Velocity Verlet Algorithm: Cannot seem to determine correct velocity for stable Orbit? - objective-c

Like many that post about this topic, I too am busy trying to write myself an accurate simulator for the movement of objects in a 2D gravitation field.
I decided early on that I would settle on Velocity Verlet Integration, as I want my objects to maintain stable orbits and conserve energy even if the timestep is rather large. So, what might the problem be?
Well, so far, everything seems to behave correctly, except for one component. When I try to calculate the correct velocity for a stable orbit at a certain distance, the resulting velocity sends them into odd elliptical orbits that quickly increase in magnitude each time.
So, to begin, here are the following methods that determine an objects next position, velocity, and acceleration in scene: (Objective C)
Acceleration:
-(CGVector)determineAccelerationFor:(SKObject *)object
{ // Ok, let's find Acceleration!
CGVector forceVector = (CGVector){0,0}; // Blank vector that we will add forces to
for (SKObject *i in self.sceneObjects)
{
if (![i isEqual:object]) // Just make sure we're not counting ourselves here
{
CGPoint distance = [self getDistanceBetween:i.position And:object.position];
float hypotenuse = sqrtf(powf(distance.x, 2)+ powf(distance.y, 2));
float force = ((self.gravity * object.mass * i.mass)/powf(hypotenuse, 3));
float xMagnitude = (force * distance.x);
float yMagnitude = (force * distance.y);
forceVector.dx += xMagnitude;
forceVector.dy += yMagnitude;
}
}
CGVector acceleration = (CGVector){forceVector.dx/object.mass, forceVector.dy/object.mass};
return acceleration;
}
Cool, so basically, I just take an object, add all the other forces that each other object imposes on it together then divide the X & Y factor by the mass of the current object to get the acceleration!
Next up is Velocity. Here I use the following equation:
The method for it is pretty straightforward too:
-(CGVector)determineVelocityWithCurrentVelocity:(CGVector)v OldAcceleration:(CGVector)ao NewAcceleration:(CGVector)a
{
float xVelocity = (v.dx + ((ao.dx + a.dx)/2) * self.timeStep);
float yVelocity = (v.dy + ((ao.dy + a.dy)/2) * self.timeStep);
CGVector velocity = (CGVector){xVelocity,yVelocity};
return velocity;
}
And finally, position! The equation for this is:
And it is determined with the following method!
-(CGPoint)determinePositionWithCurrentPosition:(CGPoint)x CurrentVelocity:(CGVector)v OldAcceleration:(CGVector)ao
{
float xPosition = (x.x + v.dx * self.timeStep + ((ao.dx * powf(self.timeStep, 2))/2));
float yPosition = (x.y + v.dy * self.timeStep + ((ao.dy * powf(self.timeStep, 2))/2));
CGPoint position = (CGPoint){xPosition,yPosition};
return position;
}
This is all called from the below method!!
-(void)refreshPhysics:(SKObject *)object
{
CGPoint position = [self determinePositionWithCurrentPosition:object.position CurrentVelocity:object.velocity OldAcceleration:object.acceleration]; // Determine new Position
SKAction *moveTo = [SKAction moveTo:position duration:0.0];
[object runAction:moveTo]; // Move to new position
CGVector acceleration = [self determineAccelerationFor:object]; // Determine acceleration off new position
CGVector velocity = [self determineVelocityWithCurrentVelocity:object.velocity OldAcceleration:object.acceleration NewAcceleration:acceleration];
NSLog(#"%# Old Velocity: %f, %f",object.name,object.velocity.dx,object.velocity.dy);
NSLog(#"%# New Velocity: %f, %f\n\n",object.name,velocity.dx,velocity.dy);
[object setAcceleration:acceleration];
[object setVelocity:velocity];
}
Okay, so those methods above dictate how objects are moved in scene. Now onto the initial issue, the ever present problem of achieving a stable orbit!
In order to determine what velocity an object should have to maintain an orbit, I use the following equation:
And I implement that as follows:
-(void)setObject:(SKObject *)object ToOrbit:(SKObject *)parent
{
float defaultSeparation = 200;
// Move Object to Position at right of parent
CGPoint defaultOrbitPosition = (CGPoint){parent.position.x + (parent.size.width/2)+ defaultSeparation,parent.position.y};
[object setPosition:defaultOrbitPosition];
// Determine Orbital Velocity
float velocity = sqrtf((self.gravity * parent.mass)/(parent.size.width/2+defaultSeparation));
CGVector vector = (CGVector){0,velocity};
[object setVelocity:vector];
}
And for some reason, despite this, I get abysmal results. Here is some of the output:
Information:
Gravity(constant) = 1000 (For test purposes)
Mass(Parent) = 5000 units
Mass(Satellite) = 1 units
Separation = 224 pixels
It determines that in order for the Satellite to Orbit the Parent, a velocity of:
149.403580 pixels/timeStep
is required. And that checks out on my calculator.
So this has left me a little confused as to what could be going wrong. I log all the output concerning new velocities and positions, and it does use the velocity I set it to, but that just doesn't seem to make a difference. If anyone could possible help spot what's going wrong here I would be immensely grateful.
If anyone believes I have left something out, tell me and I will edit this right away. Thanks!

Related

Check if a physicsbody is touching another without waiting for didBeginContact

I know you can detect contact collisions using SKPhysicsContactDelegate, but can you check if a physicsbody is currently touching another physicsbody?
I need this for checking which area in the scene is still available to put an item (eg. pick a random spot, and if there's something in the way, pick another random spot).
There's this function:
/* Returns an array of all SKPhysicsBodies currently in contact with this one */
- (NSArray *)allContactedBodies;
But it doesn't appear to return anything useful until after the next update of creating the node.
You can write a function to iterate manually through all the nodes and check if the two circles intersect a point.
Since you said that the radius of the circles will differ each time, you have to keep track of it. One method is to use the user data of the node.
[node.userData setObject:[NSNumber numberWithFloat:10.0] forKey:#"radius"];
Then you can find if there are intersecting circles in the following way.
-(BOOL)checkPointForNode:(CGPoint)point withRadius:(CGFloat)nodeRadius
{
for (SKNode* child in [self children])
{
NSNumber *childRadius = child.userData[#"radius"];
if (childRadius != nil)
{
CGFloat diffX = point.x - child.position.x;
CGFloat diffY = point.y - child.position.y;
CGFloat distance = sqrtf(diffX * diffX + diffY * diffY);
CGFloat sumRadius = nodeRadius + childRadius.floatValue;
if (distance <= sumRadius)
{
return YES;
}
}
}
return NO;
}
The function returns YES if there is a circle within the boundary of the circle you are going to add. This means you cannot add a new node without touching another node. Otherwise it returns NO. This means you can add a new node without touching any other nodes.

Find point on the perimeter of a rectangle in Objective-C (Sprite Kit)

I want to move an object from a random point just outside of the view in a Sprite Kit game.
The logical way of doing this would be to create a rectangle 100px (example) bigger than the view, and pick a random point on it's perimeter. Unfortunately, I don't know an easy way to do this.
How can I easily create a random point on the perimeter of a rectangle (which is slightly bigger than my view)?
Update
This should do what you want:
- (CGPoint)randomPointOutsideRect:(CGRect)rect withOffset:(CGFloat)offset {
NSUInteger random = arc4random_uniform(4);
UIRectEdge edge = 1 << random; // UIRectEdge enum values are defined with bit shifting
CGPoint randomPoint = CGPointZero;
if (edge == UIRectEdgeTop || edge == UIRectEdgeBottom) {
randomPoint.x = arc4random_uniform(CGRectGetWidth(rect)) + CGRectGetMinX(rect);
if (edge == UIRectEdgeTop) {
randomPoint.y = CGRectGetMinY(rect) - offset;
}
else {
randomPoint.y = CGRectGetMaxY(rect) + offset;
}
}
else if (edge == UIRectEdgeLeft || edge == UIRectEdgeRight) {
randomPoint.y = arc4random_uniform(CGRectGetHeight(rect)) + CGRectGetMinY(rect);
if (edge == UIRectEdgeLeft) {
randomPoint.x = CGRectGetMinX(rect) - offset;
}
else {
randomPoint.x = CGRectGetMaxX(rect) + offset;
}
}
return randomPoint;
}
This should be fairly straightforward, let me know if there's something unclear.
Basically, we pick one edge at random, then "fix" one axis and pick a random value on the other (within the width/height boundaries).
arc4random_uniform gives us only integers, but that's fine because floating point values in frames are bad when displaying stuff on screen.
There is probably a shorter way to write this; feel free to edit to improve, everyone.
Original answer
How can I easily create a point 100 pixels away from the edge of my view?
With CGRectOffset().
Assuming you want a CGPoint 100pt "higher" (ie. with a lower y) than your view, do:
CGRect viewFrame = // lets say for this example that your frame is at {{20, 40}, {300, 600}}
CGRect offsetFrame = CGRectOffset(viewFrame, 0, -100);
CGPoint offsetPoint = offsetFrame.origin
// offsetPoint = {20, -60}

Box2d Physics Simulation Stutter

I have some code that causes the box2d physics simulation to stutter forever after this code is called once. I ran a performance test on it and OSSpinLockLock has a lot of time spent on it. b2Island::Solve and some other box2d functions are causing this to take up so much time. I'm not really sure what is going on here. The effects of this seem to be worse on maps with more overlapping static bodies then maps with less overlapping static bodies. The code that causes the stutter is the following:
for (b2Body*b = currentWorld->GetBodyList(); b!=nil; b = b->GetNext()) {
float distance = 0;
float strength = 0;
float force = 0;
float angle = 0;
CGPoint curPos = [Helper toPixels:b->GetWorldCenter()];
distance = ccpDistance(selfPos, curPos);
if (distance>maxDistance) {
distance = maxDistance - 0.01;
}
//strength = (maxDistance - distance) / maxDistance;
strength = (distance - maxDistance)/maxDistance;
force = strength * maxForce;
angle = atan2f(selfPos.y - curPos.y, selfPos.x - curPos.x);
//b2Vec2 forceVec = [Helper toMeters:force];
//CCLOG(#"strength = %f force = %f angle = %f distance = %f",strength,angle,force, distance);
b->ApplyLinearImpulse(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
BodyNode * bn = (BodyNode*)b->GetUserData();
if ([bn isKindOfClass:[Soldier class]]) {
((Soldier*)bn).health-=abs(force*(damage * kRocketDamageFactor)); //used to be 50
}
if ([bn isKindOfClass:[BaseAI class]]) {
((BaseAI*)bn).health-=abs(force*(damage * kRocketDamageFactor));
}
if ([bn isKindOfClass:[BaseLevelObject class]]) {
if (((BaseLevelObject*)bn).takesDamageFromBullets == YES) {
((BaseLevelObject*)bn).health-=abs(force*(damage * kRocketDamageFactor));
}
}
I suspect that the ApplyLinearImpulse part is what is causing the stutter because it is the only thing related to physics. I just commented out this whole loop and the stutter never came. But as soon as I exploded some other explosives with the same code, the stutter came. Do overlapping static b2body objects slow the physics simulation down?
The issue was caused by the code giving all the bodies that were farther away than the max distance a small tap towards the explosion. I'm not really sure why this would cause the physics simulation to stutter, but adding a continue statement in place of distance = maxDistance - 0.01; solved the problem.

UIView Delta Time

I'm animating a UIView by updating its anchorpoint 60 times a second using an NSTimer.
The location of the UIView changes depending on its angle, so it always appears to be down relative to the device...
However, the NSTimer doesn't fire precisely 60 times a second. It's always a little off, causing jerky animation. I've searched this a lot, I know a bit about delta time, but I don't know how to apply it to my situation.
Here's the movement code I'm using:
float rotation = 0;
if (leftSideIsBeingHeldDown) {
rotation += (0.05f/rotationFactor);
} else if (rightSideIsBeingHeldDown) {
rotation -= (0.05f/rotationFactor);
}
movementX += -sinf(rotation);
movementY += -cosf(rotation);
float finalX = 0.0001 * movementX;
float finalY = 0.0001 * movementY;
mapView.layer.anchorPoint = CGPointMake(finalX, finalY);
mapView.transform = CGAffineTransformMakeRotation(rotation);
Does anyone know how to apply delta time to this?
You might want to look into the CADisplayLink class which provides you a timer that is tied to the display refresh rate. It should be a better solution than an NSTimer in this case.
Additionally, you need to remember the time of each "tick" and calculate the rotation or movement that should have been done since the last tick. For example (pseudo-code):
- (void)displayLinkTick:(id)sender
{
NSTimeInterval timespan;
NSDate *now;
now = [NSDate date];
if (myPreviousTick) {
timespan = [now timeintervalSinceDate:myPreviousTick];
} else {
// The very first tick.
timespan = 0;
}
// Calculate the angle according to the timespan. You need a
// value that specifies how many degrees/radians you want to
// revolve per second and simply multiply that with the timespan.
angle += myRadiansPerSecond * timespan;
// You'd do the same with the position. I guess this involves
// minor vector math which I don't remember right now and am
// too lazy to look up. You need to have a distance per second
// which you multiply with the timespan. Together with the
// direction vector you can calculate the new position.
// At the end, remember when this tick ran.
[myPreviousTick release];
myPreviousTick = [now retain];
}
You want to record the time you last rotated, and the difference in time between then and now, and use that to work out a factor, which you can use to adjust the rotation and x/y values.
for example:
NSDate now = [NSDate now];
timeDiff = now - lastRotateTime;
factor = timeDiff / expectedTimeDiff;
x = x + xIncrement * factor;
y = y + yIncrement * factor;
angle = angle + angleIncrement * factor;
There are many better examples on game dev forums, which explain it in more detail.

know the position of the finger in the trackpad under Mac OS X

I am developing an Mac application and I would like to know the position of the finger in the trackpad when there is a touch.
Is it something possible and if yes, how?
Your view needs to be set to accept touches ([self setAcceptsTouchEvents:YES]). When you get a touch event like -touchesBeganWithEvent:, you can figure out where the finger lies by looking at its normalizedPosition (range is [0.0, 1.0] x [0.0, 1.0]) in light of its deviceSize in big points (there are 72 bp per inch). The lower-left corner of the trackpad is treated as the zero origin.
So, for example:
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (!self) return nil;
/* You need to set this to receive any touch event messages. */
[self setAcceptsTouchEvents:YES];
/* You only need to set this if you actually want resting touches.
* If you don't, a touch will "end" when it starts resting and
* "begin" again if it starts moving again. */
[self setWantsRestingTouches:YES]
return self;
}
/* One of many touch event handling methods. */
- (void)touchesBeganWithEvent:(NSEvent *)ev {
NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseBegan inView:self];
for (NSTouch *touch in touches) {
/* Once you have a touch, getting the position is dead simple. */
NSPoint fraction = touch.normalizedPosition;
NSSize whole = touch.deviceSize;
NSPoint wholeInches = {whole.width / 72.0, whole.height / 72.0};
NSPoint pos = wholeInches;
pos.x *= fraction.x;
pos.y *= fraction.y;
NSLog(#"%s: Finger is touching %g inches right and %g inches up "
#"from lower left corner of trackpad.", __func__, pos.x, pos.y);
}
}
(Treat this code as an illustration, not as tried and true, battle-worn sample code; I just wrote it directly into the comment box.)
Swift 3:
I've written an extension to NSTouch that returns the trackpad-touch pos, relative to an NSView:
extension NSTouch {
/**
* Returns the relative position of the touch to the view
* NOTE: the normalizedTouch is the relative location on the trackpad. values range from 0-1. And are y-flipped
* TODO: debug if the touch area is working with a rect with a green stroke
*/
func pos(_ view:NSView) -> CGPoint{
let w = view.frame.size.width
let h = view.frame.size.height
let touchPos:CGPoint = CGPoint(self.normalizedPosition.x,1 + (self.normalizedPosition.y * -1))/*flip the touch coordinates*/
let deviceSize:CGSize = self.deviceSize
let deviceRatio:CGFloat = deviceSize.width/deviceSize.height/*find the ratio of the device*/
let viewRatio:CGFloat = w/h
var touchArea:CGSize = CGSize(w,h)
/*Uniform-shrink the device to the view frame*/
if(deviceRatio > viewRatio){/*device is wider than view*/
touchArea.height = h/viewRatio
touchArea.width = w
}else if(deviceRatio < viewRatio){/*view is wider than device*/
touchArea.height = h
touchArea.width = w/deviceRatio
}/*else ratios are the same*/
let touchAreaPos:CGPoint = CGPoint((w - touchArea.width)/2,(h - touchArea.height)/2)/*we center the touchArea to the View*/
return CGPoint(touchPos.x * touchArea.width,touchPos.y * touchArea.height) + touchAreaPos
}
}
Here is an article I wrote about my GestureHUD class in macOS. With link to a ready-made extension as well: http://eon.codes/blog/2017/03/15/Gesture-HUD/
Example:
I don't know if there's an ObjC interface, but you might find the C HID Class Device Interface interesting.
At a Cocoa (Obj-C level) try the following - although remember that many users are still using mouse control.
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html