So I want to fire a beam of laser from the enemy to the player.
so the laser sweeps from left to right. So i use the zrotation as seen below
As you can see, the frame of the player is intersecting the laserbeam. How do i overcome this problem such that i can detect the collision of laser image and the player image?
Related
I have a node with a SCNTube as its physics body and another set of nodes that cover the tube on top and bottom with SCNPlanes. Whenever a ball collides with the tube they lose most of the energy they had. If they hit the top or bottom they bounce off no problem. The balls have friction, dampening, etc. set to 0 and restitution set to 1. I tried setting the different physics values(friction, restitution, etc.) of the tubes to no effect. The planes are using the default values.
So I have a camera in my SceneKit project that is able to rotate and move around freely, and I have an object that, in some cases, needs to stay at a constant distance away from the camera and always be in the center of its view no matter how the camera rotates. Unfortunately, I'm new to SceneKit and don't know how to accomplish this.
So the key things I'm looking for are:
How to have the object always at the same distance from the camera
How to have the object always be in the center of the camera's view no matter what direction it's looking
At the moment, both the camera and the object (an SCNNode with a box geometry) are children of the same scene.
I'm coding in swift so I'd prefer an answer using that, but if you have a solution in objective-c, that works too.
Thanks a bunch!
Think about how you might solve this in the real world. Grab a two by four of the appropriate length. Use duct tape to attach the ball to one end, and to attach the camera (aimed at the ball) to the other end.
Now you can carry that rig around with you. The ball will always be in the center of the camera view, and will be a constant distance away from the camera.
You can build the same rig, virtually, in SceneKit. Create a new SCNNode to be the rig (taking the place of the two by four). Add the ball as a child node, at (0, 0, 0). Add the camera as a child node too, at (0, 0, 5) (camera looks down the -Z axis, so this position should put the ball in the center of the view). Now you can move the rig node anywhere in the scene you want, and you'll have a consistent ball position.
I use ball.frame.intersects(bar.frame), where ball = SKShapeNode(circleOfRadius: BALL_RADIUS) and bar = SKShapeNode(rectOfSize: <Some CGSize object>)
While this works, there is an invisible square in which the circle is inscribed. The diameter of the circle is the same as the side of the square. This square is what ball.frame is, so sometimes, the ball will act like it's intersecting the bar, but visually it's not.
In this screenshot you can see that the ball is stopped, and therefore the game is over because ball.frame.intersects(bar.frame) returned true, even though visually the ball isn't even touching the bar.
So how do I check if the ball is really intersecting the bar, and not if the ball's frame is intersecting?
SpriteKit has decently powerful collision detection in its physics subsystem — you don't have to roll your own.
Create SKPhysicsBody objects for the ball and the bars, using init(circleOfRadius:) for the former and init(rectangleOfSize: for the latter.
Then, you can either:
Drive all the movement yourself, and use SKPhysics only to test for collisions. In this case you need to set your scene's physicsWorld.gravity to the zero vector, and call allContactedBodies() on the ball when you want to see if it's contacting a bar.
Let SKPhysics drive the movement, by setting velocities or applying impulses. In this case you'll need to set a contact delegate for your physics world, and then you'll get a callback whenever a collision occurs.
For more details on either approach, see the docs.
After detecting that ball frame intersects box frame:
Determine the radius of the ball
Determine the point on the line that is the edge of the ball from the center of the ball to the edge of the box. (Hint: pythagorean theorem - hypotenuse = radius)
Is this point inside the box's rect?
Repeat for each point in the box.
I would like to move the camera around a SCNNode nodeA.
What I'have done so far is:
self.cameraNode.constraints = [SCNLookAtConstraint(target: nodeA)]
now if I increment the position.x of the camera the camera still look toward the nodeA. How to move the camera around the nodeA (like the moon around the hearth)?
The camera should move along a circular orbit (just a circle, I'm not interested in elliptical orbit)
Add the moon as the child node of the earth. That way translating the earth won't affect the moon's relative position. You can find an example in the SceneKit Slides for WWDC 2014 sample code.
I'm making a game in which there are horizontal rectangles falling down and a ball in the scene, when the ball collides with the rectangle the moveTo action for the rectangle should stop. This [node removeAllActions] is the first thing called in the didbegincontact method. This should basically freeze the frame when the contact starts so the ball and recitingle should be in contact.
But this is not the some 50% of the time: here is an example:
I enabled the view physics option and the physics body fits tightly with the ball so I don't think the physics body for the ball is wrong.
I have enabled usesPreciseCollisionDetection for the ball only
Is this just a lag in the game calling the removeAllActions?
Please help thanks