Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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:
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:
//====================================================
// 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 25-Nov-12 6:49am
shelby672.5K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100