Sunday, 2 April 2023

 how to rotate a tank turret and its barrel to point to a target in Unity? here is the code,

using UnityEngine;


public class tank_controller2 : MonoBehaviour

{

    public Transform turret;

    public Transform barrel;

    public Transform target;

    public float turretRotationSpeed = 5f;

    public float barrelRotationSpeed = 5f;

    public float turretRotationRange = 30f;

    public float barrelMinRotationRange = -20f; // Add this line

    public float barrelMaxRotationRange = 30f; // Add this line


    void Update()

    {

        Vector3 targetDirection = target.position - transform.position;

        float yAngle = Mathf.Atan2(targetDirection.x, targetDirection.z) * Mathf.Rad2Deg;

        yAngle = Mathf.Clamp(yAngle, -turretRotationRange, turretRotationRange);

        turret.rotation = Quaternion.RotateTowards(turret.rotation, Quaternion.Euler(0f, yAngle, 0f), turretRotationSpeed * Time.deltaTime);


        Vector3 localTarget = turret.InverseTransformPoint(target.position);

        float xAngle = -Mathf.Atan2(localTarget.y, localTarget.z) * Mathf.Rad2Deg;

        xAngle = Mathf.Clamp(xAngle, barrelMinRotationRange, barrelMaxRotationRange); // Update this line

        barrel.localRotation = Quaternion.RotateTowards(barrel.localRotation, Quaternion.Euler(xAngle, 0f, 0f), barrelRotationSpeed * Time.deltaTime);


        Debug.DrawLine(barrel.position, target.position);

    }

}



Monday, 18 May 2020

viewed as nothing if not a loyal soldier to President Obama. Not so with President Trump and Attorney General Sessions. Today the media would have you believe the Department of Justice functions independently from the president—that any coordination is somehow obstruction of justice. Media reports cited Trump’s efforts to stop the recusal of AG Sessions from the Russia investigation as grounds for obstruction. Other reports cite Trump’s dismissal of FBI director James Comey as obstruction. In a classic case of projection, Democrats unwittingly exposed this double standard in an exchange during the Senate Judiciary Committee’s June 18, 2018, questioning of DOJ inspector general Horowitz. Senator Kamala Harris (D-CA) asked Horowitz who else was shown the damning OIG report of the Clinton email investigation in advance. “Did you provide it to the White House?” He responded they had not. But then he added, “We’ve had instances, as I could testify to in

Chaffetz, Jason. The Deep State (pp. 164-165). Broadside e-books. Kindle Edition. 

Tuesday, 22 May 2018

How to check if two line segments are crossed?

this is a mathematical question, not programming, but since I am working on some 3D developing, i have to deal with lots of mathematical calculation, one of the most difficult questions is: How to check two lines are crossed?

like this:















Given line segments p1p2 and p3p4, how to check if they are crossed?

well, to answer this question, I reviewed my mathematical knowledge learned almost 25 years ago,  completely forgot. Now i give out the answer, how to check, what formula you have to use:

first we have to define some rules, P1, P2, P3, P4 they are all vector 2 points, which means they all have x and y coordinate value
P1(x,y), P2(x,y), P3(x,y), P4(x,y)

Tow points can define a line, the line formula is like this:
(x-x1)/(x2-x1) = (y-y1)/(y2-y1)
So the line defined by line segment P1P2 can be written like this:
(x-P1.x)/(P2.x-P1.x) = (y-P1.y)/(P2.y-P1.y)

the same reason,  the line defined by line segment P3P4 can be written like this:
(x-P3.x)/(P4.x-P3.x) = (y-P3.y)/(P4.y-P3.y)

now we have a perfect binary equations
(x-P1.x)/(P2.x-P1.x) = (y-P1.y)/(P2.y-P1.y)
(x-P3.x)/(P4.x-P3.x) = (y-P3.y)/(P4.y-P3.y)

the alternative way is:
(P2.y-P1.y)x+(P1.x-P2.x)y = P1.x(P2.y-P1.y) + P1.y(P1.x-P2.x)
(P4.y-P3.y)x+(P3.x-P4.x)y = P3.x(P4.y-P3.y) + P3.y(P3.x-P4.x)

to get x and y, please reference the binary equation solver, here I give out the solution only
given:





here:
b1 = (P1.x-P2.x);
c2=P3.x(P4.y-P3.y) + P3.y(P3.x-P4.x);
b2=(P3.x-P4.x);
c1=P1.x(P2.y-P1.y) + P1.y(P1.x-P2.x);
a2=(P4.y-P3.y)x+(P3.x-P4.x);
a1=(P2.y-P1.y)x+(P1.x-P2.x)

based on all these conditions, we can calculate the cross point of the two lines, than check the range

defined cross point (x,y)
if (x between P1.x and P2.x) and (y between P1.y and P2.y) and (x between P3.x and P4.x) and (y between P3.y and P4.y)
than we can see, these two line segments are crossed, otherwise, there is no cross

based on the logic above, I believe you can easily design your code to check cross






Friday, 17 November 2017

Hololens Demo: Move Scale and Spin an 3D object

I have a new Hololens Demo video
Move Scale and Spin any 3D object


this demo used the Box Frame c# script I introduced last time, this time, I added code to let user move, scale and rotate the Box Frame

I am designing more exacting demo now

will show you soon

Enjoy it

Saturday, 11 November 2017

Unity Learning: I created a Box Frame for any GameObject

you can watch this video if you don't want to read my words
Unity 2017, How to add a Box Frame to GameObject

I need to create a box frame for any game object I like, but I found it's hard, of course I can use Line Renderer, but it's not nice,

I want the frame to be in Cube mode, in Cylinder mode, not just boring plane surface

that's why I created this magic class BoxFrame.cs

enjoy the video

Tuesday, 7 November 2017

Unity, How to understand Transform.Rotate(Vector3, Space.Self) ?

when developing in Unity, you always has to use Transform.Rotate() function, one of its overload is
Transform.Rotate(Vector3 v, Space.Self), like this:

void Update () {
    this.transform.Rotate(new Vector3(1, 1, 1), Space.Self);
}

i didn't read any document explain it very well, all give reader some confusion, (really  don't understand why?)

ok, let's use some very straightforward words

Suppose the object which attach this script, the transform.position is (100, 100, 100),

there will be a little box centered by this point,  the diagonal points will be
 (100+1, 100+1, 100+1), (100-1, 100-1,100-1)

if you consider this two points as a line, the object will be rotated based on this line

this is how this function works, easy, en..