9

I am applying a force and a torque on an node. This is my code:

myNode?.physicsBody?.applyForce(SCNVector3Make(0, -6, 4), atPosition: SCNVector3Make(0, 1, -1), impulse: true)
myNode?.physicsBody?.applyForce(SCNVector3Make(0, -2, 10), impulse: true)
myNode?.physicsBody?.applyTorque(SCNVector4Make(4, 2, 2.5, 1.6), impulse: true)

The object now falls down and moves from left to right afterwards. I want it fall down and move from right to the left(basically a reflection of the first movement across y-axis). I figured it out that there is very little I can do about the first 2 lines of code, because the force has no x-component. The last line, applyTorque, is the one I need to manipulate. How do you map across the y-axis if the vector has 4 components? I am a little rusty with math

sagar.musale
  • 585
  • 1
  • 7
  • 19
potato
  • 4,479
  • 7
  • 42
  • 99
  • Good luck getting attention for Scene Kit stuff. It seems there's less than 100 people world-wide using it. A dozen of them might know what they're doing, 2 of whom used to use SO but have seemingly left. And there's no more help anywhere else that I've seen. I'm in the clueless and (obviously) confused experimental group ---- a group of 1. – Confused Sep 15 '15 at 13:41
  • A couple of guys floating around with strange user names and scores in the low 4 digits, sometimes leaving very abrupt answers to SceneKit questions appear to be Apple employees working on it. But their answers are barely more than pointers. They seem so familiar with their product they're incapable of explaining it. – Confused Sep 15 '15 at 13:43
  • I haven't used SK. From the physics view point, this sounds odd because you are trying to apply a torque to change the linear motion, unless you mean part of the object. – keithyip Sep 15 '15 at 16:37
  • The torque helps because when the objects hits another object, it rebounds based on how it hit it in the first place. – potato Sep 15 '15 at 16:41
  • I'm a little confused by your question. Something is falling, and then you want it to propel sideways, once it's reach the ground, right? And you're wanting that to happen via a rotational force on the fallen object... have I got this right? The ground is the primary other object? – Confused Sep 15 '15 at 22:06
  • If not, and you're wanting brief contact to cause the object to propel sideways on contact in accordance with the spin, you're going to need slightly "sticky" contact, and very high friction, along with enormous spin rates. One of the failings of physic's engines is that they're (mostly) very brittle compared to real life. So contacts are almost immeasurably short compared to the "softer & more flexible" real world in which contacts last longer and have transitional periods that impart spinning forces much better than in most physics engines. So two things... coming up after the word limit... – Confused Sep 15 '15 at 22:08
  • You may need to use the contact info to "fake" a longer contact period, and you might need to take advantage of that contact info to even fake the force you're wanting to impart based on your own custom calculation of what's happened at that contact to get a more reliable and less brittle realisation of your ideal physics world response, because massive spin rates might cause your objects to do the right thing during some edge case glances, and the wrong thing during more direct hits/contacts. So I'm suggesting a custom calculation to figure the angle for applyForce based on the collision info – Confused Sep 15 '15 at 22:12
  • Providing a trimmed sample project may help – keithyip Sep 16 '15 at 09:32

3 Answers3

6

The fuller version of the applyTorque function looks something like this:

.applyTorque(SCNVector4Make(x:, y:, z:, w:), impulse:)

So any numbers you put in the second position should be torque amounts around the y axis.

There's probably a relationship between the numbers and what they create in terms of rotational force on an object, but I've always just used trial-and-error to find what works. Sometimes it's HUGE numbers.

Confused
  • 6,048
  • 6
  • 34
  • 75
4

I am assuming that the x-axis is horizontal and the y-axis is vertical and the z-axis points straight at you (see the black arrows below):

enter image description here

I found evidence that this is indeed the case in SceneKit.

If

applyTorque(SCNVector4Make(x, y, z, w), impulse: boolean)

is the correct usage, then x is the amount of counter-clockwise rotation around the x-axis (see green circle arrow), and similarly for y and z. Again, this is my best guess, and it is possible that SceneKit uses clockwise rotation. Therefore, x, y, and z together determine the axis of rotation of the torsional force.

Here is a simpler way to think of it. x, y, and z create a vector in the 3D space described above. The object will rotate counter-clockwise around this vector.

w on the other hand, is the magnitude of the torque, and has nothing to do with the axis of rotation.

Your request to "map vector across the y-axis" is actually a reflection across the yz-plane. If you think about it, what you want is to rotate the opposite direction around the y-axis (negate y) and the same for z.

So the answer should be:

myNode?.physicsBody?.applyTorque(SCNVector4Make(4, -2, -2.5, 1.6), impulse: true)
mareoraft
  • 3,474
  • 4
  • 26
  • 62
  • 1
    I'm glad this works, but sorry to bother you @mareocraft, I'm confused. Is magnitude an amount of torque force (or impulse) and the numbers in the x,y,z positions sort of like "multipliers" to this value of magnitude? How does these x,y,z values actually work/do their thing? – Confused Sep 22 '15 at 16:27
  • @Confused Hopefully the edits to my answer along with everything on [this webpage](https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SceneKit_DataTypes/index.html#//apple_ref/doc/c_ref/SCNVector4) will answer your question. It says that x, y, and z are *normalized*, which means they will not affect the torsional force, only the axis of rotation. – mareoraft Sep 23 '15 at 04:02
  • Sorry, I'm more confused. If, as per the OP's question, he wants (for example) a ball to fall to the ground, and then rotate in such a way as to move from right to left, that requires the ball to be spun around the Z-axis with a force creating counter-clockwise rotation. Wouldn't that then mean that the X,Y, Z components are like switches, wherein (0,0,-1,10) applies 10 units of torque force around the Z axis in a negative direction? – Confused Sep 23 '15 at 05:34
  • Negating x, y, and z will make the axis of rotation point in the opposite direction. This would be the equivalent of negating the torque. That is, it would turn with the same force, but in the opposite direction. But in SceneKit, the axis of rotation cannot affect the *magnitude* of the torque. I'm sorry, but I cannot help you further. There are physics resources out there about torque that you can consult. – mareoraft Sep 25 '15 at 02:25
3

According to the SceneKit documentation the SCNVector4 argument specifies the direction (x, y, z vector components) and magnitude (w vector component) of the force in newton-meters. To mirror the direction of the applied torque, all you have to do is invert the magnitude. (x, y, z, -magnitude)

theB
  • 6,450
  • 1
  • 28
  • 38