一直两个坐标点求旋转角度,或已知角度求移动的方向和位置

namespace Pioneer

{

export class Game extends Behaviour{

private nState:number;

private Camera:Camera2D;

nScore:number;

nGold:number;

private nRotationAngle:number = 0;

private nRotationCount:number = 0;

private nFrameRateTimer:number;

private nScoreTimer:number;

private bLoaded:boolean;

private Bg:any;

private Ball:any;

private Player1:any;

private Player2:any;

private Player1Base:any;

private Player2Base:any;

private Btn1:any;

private YuanStage:any;

private vSpeed:any;

private nPlayer1PosX:number = Laya.stage.width/2;

private nPlayer1PosY:number = Laya.stage.height/2+260;

private nPlayer2ToBallDistance:number;

private nBallPos:any;

private nPlayer2Pos:any;

private nOffsetX:any;

private nOffsetY:any;

private nMoveTimer:number = 0;

private bisPlayer2Rotate:boolean = true;

private nPlayer2Angle:number = 0;

private nBallDestroyCount:number = 0;

private nYuanStagePos:any;

private nPlayer1Pos:any;

private Pawn:any;

private PawnOffset:any;

private bDrag = false;

private Scene:Laya.Sprite;

private GameField:Laya.Rectangle;

private nEndTimer = -1;

YCenter = 0;

super()

{

this.bLoaded = false;

this.nFrameRateTimer = 0;

}

Start()

{

this.vSpeed = new EVector3(1, 1, 1);

this.OnAssetLoaded();

}

OnAssetLoaded()

{

this.bLoaded = true;

this.Scene = new Laya.Sprite();

this.Scene.hitArea = new Laya.Rectangle(0, 0, Laya.stage.width, Laya.stage.height);

this.InitEvents(this.Scene);

SceneManager.SetStage(this.Scene);

Laya.stage.addChildAt(this.Scene, 0);

this.Bg = new Entity(1);

this.Bg.Show();

this.Bg.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2);

this.Ball = new Ball(4);

this.Ball.Show();

this.Ball.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260 );

this.Ball.SetLayer(4);

this.YuanStage = new Entity(10);

this.YuanStage.Show();

this.YuanStage.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260);

this.YuanStage.SetLayer(1);

this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 4);

this.Pawn=this.Player1;

this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2, 4 );

this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 3);

this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2, 3);

this.nPlayer1PosX = Laya.stage.width / 2;

this.nPlayer1PosY = Laya.stage.height / 2 + 520;

this.nYuanStagePos = this.YuanStage.GetPosition();

this.ResetGame();

this.StartGame();

}

StartGame()

{

DlgBattle.GetInstance().Show(true);

}

GetPlayerScore(id)

{

if (id == 1) {

return this.Player1.nPlayerScore;

}

else {

return this.Player2.nPlayerScore;

}

}

CreatBall()

{

this.Ball = new Ball(4);

this.Ball.Show();

this.Ball.SetPosition(this.Ball.RandamPosX(), this.Ball.RandamPosY());

this.Ball.SetLayer(4);

}

CreatPlayer1()

{

this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 );

this.Pawn = this.Player1;

}

CreatPlayer2()

{

this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 );

}

CreatPlayer1Base()

{

this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3);

}

CreatPlayer2Base()

{

this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3 );

}

ResetGame()

{

this.nGold = 0;

this.nScore = 0;

}

Update()

{

if(this.nRotationCount % 2 == 0)

{

this.nRotationAngle++;

this.Pawn.SetRotation(this.nRotationAngle);

}

else

{

this.nRotationAngle--;

this.Pawn.SetRotation(this.nRotationAngle);

}

if(this.Ball.Obj == null)

{

this.bisPlayer2Rotate = true;

this.nBallDestroyCount++;

this.CreatBall();

}

this.nBallPos = this.Ball.GetPosition();

this.nPlayer2Pos = this.Player2.GetPosition();

this.nPlayer1Pos = this.Player1.GetPosition();

let nStageToBallPos = Math.pow(this.nBallPos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nBallPos.y - this.nYuanStagePos.y, 2);

let nStageToPlayer1Pos = Math.pow(this.nPlayer1Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer1Pos.y - this.nYuanStagePos.y, 2);

let nStageToPlayer2Pos = Math.pow(this.nPlayer2Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer2Pos.y - this.nYuanStagePos.y, 2);

if(nStageToPlayer1Pos > 90000)

{

this.Player1.Destroy();

this.Player1Base.Destroy();

this.nPlayer1PosX = Laya.stage.width / 2;

this.nPlayer1PosY = Laya.stage.height / 2 + 260;

this.CreatPlayer1();

this.CreatPlayer1Base();

}

if(nStageToPlayer2Pos > 90000)

{

this.Player2.Destroy();

this.Player2Base.Destroy();

this.nPlayer1PosX = Laya.stage.width / 2;

this.nPlayer1PosY = Laya.stage.height / 2 + 260;

this.CreatPlayer2();

this.CreatPlayer2Base();

}

if(nStageToBallPos > 90000)

{

this.Ball.Destroy();

this.CreatBall();

}

this.nPlayer2ToBallDistance = Math.pow(this.nBallPos.x - this.nPlayer2Pos.x, 2) + Math.pow(this.nBallPos.y - this.nPlayer2Pos.y, 2);

this.nOffsetX = this.nBallPos.x - this.nPlayer2Pos.x;

this.nOffsetY = this.nBallPos.y - this.nPlayer2Pos.y;

let angle = Math.atan2(this.nOffsetY, this.nOffsetX) * 180 / Math.PI + 90;

if(angle <= 0){

angle = 360 + angle;

}

if(this.bisPlayer2Rotate)

{

if(this.nBallDestroyCount % 2 == 0){

this.nPlayer2Angle = this.nPlayer2Angle % 360 + 1;

}

if(this.nBallDestroyCount % 2 == 1)

{

this.nPlayer2Angle = this.nPlayer2Angle % 360 - 1 ;

if(this.nPlayer2Angle <= 0){

this.nPlayer2Angle = 360 + this.nPlayer2Angle;

}

}

if(Math.abs(this.nPlayer2Angle - Math.abs(angle)) < 1){

this.bisPlayer2Rotate = false;

}

this.Player2.SetRotation(this.nPlayer2Angle);

if(!this.bisPlayer2Rotate){

this.Player2.SetRotation(angle);

}

}

this.nMoveTimer++;

if(this.nMoveTimer % 20 == 0 && !this.bisPlayer2Rotate)

{

this.Player2.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5);

this.Player2Base.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5);

}

