How to achieve a high level throw toss launch? Speaking from the parabola! Cocos Creator!

Game often encounter problems parabolic trajectory, for which studies how the use of mathematical physics knowledge, high level throw throw effect. For a complete code for the bottom of the article!

Effect Preview:

Confirm the known conditions:

  • Starting point for launching velocity magnitude V
  • Acceleration of gravity G
  • After the starting point and the point

The need to obtain:

  • Emission angle a

For parabolic movement, you can go in two directions. Horizontal, uniform linear motion. Constant acceleration in the vertical direction. So we can draw the following formula:

In the tand v_yafter the last one into the formula, simplified consolidation, you can get about tan aa quadratic equation of.

The solution formula and then a quadratic equation can be solved angle tanvalue.

Then use inverse trigonometric functions, the calculated value of the angle. It should be noted that the inverse trigonometric function arctanof the value domain (-PI/2, PI/2). This is a first, four-quadrant value, in the second and third quadrants of the time to add 180 degrees (PI). That is, the left-emitting point passes the point, to increase the angle of 180 degrees (PI).

Large angle is just the effect of high throw, and throw small angle is flat effect.

Then take a look at the code. First determine the horizontal displacement and vertical displacement h s through two coordinates. A quadratic equation is then determined in accordance with the above simplification tanvalue. Finally, find the angle.

const s = location.x - START_POS.x;
const h = location.y - START_POS.y;
// a*t^2 + b*t + c = 0
const a = G * s / (2 * V * V);
const b = 1;
const c = a - h / s;
const delta = b * b - 4 * a * c;
if (delta >= 0) {
    // 一元二次方程求根公式
    const t1 = (-b + Math.sqrt(delta)) / (2 * a); // 平抛 tan 值
    const t2 = (-b - Math.sqrt(delta)) / (2 * a); // 高抛 tan 值
    // 二、四象限角度要加 180
    const alpha1 = Math.atan(t1) + (s < 0 ? Math.PI : 0);
    const alpha2 = Math.atan(t2) + (s < 0 ? Math.PI : 0);
}

Here with the physics engine, the initial velocity required in both directions, and only need to transmit the total angular velocity magnitude, the two components can be calculated velocity magnitude.

const v_x_1 = Math.cos(alpha1) * V;
const v_y_1 = Math.sin(alpha1) * V;
const v_x_2 = Math.cos(alpha2) * V;
const v_y_2 = Math.sin(alpha2) * V;
// 低抛线速度 保存起来
this._linearVelocity_1.x = v_x_1;
this._linearVelocity_1.y = v_y_1;
// 高抛线速度 保存起来
this._linearVelocity_2.x = v_x_2;
this._linearVelocity_2.y = v_y_2;

Only linear speed is not enough, we must adjust the angle of each bow. This angle can be calculated according to the linear speed of the rigid body. Each frame bow change the angle of the rigid body to the node.

// 计算夹角
const angle = rigidBody.linearVelocity.clone().signAngle(cc.v2(1, 0));
rigidBody.node.rotation = angle * 180 / Math.PI;

How to stop the emission control arrows? Add a timer can not stop launching bow and arrow.

this.schedule(this.fireArrow, 0.5, cc.macro.REPEAT_FOREVER);

Depending on the transmission mode, select a different transmit the linear velocity on it.

private _index = 0;
private fireArrow() {
    const linearVelocity = this.toggle_arrow.isChecked ? this._linearVelocity_2.clone() : this._linearVelocity_1.clone()
    const rigidBody_arrow = this._all_arrows[this._index++ % this._all_arrows.length];
    rigidBody_arrow.node.setPosition(START_POS);
    rigidBody_arrow.linearVelocity = linearVelocity;
}

The above is the use of white ice Cocos Creator to develop "high-level throw throw emission" technology sharing. There are ideas welcome message! If this bit of help to you, welcome to share with her friends.



Demo link
complete code
reference article

Guess you like

Origin www.cnblogs.com/lamyoung/p/12105393.html