I have been reading a book about DirectX game development and I just recently learned about Sprite transforms and the like. Anyways, I challenged myself to rotate the character according to where the mouse pointer is on the screen and have run into a few hiccups.
The method I decided to use was:
- Store the mouse location in a POINT variable
- Use GetCursorPos(LPPOINT) to store the current location of the cursor
- Upon each Game_Update(which occurs all the time), change the rotation of the sprite via using atan2() to get the angle that the sprite is facing.
- Rotate the sprite in Game_Update()
For some reason, the image is not rotated where the mouse pointer is yet my method seems logical or have I missed something crucial.
Here is an image so you can see:
[The red line should be in the same direction as where my mouse pointer is, X is the location that the mouse pointer was at]

Here is the code that implements the steps above:
/*
Beginning Game Programming, Thid Edition
MyGame.cpp
Rotate_Scale_Demo
*/
#include "MyDirectX.h"
#include <string>
#include <math.h>
using namespace std;
const string APPTITLE = "Sprite Rotation and Scaling Demo";
const int SCREENW = 1024;
const int SCREENH = 768;
LPDIRECT3DTEXTURE9 sunflower;
D3DCOLOR color;
int frame = 0, columns,width,height;
int startframe,endframe,starttime=0,delay;
//FPS Calculations
DWORD dwFrames;
DWORD dwCurrentTime;
DWORD dwLastUpdateTime;
DWORD dwElapsedTime;
string FPS;
bool Game_Init(HWND window)
{
//zero out timer variables
dwFrames = 0;
dwCurrentTime = 0;
dwLastUpdateTime = 0;
dwElapsedTime = 0;
//initialize Direct3D
Direct3D_Init(window,SCREENW,SCREENH,false);
//initialize DirectInput
DirectInput_Init(window);
//load the sprite image
sunflower = LoadTexture("sunflower.bmp");
return true;
}
void Game_Run(HWND window)
{
static float scale = 0.001f;
static float r = 0;
static float s = 1.0f;
//make sure the Direct3D device is valid
if (!d3ddev) return;
//update input devices
DirectInput_Update();
//clear the scene
d3ddev->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,100), 1.0f, 0);
d3ddev->ColorFill(backbuffer,NULL,D3DCOLOR_XRGB(0,255,0));
//start rendering
if (d3ddev->BeginScene())
{
//begin sprite rendering
spriteobj->Begin(D3DXSPRITE_ALPHABLEND);
//****************MOUSE STUFF EXTRA:**********************
//set rotation and scaling
GetCursorPos(&absCursorPos);
double mx = absCursorPos.x;
double my= absCursorPos.y;
r = (float)(atan2(my, mx));
//***************************
s += scale;
if (s < 0.1 || s > 1.25f) scale *= -1;
//draw sprite
width = height = 512;
frame = 0;
columns = 1;
color = D3DCOLOR_XRGB(255,255,255);
Sprite_Transform_Draw(sunflower,300,150,width,height,frame,columns,r,s,color);
//end sprite rendering
spriteobj->End();
dwFrames++;
dwCurrentTime = GetTickCount();
dwElapsedTime = dwCurrentTime - dwLastUpdateTime;
if (dwElapsedTime >= 1000)
{
FPS = (UINT)(dwFrames * 1000.0 / dwElapsedTime);
dwFrames = 0;
dwLastUpdateTime = dwCurrentTime;
//Print out FPS
}
//stop rendering
d3ddev->EndScene();
d3ddev->Present(NULL,NULL,NULL,NULL);
}
//exit when escape key is pressed
if (Key_Down(DIK_ESCAPE)) gameover = true;
if (Mouse_Button(0)) {
/*
int mx = Mouse_X();
if (mx < 0) AbsMousePosition.x -= (float)(Mouse_X());
else if (mx > 0) AbsMousePosition.x += (float)(Mouse_X());
int my = Mouse_Y();
if (my < 0) AbsMousePosition.y -= (float)(Mouse_Y());
else if (my > 0) AbsMousePosition.y += (float)(Mouse_Y());
*/
// AbsMousePosition.x += (float)(Mouse_X());
// AbsMousePosition.y += (float)(Mouse_Y());
}
}
void Game_End()
{
//free memory and shut down
sunflower->Release();
DirectInput_Shutdown();
Direct3D_Shutdown();
}
Any help is appreciated, thanks!
This post has been edited by alpha_x: 23 June 2012 - 11:14 PM

New Topic/Question
Reply



MultiQuote







|