Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm using DragonFire SDK to develop my iPad game, but I'm not sure how to modify a section of code to only allow certain directional movement. The section of code that I'm not sure of is this:
C++
void OnTimer()
{
    int BallX, BallY;
	int AccX, AccY, AccZ;
    // Get tilt (accelerometer) data for each axis.
	AccX = TiltGetx();
	AccY = TiltGety();
	AccZ = TiltGetz();    
    // Update the ball's next X and Y values based on G-Force and the strength of gravity:
    BallX = ViewGetx(BallView) + (AccX / 100);
	BallY = ViewGety(BallView) + (AccY / 100);
    // Don't let the ball go off the screen:
    // Stops for Landscape:
    if (BallY >= 731)
        BallY = 731;
    if (BallY <= 0)
        BallY = 0;
    if (BallX >= 987)
        BallX = 987;
    if (BallX <= 0)
        BallX = 0;   
/*
    // Stops for Portrait:
    if (BallX >= 731)
        BallX = 731;
    if (BallX <= 0)
        BallX = 0;
    if (BallY >= 987)
        BallY = 987;
    if (BallY <= 0)
        BallY = 0;
*/
	
    // Update the ball's position:
    ViewSetxy(BallView, BallX, BallY);
}

The problem is I'm not sure how I would update my logic to only allow Left, Right, Up, Down only movement. The code was taking from the DragonFireSDK API Docs (http://www.dragonfiresdk.net/help/DragonFireSDKHelp.html[^]), so it will move any direction that the device is tilted.



Here's the entire code:
C++
//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"

int BallImage;
int BallView;

void AppMain()
{
    // Set up an image of a ball to move around the screen:
    BallImage = ImageAdd("Images/Ball.png");
    BallView = ViewAdd(BallImage, 0, 0);

    LandscapeMode();
}

void AppExit()
{
	// Code to be called on Application Close.

	printf("AppExit");
}

void OnTimer()
{
    int BallX, BallY;
	int AccX, AccY, AccZ;
    // Get tilt (accelerometer) data for each axis.
	AccX = TiltGetx();
	AccY = TiltGety();
	AccZ = TiltGetz();    
    // Update the ball's next X and Y values based on G-Force and the strength of gravity:
    BallX = ViewGetx(BallView) + (AccX / 100);
	BallY = ViewGety(BallView) + (AccY / 100);
    // Don't let the ball go off the screen:
    // Stops for Landscape:
    if (BallY >= 731)
        BallY = 731;
    if (BallY <= 0)
        BallY = 0;
    if (BallX >= 987)
        BallX = 987;
    if (BallX <= 0)
        BallX = 0;   
/*
    // Stops for Portrait:
    if (BallX >= 731)
        BallX = 731;
    if (BallX <= 0)
        BallX = 0;
    if (BallY >= 987)
        BallY = 987;
    if (BallY <= 0)
        BallY = 0;
*/
	
    // Update the ball's position:
    ViewSetxy(BallView, BallX, BallY);
}
Posted

I'm not sure I understand your question completely, but if you want to eliminate diagonal movement then you need to use only the X or only the Y value, based on which is stronger. Capture both values and then compare them and select the one with the strongest tilt component. Then you update only that element of the ball's position so it will move up and down or left and right but not diagonally.
 
Share this answer
 
Richard as already suggested a good solution.
Using another approach you may decide to move the ball only if the tilt is decisively oriented into one of the allowed directions, discarding movements in the diagonal 'zone'.
The (squared, for semplicity) relative components of the tilt are:

Java
double rax2 = ((double)AccX*AccX/(AccX*AccX+AccY*AccY);
double ray2 = ((double)AccY*AccY/(AccX*AccX+AccY*AccY);


suppose you use a 30° degrees angle to discriminate, then:
  1. if rax2 >= 0.75 then you choose x movement.
  2. if ray2 >= 0.75 then you choose y movement.
  3. if both rax2 < 0.75 and ray2 < 0.75 then you choose no movement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900