游戏向量歪斜

时间:2015-04-27 13:39:48

标签: c++ vector path

我必须为我投入的游戏创建一个原型,游戏是在大学拥有的引擎中制作并使用C ++。

我遇到的问题是应该从角色精灵朝向在射击时绑定到光标的对象的子弹,除非光标是光标,否则它不会朝向它。在屏幕的正中心。

以下是我对游戏这些部分的代码:



//all relevant variable declarations (these are outside the game loop)
float speed = 0.01;
float Mx = 0, My = 0, mouseX, mouseY;
int shoot = -1;
float bulletX, bulletY;
float distanceX, distanceY, length;
float directionX, directionY;

//Cursor movement
myEngine->StartMouseCapture();	 // starting capture of mouse
mouseX = myEngine->GetMouseMovementX(); //moving x axis
mouseY = myEngine->GetMouseMovementY(); //moving y axis
cursor->MoveX(mouseX / 50);		 // divide by 50 to slow down speed
cursor->MoveY((mouseY / 50)*-1); //-1 otherwise it's inverted
//End of cursor movement

if((myEngine->KeyHit( Key_Space ) || myEngine->KeyHeld( Key_Space ) ) && shoot == -1) //whilst space bar is pressed or held, and shoot is -1 meaning no bullet exists
	{
		//bullet creation and setup
		bullet = sphereMesh->CreateModel( sprite->GetX(), sprite->GetY(), sprite->GetZ() ); //spawning bullet on current location of character
		bullet->Scale(0.02); //shrinking model to appropriate size
		bullet->SetSkin( "EarthClouds.jpg" ); //changing appearance of model
		shoot = 2000; //starting the count down till bullet removal

		//bullet vector calculations
		bulletX = cursor->GetX() - sprite->GetX(); //x vector calculation
		bulletY = cursor->GetY() - sprite->GetY(); //y vector calculation

		distanceX = bulletX * bulletX; //
		distanceY = bulletY * bulletY; //
		length = sqrt( distanceX + distanceY ); //calculating distance between models

		directionX = bulletX / length; //calculating x direction
		directionY = bulletY / length; // calculating y direction
		//end of bullet vector calculations
	}

if(shoot>0)
	{
		bullet->MoveX( directionX / 50 ); //move x by calculated direction above divide by 50 to slow down
		bullet->MoveY( directionY / 50 ); //move y by calculated direction above divide by 50 to slow down
		shoot--; //reducing shoot counter
	}
else if(shoot == 0)
	{
		sphereMesh->RemoveModel(bullet); //removes bullet model
		shoot--; //reduces shoot counter to -1 to allow spawn of another bullet
	}




1 个答案:

答案 0 :(得分:0)

模型Z轴'没有匹配,造成3D引擎的问题