Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 11 May 2012
Offline Last Active Aug 29 2014 07:18 PM

Topics I've Started

Trig: Getting a space ship to head in a direction according to a joystick, using AGK

21 April 2013 - 01:32 AM



I'm making a 2D spaceship-shooter game that moves in all directions, (i.e. ( -x, +x ),( -y, +y ) ).


I'm trying to make the spaceship face the direction of the movement according to a virtual joystick. The virtual joystick has it's x and y coordinates working correctly and they are normalized (ie. between -1 and +1) in both x and y.


I'm able to set the angle of the image I'm using by a call to 


SetSpriteAngle(spriteNum, angle)


where the angle is in degrees. The problem I'm having is with the trigonometry involved.


I know from trig that calculating the angle is radius*cos(x), and I could be wrong on that, so I tried this: 


x = GetVirtualJoystickX(1)
y = GetVirtualJoystickY(1)
r = sqrt(x*x + y*y)

angle = r * cos(x)



Problem one is, there's no account for the Y direction, so I tried:


angle = r*cos(x) + r*sin(y)


Still a problem, just not working correctly, so I tried arcsine and arccosine:


angle = r*aCos(x) + r*aSin(y)


Now the image is just flipping in what seems like a random direction, and just in case it was calculating, instead of the total angle, perhaps it was the angle from the x intercept, so I tried:


    if GetVirtualJoystickX(1) > 0 and GetVirtualJoystickY(1) > 0
        //angle = angle
    elseif  GetVirtualJoystickX(1) < 0 and GetVirtualJoystickY(1) > 0
        angle = angle - 180
    elseif  GetVirtualJoystickX(1) < 0 and GetVirtualJoystickY(1) > 0
        angle = angle - 270
    elseif  GetVirtualJoystickX(1) > 0 and GetVirtualJoystickY(1) < 0
        angle = angle - 360
        angle = 0


Now it's only flipping as if angle is close to 0 and moving occasionally in a certain 270 degree position  I believe,


So I guess I'm going to have to admit that my trigonometry skills aren't what they used to be and ask you guys for some help, by the way sin(x) is where x is in degrees and sinr(x) is where x is in radians, so I might be missing it there too, frankly I'm just too confused to get any farther right now.


Thanks guys!

Cube showing then disappearing

15 April 2013 - 04:08 PM

UPDATE:  This is working once I tilt the device to do a refresh, now I need to see how to initially refresh the screen.


How can I programatically tell it to refresh the screen?



I commented out just about everything I could, and the only thing that did was make the cube not appear once, however with it all uncommented it's just "blipping" a cube to the screen then disappearing.


public class CubeRenderer implements GLSurfaceView.Renderer 
	public CubeRenderer(boolean useTranslucentBackground)
		mTranslucentBackground = useTranslucentBackground;
		mCube = new Cube();
	public void onDrawFrame(GL10 gl)
		gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
		gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -7.0f);
		mTransY += 0.075f;

	public void onSurfaceChanged(GL10 gl, int width, int height) 
		float aspectRatio;
		float zNear = 0.1f;
		float zFar = 1000;
		float fieldOfView = 30.0f/57.3f;
		float size;
		aspectRatio = (float)width/(float)height;
		size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));
		gl.glFrustumf(-size, size, -size / aspectRatio, 
		  						    size / aspectRatio, zNear, zFar);

	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
		if (mTranslucentBackground)
			gl.glClearColor(0, 0, 0, 0);
		} else {
			gl.glClearColor(1, 1, 1, 1);

	private boolean mTranslucentBackground;
	private Cube mCube;
	private float mTransY;

I'm not sure what's wrong with this code, it is showing a cube, very briefly, then disappearing, any suggestions as to what's wrong?
Here's the cube class:

class Cube
    public Cube()
        float vertices[] = 
                -1.0f,  1.0f, 1.0f,
                 1.0f,  1.0f, 1.0f,
                 1.0f, -1.0f, 1.0f,
                -1.0f, -1.0f, 1.0f,
                -1.0f,  1.0f,-1.0f,
                 1.0f,  1.0f,-1.0f,
                 1.0f, -1.0f,-1.0f,
                -1.0f, -1.0f,-1.0f

        byte maxColor=(byte)255;
        byte colors[] = 
         maxColor,maxColor,       0,maxColor,
                0,       maxColor,maxColor,maxColor,
                0,              0,       0,maxColor,
                maxColor,       0,maxColor,maxColor,
         maxColor, 0,       0,maxColor,
                0,       maxColor,  0,maxColor,
                0,              0,maxColor,maxColor,
                0,        0,  0,maxColor

        byte tfan1[] = 

        byte tfan2[] = 

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        mFVertexBuffer = vbb.asFloatBuffer();
        mColorBuffer = ByteBuffer.allocateDirect(colors.length);
        mTfan1 = ByteBuffer.allocateDirect(tfan1.length);
        mTfan2 = ByteBuffer.allocateDirect(tfan2.length);

    public void draw(GL10 gl)
        gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);
        gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
     gl.glDrawElements( GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan1);
     gl.glDrawElements( GL10.GL_TRIANGLE_FAN, 6 * 3, GL10.GL_UNSIGNED_BYTE, mTfan2);

    private FloatBuffer mFVertexBuffer;
    private ByteBuffer  mColorBuffer;
    private ByteBuffer  mTfan1;
    private ByteBuffer  mTfan2;

And here's the Activity class:

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		GLSurfaceView view = new GLSurfaceView(this);
		view.setRenderer(new CubeRenderer(true));

Looking to be an indie game developer, have questions

08 January 2013 - 07:04 PM

Hi, I'm trying to become an indie game developer. I'm learning directX atm and I'm trying to figure out what direction to take.


I'm wanting to develop intensive 2D and perhaps some simple 3D games.


I've looked into the windows store, but I'm not sure that's the place for me. Are the retailers to sell games for real, or are there a lot of scams out there?


How do I find a trusted retailer to sell my games? Should it cost money to sell my game? What programming API's should I be looking to develop with that will encompass 2D and 3D desktop games? What do I do if I want to go mobile, the API's seem unrefined for beginners?


I know these are a little bit scattered questions, I'm just full of questions, I really want to make games.

Is DirectX 11 much different from DirectX 10?

08 January 2013 - 01:32 AM

I own, "Introduction to 3D Game Programming with DirectX 10" by Frank D. Luna, is that a good book to start with. I havn't read it yet?


Is directX 11 much different from directX 10? I know everything is going to shaders now, even if I learn shaders in directx 10 will I be able to write in 11?

Which DirectX to Pick

05 January 2013 - 11:55 PM

Which DirectX API should I choose to use?


Is there an easier way to code in DX11 or DX10 even compared to 9?


Are there more things to check for before initialization of a DirectX interface?


Since they're all backwards compatible, why not even use DirectX 1, Microsoft still uses tutorials for this?