I have a simple logic for my 2d game that is enabled on the iphone with the orientation locked to Landscape Left.
If the phone is rotate away from you, imagine holding it in landscape mode, I want my object to be moved up otherwise I want the object to be moved down.
-(void)outputRotationData:(CMRotationRate)rotation
{
if (fabs(currentMaxRotY - rotation.y) < .3) {
return;
}
if(rotation.y > 0 ){
if (!(ship.position.y < 10)) {
[ship runAction:actionMoveDown];
NSLog(#"moving down");
}
}else{
if (!(ship.position.y > screenHeight)) {
[ship runAction:actionMoveUp];
NSLog(#"moving up");
}
}
currentMaxRotY = rotation.y;
}
The code above works. Just not as you would expect. Even when the phone is rotated away from you, sometimes the object moves down; however it correctly moves up most of the times. I assume due to the sensitivity which I thought would have been fixed by returning if our last rotation and current rotation don't differ by more than .3
As #LearnCocos2D mentioned in a comment, gyro is not what we want to use here.
The code below will place the object at a location based on a the angle.
I ended up not using this either and just use tap to move!
-(void)outputAccelertionData:(CMAcceleration)acceleration
{
double x, y, z, angle;
x = roundThree(acceleration.x);
y = roundThree(acceleration.y);
z = roundThree(acceleration.z);
angle = atan(x/z);
angle = [self RadiansToDegrees:angle];
if (z == 0) {
angle = 90;
}
if (angle < 40) {
[self runThisAction:angle];
}else if (angle > 50){
[self runThisAction:-angle/2];
}
}
Related
I am trying to make a platform game for the iphone, using cocos2d and Tiled (for the maps).
All of the tutorials i've seen on the net are using Layers in Tiled to do collision detection.
I want to use objects to do that...not Layers.
With objects you can create custom shapes that can give a better 'reality' into the game.
To give an example of what i mean :
I've drawn the ground as a background and created an object layer on top.
I want to detect player collision with that, instead of the background tile.
Now using the most famous tutorial out there : http://www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1
I am trying to rewrite checkForAndResolveCollisions method to check collisions for the objects instead.
The problem is that in Tiled the coordinates system is different than cocos2d. Tiled starts from top left corner, cocos2d from bottom left corner....and not only that...I noticed that the width and height of the object properties in Tiled (probably) dont correspond to the same in iphone devices.
The above rectangle has properties:
its w/h is 480/128 in tiled (for retina devices) which means its probably huge inside the map if i keep them like this. My guess is i have to divide this by 2.
So far i got this:
-(void)checkForAndResolveObjCollisions:(Player *)p {
CCTiledMapObjectGroup *objectGroup = [map objectGroupNamed:#"Collision"];
NSArray* tiles = [objectGroup objects];
CGFloat x, y, wobj, hobj;
for (NSDictionary *dic in tiles) {
CGRect pRect = [p collisionBoundingBox]; //3
x = [[dic valueForKey:#"x"] floatValue];
y = [[dic valueForKey:#"y"] floatValue];
wobj = [[dic valueForKey:#"width"] floatValue];
hobj = [[dic valueForKey:#"height"] floatValue];
CGPoint position = CGPointMake(x, y);
CGPoint objPos = [self tileForPosition:position];
CGRect tileRect = CGRectMake(objPos.x, objPos.y, wobj/2, hobj/2);
if (CGRectIntersectsRect(pRect, tileRect)) {
CCLOG(#"INTERSECT");
CGRect intersection = CGRectIntersection(pRect, tileRect);
NSUInteger tileIndx = [tiles indexOfAccessibilityElement:dic];
if (tileIndx == 0) {
//tile is directly below player
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height);
p.velocity = ccp(p.velocity.x, 0.0);
p.onGround = YES;
} else if (tileIndx == 1) {
//tile is directly above player
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y - intersection.size.height);
p.velocity = ccp(p.velocity.x, 0.0);
} else if (tileIndx == 2) {
//tile is left of player
p.desiredPosition = ccp(p.desiredPosition.x + intersection.size.width, p.desiredPosition.y);
} else if (tileIndx == 3) {
//tile is right of player
p.desiredPosition = ccp(p.desiredPosition.x - intersection.size.width, p.desiredPosition.y);
} else {
if (intersection.size.width > intersection.size.height) {
//tile is diagonal, but resolving collision vertially
p.velocity = ccp(p.velocity.x, 0.0);
float resolutionHeight;
if (tileIndx > 5) {
resolutionHeight = -intersection.size.height;
p.onGround = YES;
} else {
resolutionHeight = intersection.size.height;
}
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + resolutionHeight );
} else {
float resolutionWidth;
if (tileIndx == 6 || tileIndx == 4) {
resolutionWidth = intersection.size.width;
} else {
resolutionWidth = -intersection.size.width;
}
p.desiredPosition = ccp(p.desiredPosition.x + resolutionWidth , p.desiredPosition.y);
}
}
}
// }
}
p.position = p.desiredPosition; //8
}
- (CGPoint)tileForPosition:(CGPoint)p
{
NSInteger x = (NSInteger)(p.x / map.tileSize.width);
NSInteger y = (NSInteger)(((map.mapSize.height * map.tileSize.width) - p.y) / map.tileSize.width);
return ccp(x, y);
}
I am getting the object x,y,w,h and try to convert them to cocos2d dimensions and sizes.
The above translates to this:
Dimens: 480.000000, 128.000000
Coord: 0.000000, 40.000000
Basically its a mess. And its not working .....at all. The player just falls right through.
I am surprised noone has done collision detection based on objects before...unless i am wrong.
Does anyone know if this can be done or how it can be done ?
Kinda what he does here : https://www.youtube.com/watch?feature=player_detailpage&v=2_KB4tOTH6w#t=30
Sorry for the long post.
Thanks for any answers in advance.
How can I achieve Tinder effect in Swift?
I mean, I have an image and want to accept if I swipe to right and reject if I swipe to left.
I can do it with the code bellow:
#IBAction func SwipeRight(sender: UISwipeGestureRecognizer) {
UIView.animateWithDuration(1) {
self.Imagem.center = CGPointMake(self.Imagem.center.x - 150, self.Imagem.center.y )
}
//other things after acception
}
and
#IBAction func SwipeLeft(sender: UISwipeGestureRecognizer) {
UIView.animateWithDuration(1) {
self.Imagem.center = CGPointMake(self.Imagem.center.x + 150, self.Imagem.center.y )
}
//other things after rejection
}
But this way the user can't cancel the action. I want that if the user swipes to a delta distance from the edge (left or right), it would appear an image to let the user now that, if he ends the movement, the action will take place. Otherwise, the user can, without ending the movement, go back to a distance bigger than delta, and the action would be cancelled.
I would like to thanks the people who suggested solutions. Follow the solution I developed with a huge help of a lot of people from Stack Overflow:
#IBAction func Arrastei(sender: UIPanGestureRecognizer) {
var origem = CGPoint(x: 0, y: 0)
var translation : CGPoint = sender.translationInView(Imagem)
var txy : CGAffineTransform = CGAffineTransformMakeTranslation(translation.x, -abs(translation.x) / 15)
var rot : CGAffineTransform = CGAffineTransformMakeRotation(-translation.x / 1500)
var t : CGAffineTransform = CGAffineTransformConcat(rot, txy);
Imagem.transform = t
if (translation.x > 100) {
LbResultado.textColor = btVerdadeiro.textColor
LbResultado.text = btVerdadeiro.text
LbResultado.hidden = false
} else {
if (translation.x < -100) {
LbResultado.textColor = btFalso.textColor
LbResultado.text = btFalso.text
LbResultado.hidden = false
} else {
LbResultado.hidden = true
}
}
if sender.state == UIGestureRecognizerState.Ended {
if (translation.x > 100) {
objJogo.Rodada_Vigente!.Responder(true)
} else {
if (translation.x < -100) {
objJogo.Rodada_Vigente!.Responder(false)
} else {
sender.view.transform = CGAffineTransformMakeTranslation(origem.x, origem.y)
sender.view.transform = CGAffineTransformMakeRotation(0)
}
}
}
}
This solution uses:
Imagem --> UIImageView - to be accepted or rejected
LbResultado --> UITextView - to show the user he is in acceptance or rejection area
There is no math calculations to set rotation and translation. I used values that give me a visually good effect.
The action (acceptance and rejection) area is when the user drag image more than 100 pixels to left (reject) or right (accept). If the user ends the movement out the action area, the image will go back to its original position.
I will be glad if someone suggests improvements to this code.
It's better to use UIPanGestureRecognizer here and manage its states, as you've already figured out. For managing cards it would be good solution to create class-manager that will handle interactions between cards(moving background cards on swiping the front). You can look at the implementation of card and manager here, there are implementation of dragging, moving background cards and revert animations.
https://github.com/Yalantis/Koloda
Try this:
https://github.com/cwRichardKim/TinderSimpleSwipeCards
You can find a better solution here with rotation. See DraggableView.m
-(void)beingDragged:(UIPanGestureRecognizer *)gestureRecognizer
{
//%%% this extracts the coordinate data from your swipe movement. (i.e. How much did you move?)
xFromCenter = [gestureRecognizer translationInView:self].x; //%%% positive for right swipe, negative for left
yFromCenter = [gestureRecognizer translationInView:self].y; //%%% positive for up, negative for down
//%%% checks what state the gesture is in. (are you just starting, letting go, or in the middle of a swipe?)
switch (gestureRecognizer.state) {
//%%% just started swiping
case UIGestureRecognizerStateBegan:{
self.originalPoint = self.center;
break;
};
//%%% in the middle of a swipe
case UIGestureRecognizerStateChanged:{
//%%% dictates rotation (see ROTATION_MAX and ROTATION_STRENGTH for details)
CGFloat rotationStrength = MIN(xFromCenter / ROTATION_STRENGTH, ROTATION_MAX);
//%%% degree change in radians
CGFloat rotationAngel = (CGFloat) (ROTATION_ANGLE * rotationStrength);
//%%% amount the height changes when you move the card up to a certain point
CGFloat scale = MAX(1 - fabsf(rotationStrength) / SCALE_STRENGTH, SCALE_MAX);
//%%% move the object's center by center + gesture coordinate
self.center = CGPointMake(self.originalPoint.x + xFromCenter, self.originalPoint.y + yFromCenter);
//%%% rotate by certain amount
CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
//%%% scale by certain amount
CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
//%%% apply transformations
self.transform = scaleTransform;
[self updateOverlay:xFromCenter];
break;
};
//%%% let go of the card
case UIGestureRecognizerStateEnded: {
[self afterSwipeAction];
break;
};
case UIGestureRecognizerStatePossible:break;
case UIGestureRecognizerStateCancelled:break;
case UIGestureRecognizerStateFailed:break;
}
}
Right now I'm trying to implement a pinch-to-zoom feature in my Cocos2D game for iOS and I'm encountering really strange behavior. My goal is to use the handler for UIPinchGestureRecognizer to scale one of the CCNodes that represents the game level when a player pinches the screen. This has the effect of zooming.
The issue is if I set the anchor for zooming to some arbitrary value such as .5, .5 (the center of the level CCNode) it scales perfectly around the center of the level, but I want to scale around the center of the player's view. Here is what that looks like:
- (void) handlePinchFrom:(UIPinchGestureRecognizer*) recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded)
{
_isScaling = false;
_prevScale = 1.0;
}
else
{
_isScaling = true;
float deltaScale = 1.0 - _prevScale + recognizer.scale;
// Obtain the center of the camera.
CGPoint center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2);
CGPoint worldPoint = [self convertToWorldSpace:center];
CGPoint areaPoint = [_area convertToNodeSpace:worldPoint];
// Now set anchor point to where areaPoint is relative to the whole _area contentsize
float areaLocationX = areaPoint.x / _area.contentSize.width;
float areaLocationY = areaPoint.y / _area.contentSize.height;
[_area moveDebugParticle:areaPoint];
[_area setAnchorPoint:CGPointMake(areaLocationX, areaLocationY)];
if (_area.scale*deltaScale <= ZOOM_RADAR_THRESHOLD)
{
_area.scale = ZOOM_RADAR_THRESHOLD;
}
else if (_area.scale*deltaScale >= ZOOM_MAX)
{
_area.scale = ZOOM_MAX;
}
else
{
// First set the anchor point.
_area.scale *= deltaScale;
}
_prevScale = recognizer.scale;
}
}
If I set the anchor point to .5, .5 and print the calculated anchor point (areaLocationX, areaLocationY) using a CCLog it looks right, but when I set the anchor point to these values the layer scales out of control and entirely leaves the view of the player. The anchor point takes on crazy values like (-80, 10), although generally it should be relatively close to something in the range of 0 to 1 for either coordinate.
What might be causing this kind of behavior?
Ok it looks like I solved it. I was continually moving the anchor point -during- the scaling rather than setting it at the very beginning once. The result was really erratic scaling rather than something smooth and expected. The resolved code looks like this:
- (void) handlePinchFrom:(UIPinchGestureRecognizer*) recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded)
{
_isScaling = false;
_prevScale = 1.0;
}
else
{
if (!_isScaling)
{
// Obtain the center of the camera.
CGPoint center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2);
CGPoint areaPoint = [_area convertToNodeSpace:center];
// Now set anchor point to where areaPoint is relative to the whole _area contentsize
float anchorLocationX = areaPoint.x / _area.contentSize.width;
float anchorLocationY = areaPoint.y / _area.contentSize.height;
[_area moveDebugParticle:areaPoint];
[_area setAnchorPoint:CGPointMake(anchorLocationX, anchorLocationY)];
CCLOG(#"Anchor Point: (%f, %f)", anchorLocationX, anchorLocationY);
}
_isScaling = true;
float deltaScale = 1.0 - _prevScale + recognizer.scale;
if (_area.scale*deltaScale <= ZOOM_RADAR_THRESHOLD)
{
_area.scale = ZOOM_RADAR_THRESHOLD;
}
else if (_area.scale*deltaScale >= ZOOM_MAX)
{
_area.scale = ZOOM_MAX;
}
else
{
_area.scale *= deltaScale;
}
_prevScale = recognizer.scale;
}
}
I am trying to check whether my SKSpriteNode will remain in bounds of the screen during a drag gesture. I've gotten to the point where I am pretty sure my logic toward approaching the problem is right, but my implementation is wrong. Basically, before the player moves from the translation, the program checks to see whether its in bounds. Here is my code:
-(CGPoint)checkBounds:(CGPoint)newLocation{
CGSize screenSize = self.size;
CGPoint returnValue = newLocation;
if (newLocation.x <= self.player.position.x){
returnValue.x = MIN(returnValue.x,0);
} else {
returnValue.x = MAX(returnValue.x, screenSize.width);
}
if (newLocation.y <= self.player.position.x){
returnValue.y = MIN(-returnValue.y, 0);
} else {
returnValue.y = MAX(returnValue.y, screenSize.height);
}
NSLog(#"%#", NSStringFromCGPoint(returnValue));
return returnValue;
}
-(void)dragPlayer: (UIPanGestureRecognizer *)gesture {
CGPoint translation = [gesture translationInView:self.view];
CGPoint newLocation = CGPointMake(self.player.position.x + translation.x, self.player.position.y - translation.y);
self.player.position = [self checkBounds:newLocation];
}
For some reason, my player is going off screen. I think my use of the MIN & MAX macros may be wrong, but I am not sure.
Exactly, you mixed up MIN/MAX. The line MIN(x, 0) will return the lower value of x or 0, meaning the result will be 0 or less.
At one line you're using -returnValue.y which makes no sense.
You can (and should for readability) omit the if/else because MIN/MAX, if used correctly, make if/else unnecessary here.
I need to calculate "facing" (it doesn't matter if it will be based on true north or magnetic one). As it can be seen on the iOS devices the CLHeading objects returned by the CLLocationManager gives us both the true and the magnetic heading by corresponding properties. Also, we can very easily see, that those values are related to the top of the device (the positive Y axis of the devices coordinate system) which is not good for my purposes.
What I actually need is to calculate the facing related to the screen of the device (Z axis) as I don't need the compass, but a king of AG application. The issue is when you rotate the device to landscape you get heading values to the left or to the right from your facing direction, which is what I need in the end.
As I know, I can get the magnetometer "raw" data (given to me in microtesla units with values from 128 to -128 for each device axis) along with the gyroscope "raw" data ( which comes in three types: Euler angels, Rotation matrix or Quaternion). What I need is to know, which calculations I need to apply to those to get the "facing" direction instead of "heading".
I've made it a while ago and because I see no answers, I've decided to put my solution here for those who'll search answer for the same question...
_motionManager = [[CMMotionManager alloc]init];
if (_motionManager.gyroAvailable) {
_motionManager.deviceMotionUpdateInterval = 1.0/20.0;
[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
CMAcceleration gravity = motion.gravity;
CGPoint tiltVector = CGPointMake(-gravity.x, -gravity.y);
_tiltAngle = [self angleYAxisToVector:tiltVector];
CLLocationDirection heaqding = [[SVSession sharedSession] heading].trueHeading;
double newHeading = fmod(heaqding + _tiltAngle, 360.0);
self.azimuth = degreesToRadian(newHeading);
[self updateLocations]; //this function updates my ui for the new heading
}];
} else {
NSLog(#"No gyroscope on device.");
[_motionManager release],_motionManager = nil;
}
And here are some additional snippets that may help to understand this example:
-(double)angleYAxisToVector:(CGPoint)vector{
double dX = vector.x;
double dY = vector.y;
if(dY == 0){
if(dX > 0){
return 0.0;
}else{
if(dX < 0){
return 180.0;
}else{
return -1;
}
}
}
double beta = radiansToDegrees(atan(dX/dY));
double angle;
if(dX > 0){
if (dY < 0){
angle = 180 + beta;
}else{
angle = beta;
}
}else{
if (dY < 0){
angle = 180 + beta;
}else{
angle = 360 + beta;
}
}
// NSLog(#"angle = %f, normalized = %f",beta,angle);
return angle;
}
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radiansToDegrees(x) ((x) * 180.0 / M_PI)
#define degreesToRadians(x) degreesToRadian(x)
#define radiansToDegree(x) radiansToDegrees(x)
Happy coding...