1

I have a moving object that spawns object2 thats attached by a sliding joint

When i press the button , the joint is removed and the object2 falls down with gravity

this works, but only falls down on the x axis without retaining its y axis speed.

How can i make Object2 fall down in a more horizontal way like in real life

Scene Example

Jackster
  • 41
  • 7
  • You mean vertical, nevermind, I think you mean falls down on y axis without retaining x axis speed. so you want to move it horizontally – Knight0fDragon Jun 30 '16 at 20:34

2 Answers2

2

You can set velocity directly:

sprite.physicsBody?.velocity = CGVectorMake(10.0,10.0)

Just set the vx the same as the moving object.

Valar Morghulis
  • 647
  • 5
  • 16
  • this works, only now what is the code to determine the speed of a moving object? I can't put a constant value because object1 changes speed and direction. – Jackster Jun 30 '16 at 02:29
  • If you can set a sprite's velocity, I assume you can get it. Try with moveObject.physicsBody.velocity.x – Valar Morghulis Jun 30 '16 at 02:34
  • seems like its only outputting a velocity when interactive with the physics world. Ive got object1 moving with the SKAction.MoveToY command – Jackster Jun 30 '16 at 02:54
  • Hmm.... there might be a way to do it not so elegantly. For example, record two x1 and x2 of moving object during two calls of the `update` function. Then divide it by the time interval, to give an estimation of the initial velocity. – Valar Morghulis Jun 30 '16 at 04:43
0

use after the remove of the joint

object2.physicsBody?.velocity = object1.physicsBody.velocity
object2.physicsBody?.affectedByGravity = true

if you use moveToY take a look at this question

Community
  • 1
  • 1
2BC.Wasabi
  • 75
  • 6