• Create Account

# mathacka

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

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

21 April 2013 - 01:32 AM

Hi,

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)

SetSpriteAngle(1,angle)```

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
else
angle = 0
endif
```

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.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f, 0.5f, 0.5f, 1.0f);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -7.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

mCube.draw(gl);

mTransY += 0.075f;
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
gl.glViewport(0,0,width,height);

float aspectRatio;
float zNear = 0.1f;
float zFar = 1000;
float fieldOfView = 30.0f/57.3f;
float size;

gl.glEnable(GL10.GL_NORMALIZE);

aspectRatio = (float)width/(float)height;

gl.glMatrixMode(GL10.GL_PROJECTION);

size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));

gl.glFrustumf(-size, size, -size / aspectRatio,
size / aspectRatio, zNear, zFar);

gl.glMatrixMode(GL10.GL_MODELVIEW);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glDisable(GL10.GL_DITHER);

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

if (mTranslucentBackground)
{
gl.glClearColor(0, 0, 0, 0);
} else {
gl.glClearColor(1, 1, 1, 1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_DEPTH_TEST);
}

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[] =
{
1,0,3,
1,3,2,
1,2,6,
1,6,5,
1,5,4,
1,4,0

};

byte tfan2[] =
{
7,4,5,
7,5,6,
7,6,2,
7,2,3,
7,3,0,
7,0,4
};

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
mFVertexBuffer.put(vertices);
mFVertexBuffer.position(0);

mColorBuffer = ByteBuffer.allocateDirect(colors.length);
mColorBuffer.put(colors);
mColorBuffer.position(0);

mTfan1 = ByteBuffer.allocateDirect(tfan1.length);
mTfan1.put(tfan1);
mTfan1.position(0);

mTfan2 = ByteBuffer.allocateDirect(tfan2.length);
mTfan2.put(tfan2);
mTfan2.position(0);
}

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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new CubeRenderer(true));
setContentView(view);
}
}
```

### 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?

PARTNERS