I have a tilemap (Kobald Kit's KKTilemapNode) and a character in the middle, now I use joystick to move the tilemap up and down and calculate the speed, the problem I have is that if I turn the character left, it goes still just up and down as I don't know how to calculate the new position under an angle. I have tried to port some Flash examples but the ones I have found don't have tile maps :(
Edit (adding source):
rotationStep = (_velocity.x / 20); // Rotate the car by
if (fabs(_speed) > 0.3) {
_carRotation -= (rotationStep * (_speed / maxSpeed)); // Rotation angle if speed is at least 0.3
}
CGFloat speedX = ((_carRotation * (M_PI / 180)) * _speed); // Calculation stolen from the flash game and probably changed over tooo much
CGFloat speedY = ((_carRotation * (M_PI / 180)) * _speed);
CGFloat rotationValue = (degreesToRadians(_carRotation)); // Getting radians for the rotation
if (_velocity.x != 0 || _velocity.y != 0) {
_car.zRotation = rotationValue; // Setting correct rotation
}
NSLog(@"Speed X: %f - Y: %f", speedX, speedY); // Getting probably very incorrect values
[_track setPosition:CGPointMake(5, (_track.position.y - _speed))]; // And moving the tile map just up and down in the end based on a speed I have calculated earlier
One way to calculate the angle:
float angle = atan2f (touchY - joystickY, touchX - joystickX);
where touchY and touchX are the coordinates of where the player touched the joystick and joystickY and joystickX are the coordinates of the joystick. To move a node by that angle using a SKAction (you may not want to use actions but you can still use the maths to calculate the destination coordinates):
SKAction *moveWhatever = [SKAction moveByX: yourDistance*cosf(angle) y:yourDistance*sinf(angle) duration:yourDuration];
[node runAction: moveWhatever];
where you set yourDistance and yourDuration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.