applyForce to a node according to its rotation

Drakalex

I work with Xcode 6, SpriteKit and Swift,

I have a SKSpriteNode which is the default Spaceship image of a new Xcode project. This spaceship have a physicbody, but is not affected by the gravity, I want to simulate the space.

I want that when I tap the screen, a force is applied to the node so the spaceship can go forward. The problem is when the spaceship has rotated and I try to apply a force, the spaceship will not go forward like I want, but in the direction of the force applied, which is normal.

I want to know if there is any way to apply a force to a node according to its zRotation ? So no matter in which direction will be the spaceship, it will always go forward.

lchamp

I can't give it a try right now, but it should work :

// grab the spaceship rotation and add M_PI_2
var spaceShipRotation : CGFloat = spaceShipNode.zRotation
var calcRotation : Float = Float(spaceShipRotation) + Float(M_PI_2);

// cosf and sinf use a Float and return a Float
// however CGVector need CGFloat
let intensity : CGFloat = 10.0 // put your value
let xv = intensity * CGFloat(cosf(calcRotation))
let yv = intensity * CGFloat(sinf(calcRotation))
let vector : CGVector = CGVectorMake(xv, yv)

// apply force to spaceship
spaceShipNode.physicsBody?.applyForce(vector)

You have to work with math, or more precisely with trigonometry, which includes : PI, cos, sin, ... I'm not really confortable with all of that, especially without being to try right now, but as I've told it should work. Let me know !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

applyForce to a node according to its rotation

From Dev

How to auto resize the parent node according to its children

From Dev

How to get node bounds according to its specific ancestor in JavaFX 8?

From Dev

How do i return a node to its original position after a rotation transition

From Dev

Android - Moving view according to sensor rotation matrix

From Dev

Move object twoards its rotation

From Dev

Rotation value of image as its dragged

From Dev

Force layout node rotation

From Dev

Is SceneKit node rotation cumulative

From Dev

Color shape according to its text

From Dev

Log Rotation in Node.js?

From Dev

SpriteKit - Selecting Node After Rotation

From Dev

Stop node rotation in Sprite Kit

From Dev

Log Rotation in Node.js?

From Dev

AVL Rotation - Which node to rotate

From Dev

Fragment in ViewPager is not displaying anything in its RecyclerView on rotation

From Dev

choose document() according to node value

From Dev

Access a node value according to the attribute

From Java

Removing array object according to its property in Swift

From Dev

Sorting an array according to its Geolocation positions

From Dev

change subviews according to its superview frame

From Dev

Jqgrid Column width According to Its Content

From Dev

Jqgrid Editable Column width According to Its Content

From Dev

Styling Cell in DataGrid according to its value

From Dev

MySQL join and count according to its column value

From Dev

How to change the UIButton length according to its title

From Dev

sort function definitions according to its size

From Dev

change subviews according to its superview frame

From Dev

Splitting a list according to a partition of its length

Related Related

HotTag

Archive