Author Topic: Moving a sprite according to an angle (half working)  (Read 2867 times)

rjanes

  • Guest
Moving a sprite according to an angle (half working)
« on: May 11, 2012, 05:22:34 pm »
Hey guys,

So here's what I'm trying to accomplish, at the bottom left of the window is a touch area, where the thumbstick will be.  Depending on where you touch (hold/move your finger) on that thumbstick touch area, will determine which way the sprite moves and faces.  I've managed to get the touch area added, and it properly determines the angle that we've touched and properly rotates the sprite so that he's facing whatever direction we're touching. 

Where I run into a problem is with the sprite moving using ViewSetxy().  On the touch event==2, I set a boolean to true, (it sets to false on the event==3).  On my OnTimer() event, I think I'm just messing up the trig to calcuate where the sprite should move to.  It moves, just not equal to the angle my sprite is facing. 

Here is the code, I've also uploaded the entire folder  and assets so you can test it yourself (bare in mind the touch area for rotating your sprite is at the bottom left of the screen, visualize a thumbstick here).

www.samugames.com/pb.zip

Any thought as why it's not moving in the appropriate direction?
Code: [Select]
//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"
#include <iostream>
#include <math.h>
#define pi 3.14159

//Used for storing movement and our angle
float dx, dy;
float newX,newY;
int Speed = 2;
double tcX,tcY;
double Angle;

//Images
int imCharacter;
int imMap,vwMap;

//Movement Boolean
bool isMoving;

//Player Triangle
int vwMyPlayer;
int MyX,MyY;

//The touch
int tcScreen;
int TouchScreen(int id, int event, int x, int y);

//Vector
struct Vec2D{ 
float x; 
float y;
};

void AppMain()
{
//Landscape mode
LandscapeMode();
//Load the Character and Map into Memory
imCharacter=ImageAdd("Images/Character0.png");
imMap=ImageAdd("Images/Map1.png");
//Set our Moving flag to false
isMoving=false;
//View of the Map
vwMap=ViewAdd(imMap,0,0);
//Initial Coords for placing the player triangle
MyX=100;
MyY=100;
//Define the NewX/NewY used in the OnTimer() for our new position
newX=MyX;
newY=MyY;
//Add the player view ontop of the screen
vwMyPlayer=ViewAdd(imCharacter,MyX,MyY);
//Add our touch to the bottom left of the screen in a 220x220 frame, this will be where the thumbstick is
tcScreen=TouchAdd(0,100,220,220,TouchScreen,0);

}

void AppExit()
{

}

void OnTimer()
{


if(isMoving)
{

//this is where I'm running into an issue, it's not moving to the appropriate location
dx=cos(Angle);
dy=sin(Angle);
newX+=(Speed*dx);
newY+=(Speed*dy);
ViewSetxy(vwMyPlayer,newX,newY);
}
 
}
int TouchScreen(int id, int event, int x, int y)
{

if(event==2)
{
isMoving=true;
//Declare our vectors
Vec2D touch;
Vec2D thumbstick;
Vec2D direction;
//Where we touched on the Touch Area (relative to the Thumbstick in the middle of the touch)
touch.x = x;
touch.y = y;
//The center of the Touch Area (When we hold our finger in around that area, our player image faces the right angle)
thumbstick.x = 80;
thumbstick.y = 230;
//Direction
direction.x = touch.x - thumbstick.x;
direction.y = touch.y - thumbstick.y;
tcX=direction.x;
tcY=direction.y;
//Determine the angle we're facing
Angle = atan2(tcY,tcX);
Angle = (Angle * 180 / pi);
//Rotating the Image
ViewSetRotate(vwMyPlayer,Angle);
}

if(event==3)
{
//Stop moving
isMoving=false;
}
return(0);
}

rjanes

  • Guest
Re: Moving a sprite according to an angle (half working)
« Reply #1 on: May 11, 2012, 06:27:47 pm »
Nevermind had a blonde moment...total blonde moment.  Was working in degrees and not radians.

Code: [Select]
if(isMoving)
{

dx=cos(atan2(tcY,tcX));
dy=sin(atan2(tcY,tcX));
newX+=(Speed*dx);
newY+=(Speed*dy);
ViewSetxy(vwMyPlayer,newX,newY);
}