• 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
shawnre

Display variable information in label in C#

10 posts in this topic

I created a simple form in Windows to display values of my variables as different inputs were detected. Label1 = Rotation angle Label2 = X-Coordinate Label3 = Y-Coordinate I used labels as illustrated above to display the values of the variables. The problem I see is in my rotation angle. As it steps from 0, right now in .03 increments, I expect it to return back to zero. Sometimes that happens, sometimes it doesnt. In other words, if I decrement by .03, then add .03, I expect to be back at zero. Sometimes yes, sometimes no. I am guessing the the conversion from a float to a string is causing the problem, so, question is how do i display my variable values then without having to convert to string?
0

Share this post


Link to post
Share on other sites
When you convert the float into a string are you using the roundtrip option? If not, try using:


// x != y (well, it *might*)
float x = .03f;
float y = float.Parse(x.ToString());

// x == y
float x = .03f;
float y = float.Parse(x.ToString("R"));




If you don't use the "R" option with ToString() you get additional digits which is probably whats messing up the arithmetic.
0

Share this post


Link to post
Share on other sites
LOL, sorry man, not sure what you mean. I'm such a noob, lmao.

Anyhow, this is what I have so far:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = Convert.ToString(rotation_angle);
label2.Text = Convert.ToString(x_coordinate);
label3.Text = Convert.ToString(y_coordinate);
}

float rotation_angle= 0;
int x_coordinate = 100;
int y_coordinate = 100;

public void Updatelabels()
{
label1.Text = Convert.ToString(rotation_angle);
label2.Text = Convert.ToString(x_coordinate);
label3.Text = Convert.ToString(y_coordinate);

}

private void button1_Click(object sender, EventArgs e)
{
y_coordinate += 1;
Updatelabels();

}

private void button3_Click(object sender, EventArgs e)
{
y_coordinate -= 1;
Updatelabels();
}

private void button2_Click(object sender, EventArgs e)
{
rotation_angle -= .03f;
Updatelabels();
}

private void button4_Click(object sender, EventArgs e)
{
rotation_angle += .03f;
Updatelabels();
}

}
}


0

Share this post


Link to post
Share on other sites
I guess maybe instead of doing the conversions this way:


label1.Text = Convert.ToString(rotation_angle);


you can try just:


label1.Text = rotation_angle.ToString("R");


everything in c# has a ToString method, don't need to use a Converter or something like that to make the cast.
This way you can use the round up idea previously put by ontheheap

Give it a shot, see if works
0

Share this post


Link to post
Share on other sites
Quote:
Original post by MACSkeptic
I guess maybe instead of doing the conversions this way:


label1.Text = Convert.ToString(rotation_angle);


you can try just:


label1.Text = rotation_angle.ToString("R");


everything in c# has a ToString method, don't need to use a Converter or something like that to make the cast.
This way you can use the round up idea previously put by ontheheap

Give it a shot, see if works


Done, still same sort of problem. Reach + or - .12 and trying to go back to zero does not work. Would it be safe to assume that it is this conversion from float to text that is causing the error? I just dont see how stepping backwards from say .12 ends up giving me -3.7252903E-09. Granted this is close to zero, but, not where we started despite going 4 up and 4 down in increments of .03

Assuming that it is just the "cast" as it was referred to, with these 3 variables is it now possible to determine x and y coordinates based on the rotationangle? That is the heart of the matter that this "simple" program was supposed to help me with. Evidentally I need to use Sin and Cos to attempt this. Assuming a 0 rotation is pointing straight right, and the user rotates, how to adjust x and y based off the rotation?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by shawnre
LOL, sorry man, not sure what you mean. I'm such a noob, lmao.

Anyhow, this is what I have so far:

*** Source Snippet Removed ***


Ah, OK. I was thinking you were *getting* the values from the labels, not just setting them. I think your problem comes from the inaccuracy of float. Try using the Decimal type instead. Keep in mind that Decimal is larger than float or double and operations are slower. Information on floating point numbers in .NET can be found here
0

Share this post


Link to post
Share on other sites
Speed is not really an issue in this context. However, your comment has brought up another question in addition to ones added by way of edit above your latest comment ontheheap.

I believe I read that rotation angles are done in radians (Correct?)
If so, can the variable rotation_angle still be decimal as you suggest?

Maybe I should read your link first, but, I jumped the gun.

*Edit* - Nice link, and yes, more than likely it was the float, tried with double and worked just fine.
0

Share this post


Link to post
Share on other sites
And to correct what I just said, it is sin and cos that work in radians, in case anyone reads this, dont want to confuse anyone.

Now, based off the revised code, I am assuming that calculating the x and y coordinates can now be done using the rotation_angle.

Does the positive rotation_angle move clockwise or counter?


[Edited by - shawnre on December 17, 2006 1:18:59 PM]
0

Share this post


Link to post
Share on other sites
I believe the positive rotation angle would move counterclockwise. See if this helps:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
decimal rotation_angle;
decimal x_location;
decimal y_location;

public Form1()
{
InitializeComponent();
}

public void updateFields()
{
angleTxtBox.Text = rotation_angle.ToString();
xLocTxtBox.Text = x_location.ToString();
yLocTxtBox.Text = y_location.ToString();
}

public void updateShip()
{
shipLbl.Location = new Point((int)x_location, (int)y_location);
}

private void Form1_Load(object sender, EventArgs e)
{
rotation_angle = 0.0M;
x_location = 100;
y_location = 100;

updateFields();
}

private void rotateLeftBtn_Click(object sender, EventArgs e)
{
rotation_angle = rotation_angle + .03M;
updateFields();
}

private void rotateRightBtn_Click(object sender, EventArgs e)
{
rotation_angle = rotation_angle - .03M;
updateFields();
}

private void forwardBtn_Click(object sender, EventArgs e)
{
x_location -= (decimal)(Math.Sin((double)rotation_angle));
y_location -= (decimal)(Math.Cos((double)rotation_angle));
updateShip();
}

private void backwardBtn_Click(object sender, EventArgs e)
{
x_location += (decimal)(Math.Sin((double)rotation_angle));
y_location += (decimal)(Math.Cos((double)rotation_angle));
updateShip();
}
}
}

0

Share this post


Link to post
Share on other sites
Hey hey thanx ontheheap, exactly what I was trying to do. Didnt mean for you to do it though, but, pretty much answered all my questions. Thanks for your help, though I do have one more question.

rotation_angle = rotation_angle + .03M;

What exactly is the purpose of the M after .03??
0

Share this post


Link to post
Share on other sites
Check out the C# Language Specification (Section 9.4.4.3).

Quote:

If no real-type-suffix is specified, the type of the real literal is double. Otherwise, the real-type-suffix determines the type of the real literal, as follows:

  • A real literal suffixed by F or f is of type float. [Example: The literals 1f, 1.5f, 1e10f, and 123.456F are all of type float. end example]

  • A real literal suffixed by D or d is of type double. [Example: The literals 1d, 1.5d, 1e10d, and 123.456D are all of type double. end example]

  • A real literal suffixed by M or m is of type decimal.


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