• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Joker123

How can I use Windows Phone sensors to move an object?

3 posts in this topic

I want to use the Windows Phone sensors to move a ball in a Windows Phone game. I tried to move the ball with the accelerometer, but the ball doesn't move correctly if I tilt my Windows Phone device(Lumia 920) in a direction. 
For example, if I tilt the device to the left, the ball isn't moving to the left, instead the ball moves very strange. I don't know what I could change in my code so that the ball moves correctly.
What is wrong? Why is the ball not moving in the right direction when I tilt my Windows Phone device?

What should I change so that the ball moves correctly if I tilt the Windows Phone device?

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Motion motion;
    Texture2D Ball;
    Vector2 BallPos = new Vector2(400, 300);
    Vector2 BallVelocity;

    void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
    {
        UpdateUI(e.SensorReading);
    }

    private void UpdateUI(MotionReading e)
    {               
        float accelerometerX = e.DeviceAcceleration.X;
        float accelerometerY = e.DeviceAcceleration.Y;
        speed.X += accelReading.X;
        speed.Y += -accelReading.Y;
        BallPos.X += speed.X *0.05f;
        BallPos.Y += speed.Y *0.05f;
    }

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        TargetElapsedTime = TimeSpan.FromTicks(333333);
        InactiveSleepTime = TimeSpan.FromSeconds(1);
        graphics.IsFullScreen = true;
    }

    protected override void Initialize()
    {
        if (Motion.IsSupported)
        {
            motion = new Motion();
            motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
            motion.Start();
        }

        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Message = Content.Load<SpriteFont>("Arial");
        Ball = Content.Load<Texture2D>("ballbig");
    }

    protected override void Update(GameTime gameTime)
    {

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
          spriteBatch.Draw(Ball, BallPos, null, Color.White, 0, new Vector2(Ball.Width/2,Ball.Height/2), 1f, SpriteEffects.None,1);          
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
0

Share this post


Link to post
Share on other sites

I haven't messed with the motion sensors on the windows phone, but you may need to take average out the inputs as opposed to directly translating the accelerometer input to values.  A person will not move a phone steadily.  The values will constantly move up and down which will cause your motion to be jagged and it will behave weird. 

 

I would take a couple of readings (couple of nano seconds?) and average the readings to see where it's mostly moving towards.  I would then apply that number to the acceleration for my object and update the object within the update(gameTime) method.  As for how you would implement this, I would google how others have done it.  Maybe google "interpolating accelerometer" or something of that sort would help.

 

EDIT: Actually interpolation ISN'T what you would use here.  See if this helps you out.  Also, I think you may need to call motion.Start() in your initialization code

 

http://blogs.windows.com/windows_phone/b/wpdev/archive/2010/09/08/using-the-accelerometer-on-windows-phone-7.aspx

Edited by SonicD007
0

Share this post


Link to post
Share on other sites

I don't know how to use this formula in my code: On = On-1 + ?(In – On-1)

How can I use the formula or a similar formula in my code?

0

Share this post


Link to post
Share on other sites

I don't know how to use this formula in my code: On = On-1 + ?(In – On-1)

How can I use the formula or a similar formula in my code?

I skimmed through the article and it seems they're using extrapolation to smooth out the data.  Someone else can chime in if this is too overkill for what you're doing, but the formula seems to be the formula of a line i.e. y= m(x-x1) + y1

 

If you've taken calc 1 that formula should be familiar to you (or pre calc)

I'm not sure what On-1 stands for in this context, it seems like it's an initial reading.  Maybe it's the first reading that's gotten over the  50 inputs they get?

 

In is the raw input data which the sensor would give you.  I haven't researched if the SDK gives you the reading filtered or not or if it really is raw data.  You'll have to research that.  Hopefully something on the wikipedia page can help you a bit more.

 

http://en.wikipedia.org/wiki/Extrapolation

 

The article is using linear extrapolation

 

EDIT: From what I gathered, you would use the formula to sample 50 inputs within a given amount of time and average them out using that formula to have a smooth input. alpha (?) would be 0 to 1 which is basically how much smoothing should be done.  It's a constant.

Edited by SonicD007
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0