if(this.Player2.TestCollider(this.Ball))

{

this.Ball.Destroy();

this.Player2.nPlayerScore++;

console.log("玩家2的分数" + this.Player2.nPlayerScore);

if (DlgBattle.IsInstanced()) {

DlgBattle.GetInstance().UpdateLifeList();

}

}

}

OnMouse(e:Laya.Event)

{

if (!this.Pawn) {

return;

}

if (e.type == Laya.Event.MOUSE_DOWN)

{

this.nRotationCount++;

if (this.Bg.TestContains(e.stageX, e.stageY))

{

//this.Pawn.SetScale(1.2,1.2);

let x1 = Math.cos((this.nRotationAngle - 90) * Math.PI / 180);

let y1 = Math.sin((this.nRotationAngle - 90) * Math.PI / 180);

this.Pawn.SetPosition(this.nPlayer1PosX += x1 * 30,this.nPlayer1PosY += y1 * 30);

this.Player1Base.SetPosition(this.nPlayer1PosX, this.nPlayer1PosY);

if(this.Pawn.TestCollider(this.Ball))

{

this.Ball.Destroy();

this.Player1.nPlayerScore++;

console.log("玩家1的分数" + this.Player1.nPlayerScore);

if (DlgBattle.IsInstanced()) {

DlgBattle.GetInstance().UpdateLifeList();

}

}

}

}

else if(e.type == Laya.Event.MOUSE_UP){

//this.Pawn.SetScale(1, 1);

}

else if (e.type == Laya.Event.MOUSE_OUT) {

}

}

}

}

原文地址:https://www.cnblogs.com/StevenChancxy/p/9171657.html

时间: 2024-10-09 08:29:23

一直两个坐标点求旋转角度,或已知角度求移动的方向和位置的相关文章

先序,中序,后序,已知两者求第三者

今天来总结下二叉树前序.中序.后序遍历相互求法,即如果知道两个的遍历,如何求第三种遍历方法,比较笨的方法是画出来二叉树,然后根据各种遍历不同的特性来求,也可以编程求出,下面我们分别说明. 首先,我们看看前序.中序.后序遍历的特性: 前序遍历:     1.访问根节点     2.前序遍历左子树     3.前序遍历右子树 中序遍历:     1.中序遍历左子树     2.访问根节点     3.中序遍历右子树 后序遍历:     1.后序遍历左子树     2.后序遍历右子树     3.访问

已知点和线, 求垂足

已知点 \(A(x_1, y_1)\), 现有一条直线 \(l\): \(ax+by+c=0\), 直线 \(l\) 到\(A\) 的举例为 \(d\), 点 \(A\) 到直线 \(l\) 的垂足点记为 \(B\), 求 \(B\) 的坐标. 解答 已知直线的表达式, 可知直线上的一个垂线量为: \((a,b)\), 记 \(B\) 的坐标表示为 \((x_2,y_2)\), 那么向量 \(\overrightarrow{BA} = (x_1 - x_2, y_1 - y_2)\), \(d(

二叉树--已知先序中序求后序--已知中序后序求先序(基本按照网上某大神思路搬过来的)

思路来自(转载自)  http://www.cnblogs.com/fzhe/archive/2013/01/07/2849040.html 题目描述不说了. 前序遍历:  GDAFEMHZ 中序遍历:  ADEFGHMZ 求中序遍历. 1 确定根,确定左子树,确定右子树. 2 在左子树中递归. 3 在右子树中递归. 4 打印当前根. 代码如下: 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 char pr[1000],in[100

已知面积求周长

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this pap

使用python已知平均数求随机数

问题描述:产生40个数,范围是363-429之间,平均值为402 思路: 1 产生一个随机数 2 使用平均数求和随机数求出第二个数,生成20组 3 将排序打乱 # -*- coding: cp936 -*- import random import string ###################产生随机整数################### ###################第一个数随机产生,第二个使用平均数求出################### #count 数字的个数 #a

ACM已知面积求最小周长

Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper. Input In the first line, there is an integer T indicates the number of te

睡觉前请关灯的 破解尝试版本 由已知解求一个矩阵的步骤

#include<iostream> #include"wz.h" #include<ctime> using namespace std; #define  MAX  5 void show(int arr[][MAX]) {  for(int i=0;i<MAX;i++)  {   for(int j=0;j<MAX;j++)   {    cout<<arr[i][j]<< " ";   }   co

已知二叉树前、中序遍历,求后序 / 已知二叉树中、后序遍历,求前序

void solve(int start,int end,int root) { // 前序和中序 -> 后序 // 每次调用solve()函数,传入pre-order的start,end,root if (start > end) // 递归边界 return; int i = start; while (i < end && in.at(i) != pre.at(root)) // 找到左右子树的分割点 i++; solve(start, i - 1, root +

Codeforce 459A - Pashmak and Garden (已知两点求另外两点构成正方形)

Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden