3D Game Programming and Design - Space and Sports

Keywords: Unity Programming github

Short answer and validation with program

1

What is the essence of game object movement?

The essence of the game object movement is to change the coordinates of the game object itself.
In game development, we can transform game objects by matrix transformation, such as:

public class TranslationScript : MonoBehaviour
{
	// The migration direction vector of the game object, whose modulus length is the translation speed, and the vector direction is the translation direction.
	Vector3 translation = new Vector3(1, 0, 0);

	void Update()
	{
		// Each frame moves the game object, and the moving distance needs to consider the frame length.
		transform.Translate(Time.deltaTime * translation);
	}
}
public class RotationScript : MonoBehaviour
{
	// The rotation vector of the game object is determined by the length of the vector module. The game object will rotate counterclockwise along the axis of the straight line in the direction of the vector.
	Vector3 translation = new Vector3(1, 0, 0);

	void Update()
	{
		// For each frame of moving game object, the rotation angle should consider the frame length.
		transform.Translate(Time.deltaTime * translation);
	}
}

2

Please use more than three methods to realize parabolic motion of objects.
(For example, modify the Transform property, using the vector Vector 3 method...)

Method 1

It is implemented by Translate method.
In the two-dimensional coordinate system, the parabolic motion equation of the object is:
x=vxty=vyt+12at2 \begin{aligned} x&=v_xt\\ y&=v_yt+\frac{1}{2}at^2 \end{aligned} xy​=vx​t=vy​t+21​at2​
Then the motion vector of each time interval t Delta t t is:
x2−x1=vxt2−vxt1=vxΔty2−y1=vyt2+12at22−vyt1+12at12=vyΔt+12a(Δt2+2t1Δt) \begin{aligned} x_2-x_1&=v_xt_2-v_xt_1=v_x\Delta t\\ y_2-y_1&=v_yt_2+\frac{1}{2}at_2^2-v_yt_1+\frac{1}{2}at_1^2=v_y\Delta t+\frac{1}{2}a(\Delta t^2+2t_1\Delta t) \end{aligned} x2​−x1​y2​−y1​​=vx​t2​−vx​t1​=vx​Δt=vy​t2​+21​at22​−vy​t1​+21​at12​=vy​Δt+21​a(Δt2+2t1​Δt)​
Then we just need to pass code validation:

public class ProjectileMotionScript : MonoBehaviour
{
	// Initial velocity
    public Vector3 v; // Initial speed can be set to (10, 0, 0) in Unity interface.
    public float g; // Gravity acceleration, which can be set to 9.8 at Unity interface
    private float t = 0;
    
    void Update()
    {
        transform.Translate(v * Time.deltaTime);
        transform.Translate(0, -0.5f * g * (Time.deltaTime * Time.deltaTime + 2 * Time.deltaTime * t), 0);
        t += Time.deltaTime;
    }
}

3

Write a program to achieve a complete solar system, other planets around the sun must rotate at different speeds, and not on a normal plane.

We can add Trail Renderer components to each planet to track its trajectory.

  1. Firstly, we add Rotate AroundScript to each planet. As shown below, the game object will rotate around its parent object. We can set the rotation plane and the rotation rate in Unity panel by setting direction and speed.
public class RotateAroundScript : MonoBehaviour
{
    public Vector3 direction;
    public float speed;

    void Update()
    {
        transform.RotateAround(transform.parent.position, direction * Time.deltaTime, speed * Time.deltaTime);
    }
}
  1. Then we add the RotationScript component for each planet, as shown below, to allow direction and speed to be set through the Unity panel to control the rotation angle and rotation rate.
public class RotationScript : MonoBehaviour
{
    public Vector3 direction;
    public float speed;

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(direction * Time.deltaTime, speed * Time.deltaTime);
    }
}

Programming practice

Requirements to be met by the procedure:

  1. play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
  2. List the things mentioned in the game (Objects)
  3. Use tables to list players'action tables (rule tables). Note that the fewer the actions, the better.
  4. Please prefabricate the objects in the game
  5. Create rectangles, squares, balls and their colors in GenGameObjects to represent the objects in the game.
  6. Effective organization of objects using C # collection type
  7. The whole game only has a main camera and an Empty object, other objects must be generated dynamically!!!! Find Game Objects and SendMessage, which break through the program structure, are not allowed to appear in the whole game. Failure to give points in violation of this rule
  8. Please use courseware architecture diagram to program, do not accept non-MVC structure program
  9. Attention to details, such as: the ship is not ashore, the priest and the devil boarding and disembarking movement, can not accept user events!

See https://github.com/huanghongxun/3D-Programming-And-Design/tree/master/homework2/PriestsAndDevils.

Thinking questions

Using vectors and transformations to implement and extend the methods provided by Tranform, such as Rotate, Rotate Around, etc.

// Let t rotate with specific axis and angle.
void Rotate(Transform t, Vector3 axis, float angle)
{
	var rot = Quaternion.AngleAxis(angle, axis);
    t.position = rot * t.position;
    t.rotation *= rot;
}
// Let t rotate around center with specific axis and angle.
void RotateAround(Transform t, Vector3 center, Vector3 axis, float angle)
{
	var position = t.position;
    var rot = Quaternion.AngleAxis(angle, axis);
    var direction = position - center;
    direction = rot * direction;
    t.position = center + direction;
    t.rotation *= rot;
}

Posted by efron on Sat, 21 Sep 2019 06:59:38 -0700