2

I am making a basic app that pushes shapes across the screen and detects collision with Sprite Kit. My first attempt was using moveTo on the nodes. The issue I had was with collision, the objects would rotate around each other instead of bounce.

Therefore I found I need to use applyForce OR applyImpulse.

In this situation I have a circle for example that is position off screen at its start of life. We then determine a target exit point, and want to 'flick'/'push' the node in that direction.

I cannot figure out how to applyImpulse towards the target end position I have as a CGPoint. I need to get this to a CGVector but I am not sure what needs to be done. I had a look around and found some Ray tuts but they just show applyForce or moveTo. I am not sure how to calculate this.

StuartM
  • 6,743
  • 18
  • 84
  • 160
  • Are you asking what the dx and dy values and how they work? – sangony Apr 28 '15 at 21:48
  • Ok, what I am asking in short is... I have a starting point and an end target point. I want to move that node (via an applyImpulse) towards the target point. So I want to applyImpulse toward the target end point, then should it collide into anything on the way it will change direction. But I am not sure how to get the CGVector that the applyImpulse method takes from two points (start, target end). – StuartM Apr 28 '15 at 21:49
  • This is probably what you are looking for http://stackoverflow.com/questions/22157138/applyimpulse-towards-cgpoint-spritekit – sangony Apr 28 '15 at 21:53
  • Yeah I found that too, but unfortunately not. If you look at the rayW link from that answer, the Swift implementation no longer uses applyImpulse and instead uses moveTo. The moveTo would work fine in that situation where they are not moving once collision, but in this case it would not... – StuartM Apr 28 '15 at 21:55
  • Basically you would applyImpulse to your node's physicsBody like this [myNode.physicsBody applyImpulse:CGVectorMake(10, 10)]; This would push your node up and to the right. How you calculate the exact values for dx,dy are a math function which you can google. – sangony Apr 28 '15 at 22:00
  • Yes, I understand I could do that i.e. 10,10, but I want to specifically push it towards a certain point. So how would I translate that point into a CGVector. Also how would I control the speed the node moves towards the point? – StuartM Apr 28 '15 at 22:02
  • Thanks I had already, I'll have another look – StuartM Apr 28 '15 at 22:12

2 Answers2

1

I found a site that explains 2D physics well.
http://www.rodedev.com/tutorials/gamephysics/

With this I worked out what the angle needed to be and have a speed that I can control and it works well.

StuartM
  • 6,743
  • 18
  • 84
  • 160
0

You can move an object by changing manually the x and y position so you can reach your end point. In the update function you change yourObject.position.x and yourObject.position.y if I have understood correctly your question. If not please be more explicit. Hope that helps.

Eduard
  • 620
  • 9
  • 21
  • I thought I was explicit :) I understand I can move the position by x and y, and I can use moveTo but that will not work with collisions. So i need to apply an impulse to the object but cannot work out how to determine the dx and dy – StuartM Apr 28 '15 at 21:17
  • Oh, I'll try to figure it out. – Eduard Apr 28 '15 at 21:33