How to get Direction after cueBall collide with any ball in unity3d? - unity3d

I am creating one pool table unity game. I want to get the direction of ball to show user where the ball will move. Consider the following image for an example.
Here A is our cueBall and B is ball on the table. Let's consider that when user hit ball A in the direction as per "DIR 1" it will hit to ball B at that time ball A will be at the position of C as per image.
Now I want to know there point
How to find position of c.
What is the touch point of B & C means which place A will collide with B.
In which direction ball B will move after collide by A.
I hope this Question is clear to all and provide proper detail for my problem.

Assuming you are using a Rigidbody on your balls, you could use :
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.SweepTest.html
you will use your 'DIR1' as the parameter of the SweepTest function.
Once it returned a 'true' and confirmed a collision, you could use the RaycastHit.distance and 'DIR1' to calculate position 'C'
With position 'C', RaycastHit.point(impact position), and position 'B', you could now calculate 'DIR2'.
--
Physics.Capsule cast is also another option > http://docs.unity3d.com/Documentation/ScriptReference/Physics.CapsuleCast.html

Related

Precision in onCollisionStart in flame with flutter

I'm attempting to write a bouncing ball game using flame in flutter. To detect collisions the onCollision and onCollisionStart methods are provided. What I had hoped is that onCollisionStart would give a precise location when two objects first hit each other. However, instead it gives a list of positions indicating where the two objects overlap after the first game-tick when this happens (i.e. onCollisionStart is called at the same time as onCollision, but is not called a second time if the same two objects are still colliding on the next tick).
This is illustrated in the attached picture. The collision points are marked with red dots. If the ball were moving downwards, then the ball would have hit the top of the rectangle and so should bounce upwards. However, if the ball were moving horizontally, then its first point of contact would have been the top left corner of the box, and the ball would bounce upwards and to the left.
If I want to work out correct angle that the ball should fly off, then I would need to do some clever calculations to work out the point that the ball first started hitting the other object (those calculations would depend on the precise shape of the other object). Is there some way to work out the point at which the two objects first started colliding? Thanks
What you usually need for this is the normal of the collision, but unfortunately we don't have that for the collision detection system yet.
We do have it in the raytracing system though, so what you could do is send out a ray and see how it will bounce and then just bounce the ball in the same way.
If you don't want to use raytracing I suggest that you calculate the direction of the ball, which you might already have, but if you don't you can just store the last position and subtract it from the current position.
After that you need to find the normals of the edges where the intersection points are.
Let's say the ball direction vector is v, and the two normal vectors are n1 and n2.
Calculate the dot product (this is build in to the vector_math library) of the ball direction vector and each of the normal vectors:
dot1 = v.dot(n1)
dot2 = v.dot(n2)
Compare the results of the dot products:
If dot1 > 0, n1 is facing the ball.
If dot2 > 0, n2 is facing the ball.
After that you can use v.reflect(nx) to get the direction where your ball should be going (where nx is the normal facing the ball).
Hopefully we'll have this built-in to Flame soon!

Make one sprite follow another off centre, including rotation

I'm working on a car drift game, which I want to make skid marks with using the pen.
I created a circle, radius 25 around the car that I am using as a reference for where the tires would be, and I need to get the tire sprite to follow the back end of car according to its rotation. I'm trying to adapt the formulas x'= xcosθ−ysinθ, y'= xsinθ+ycosθ but I don't know how to do it.
I set the initial x value as 0 and the y value as -25, but I know this is wrong because it doesn't stay the same when the car is moved and rotated.
Here is my project:
https://scratch.mit.edu/projects/535410396/
I got the solution from a user of the Scratch Forums. For the x value:
CarX-25*sin(CarRotation)
For the y value:
CarY-25*cos(CarRotation)

How to smoothly move a node in an ARkit Scene View based off device motion?

Swift beginner struggling with moving a scene node in ARkit in response to the device motion.
What I want to achieve is: First detect the floor plane, then place a sphere on the floor. From that point onwards depending on the movement of the device, I want to move the sphere along its x and z axis to move it around the floor of the room. (The sphere once created needs to be in the center of the device screen and locked to that view)
So far I can detect the floor and place a node no problem. I can use device motion to obtain the device attitude (pitch, roll and yaw) but how to translate these values into meaningful x, y, z positions that I can update my node with?
Are there any formulas or methods that are used to calculate such information or is this the wrong approach? I would appreciate a link to some info or an explanation of how to go about this. Also I am unsure how to ensure the node would be always at the center of the device screen.
so, as far as I understood you want to have a following workflow:
Step 1. You create a sphere on a plane (which is already done)
Step 2. Move the sphere with respect to the camera's horizontal plane (i.e. along its x and z axis to move it around the floor of the room depending on the movement of the device)
Assuming that the Step 1 is done, what you can do:
Get the position of the camera and the sphere
This should be first called within the function that is invoked after sphere creation (be it a tapGestureRecognizer(), touchesBegan(), etc.).
You can do it by calling position property of SCNNode for sphere and for camera position and/or orientation by calling sceneView.session.currentFrame's .camera.transform which contains all necessary parameters about current position of the camera
Move the sphere as camera moves
Having the sphere position on the Scene and the transformation matrix of the camera, you can find the distance relation between them. Here you can find a good explanation of how exactly you can do it
After you get those things you should implement a proper logic within renderer(_:didUpdate:for:) to obtain continuous lock of the ball with respect to the camera position
If you are interested about the math behind it, you can kick off by reading more about transformation matrices which is a big part of Image Processing and many other areas
Hope that this will help!

Unity 2D game Shooting Target Problems

so basically i have a target like the type for archery the 3 rings (bullseye, inner circle and outer circle)
now i basically used a cylinder to create these and then added to them rigidbody2D and a circleCollider 2D , now my problem is because the rings are essentially on top of each other i have them layered out on the z axis a little to make them all visible but when it comes to doing a raycast2d on the target it isnt picking correct ones up for example it goes from outer circle straight to bullseye and skips out inner circle yet all have colliders set up the same way
i cant figure out a way to overcome this and if not ill have to change to a different target where nothing overlaps in order to get it to work but i would really like the archery type targets
Thanks
You could just vary the distance from the camera for each ring so that the ones over the top are hit first.
Alternatively you could add tags to the three rings, use raycast all, and check the tags of all hit colliders to decide which one was hit first. For example, if all 3 register a hit, then you know the center was hit, and if the outer 2 register then you know it's the inner ring, and so on.
http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

animating object along a path in unity 3d

I need to animate an object along a path. I am doing so by creating an animation like:.
This works great but since my terrain is not flat it will be nice if I don't have to deal with the y component. In other words I want to move an object along the x-axis and z-axis and if there is a small slope increase then increase the object y position. same thing if there is a downward slope. Or maybe I have to create a script where it checks to see if the object is colliding with the terrain. if not then decrease its y position. I don't know if this will work meanwhile animating an object though.
I think the easiest way I can think of to solve this is for you to make the object a rigid body and use collision (probably a mesh or capsule collider if its a player character) to get it to sit on the floor.