Building a Better D-Pad
by Dave Calabrese · in iTorque 2D · 12/10/2009 (1:00 am) · 8 replies
Okay, here is something I'll turn into a resource for everyone once I get it finished - a better D-Pad GuiObject. Well, 'better' is subjective, as iTGB doesn't have one at all right now - so I built a simple one for most of the Shinobi Ninja development. Though, by simple, I mean a 9 space grid. Worked great, but not awesome for hardcore playability - for that, I want an angular based D-Pad like most other iPhone games have.
What I have done so far is created a GuiCtrl that displays an image. The player touches it, and the C++ code determines the angle from the center that was touched, then uses that to determine how to move the player. Problem is, it's pretty flakey right now. Hopefully someone out there with a better understanding of angular math might be able to point out my problem. Here is the code:
What happens is, no matter where you touch the GuiObject, the player moves in an almost random direction. Hopefully I just swapped something somewhere in my math, but any help would be greatly appreciated!
What I have done so far is created a GuiCtrl that displays an image. The player touches it, and the C++ code determines the angle from the center that was touched, then uses that to determine how to move the player. Problem is, it's pretty flakey right now. Hopefully someone out there with a better understanding of angular math might be able to point out my problem. Here is the code:
Point2I pos = globalToLocalCoord(event.mousePoint);
t2dVector controlDiscCenter;
t2dVector controlDiscTouchPos;
controlDiscCenter.mX = 68.5;
controlDiscCenter.mY = 68.5;
controlDiscTouchPos.mX = pos.x;
controlDiscTouchPos.mY = pos.y;
// find angle
F32 angleOut = mDegToRad(mFmod(mAtan( controlDiscTouchPos.mX - controlDiscCenter.mX , controlDiscCenter.mY - controlDiscTouchPos.mY), 360.0f));
// normalize to 0 - 360
if ( angleOut < 0.0f ) angleOut += 360.0f;
F32 sin, cos;
F32 speed = 35;
mSinCos( angleOut, sin, cos );
// Set Gross Linear Velocity.
gCurrentPlayer->setLinearVelocity( t2dVector( cos*speed, sin*speed ) );What happens is, no matter where you touch the GuiObject, the player moves in an almost random direction. Hopefully I just swapped something somewhere in my math, but any help would be greatly appreciated!
About the author
Recent Threads
#2
12/11/2009 (12:11 am)
Got it! Here is the final code. I'll finish this up and create a resource out of it shortly:Point2I pos = globalToLocalCoord(event.mousePoint);
t2dVector controlDiscCenter;
t2dVector controlDiscTouchPos;
controlDiscCenter.mX = 68.5;
controlDiscCenter.mY = 68.5;
controlDiscTouchPos.mX = pos.x;
controlDiscTouchPos.mY = pos.y;
// find angle
F32 angleOut = mFmod(mAtan( controlDiscTouchPos.mY - controlDiscCenter.mY , controlDiscTouchPos.mX - controlDiscCenter.mX), (2 * 3.14159));
// normalize to 0 - 360
if ( angleOut < 0.0f )
angleOut += (2 * 3.14159);
F32 sin, cos;
F32 speed = 35;
mSinCos( angleOut, sin, cos );
// Set Gross Linear Velocity.
gCurrentPlayer->setLinearVelocity( t2dVector( cos*speed, sin*speed ) );
return;
#3
12/14/2009 (5:26 pm)
It would be really cool if you could show us how to add this to the code, and how to call it.
#4
www.torquepowered.com/community/resources/view/18925
I anticipate we will all expand the resource, as right now it goes as far as acquiring the data and giving an example on how to work with it, because every game will most likely utilize the data in a different manner. Move discussions over there and I'll help everyone work through their custom versions!
12/14/2009 (6:16 pm)
Just added a resource for this! It can be found here:www.torquepowered.com/community/resources/view/18925
I anticipate we will all expand the resource, as right now it goes as far as acquiring the data and giving an example on how to work with it, because every game will most likely utilize the data in a different manner. Move discussions over there and I'll help everyone work through their custom versions!
#6
12/26/2009 (12:39 pm)
@Edoardo: If you recompile the TGB editor with the new D-Pad source files as part of it, the new control should automatically become part of the editor. Not at joke to verify this, but I'm pretty sure the editor automatically grabs any control created like this or derived from the parent control.
#7
12/26/2009 (3:27 pm)
Dave, have you tried this with PSK?
#8
12/27/2009 (3:33 am)
I have not, however I can't think of any reason why it wouldn't work.
Associate Matt Mitman
I'm not positive off the top of my head, but I seem to recall you need to check if the x value of the vector in mAtan is negative, and add Pi to the angleOut result if it is.