Few Newbie questions and some C# code

Started by
4 comments, last by Ray J 13 years, 3 months ago
Hello,

I am a newbie to game development (and a newbie in this community also), so please do not scold me harshly :]

I decided to learn some basic game programing on C#. Usually i learn few little things before starting something more serious. I've stumbled upon few things, while trying to learn "drawing" basics.

a) What are the ways, to get a curve (path of missile lets say)? I am trying to use Bezier Curves, but they seem kinda... wrong, somehow.

b) Are there any formulas to determine if two Bezier curves have intersected each other? Or any sort of surfaces, like lets say, two ellipses?

c) Need a bit of a help with C# code on this one. Problem is - it's damn slow and i have no idea how to solve this problem, i think that i am doing something wrong when redrawing image, but i am not sure what. Code is a bit... crappy, but, i don't think that this is the actual problem in this case.

Quote:
        private System.Windows.Forms.Timer timer1; // magical timer        private SolidBrush megaBrush = new SolidBrush(Color.Red);        private Rectangle rect; // rectangle box        private float x1, x2, x3, y1, y2, y3, t; // ignore these for now        public Form1()        {            InitializeComponent();        }        private float get3(float i, char cord) // determine point in space        {            if (cord == 'X')            {                i = (((1 - i) * (1 - i)) * x1) + (2 * (1 - i) * i * x2) + (i * i * x3);            }            else            {                i = (((1 - i) * (1 - i)) * y1) + (2 * (1 - i) * i * y2) + (i * i * y3);            }            return i;        }        private void Form1_Load(object sender, EventArgs e)        {            rect = new Rectangle(0, 0, 30, 30);            t = 0.00f;            x1 = 0f; y1 = 0f; // p0            x2 = 150f; y2 = 300f; // p1            x3 = 300f; y3 = 0f; // p2            timer1 = new Timer();            timer1.Interval += 50;            timer1.Tick += new System.EventHandler(timer1_Tick);            timer1.Start();        }        private void timer1_Tick(object sender, System.EventArgs e)        {            Invalidate(); // redraw        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics gMan = e.Graphics;            gMan.FillRectangle(megaBrush, rect);            rect.X = (int)get3(t, 'X');            rect.Y = (int)get3(t, 'Y');            t += 0.005f;        }    }
Advertisement
i can help a with 'c'. Don't overload OnPaint on a form. Will almost always causes performance issues. Overloading OnPaint on an individual control is fine, but on a form, just causes problems. There are lots of reasons your form will redraw and can do it multiple times depending on what is going on.

theTroll
a) For hobby projects, I usually don't compute a path that the missile should follow exactly. Typically my missiles are either straight-line, affected by gravity and/or tracking. For each system, I give the missile position, velocity, and acceleration variables. Each frame, I reset acceleration to zero then add things like gravity, wind, and the direction the guidance system wants to go. Then I "integrate" them each frame:

velocity += acceleration * time_between_frames;position += velocity * time_between_frames;


Sometimes the order of those two lines is reversed, which affects how imprecision accumulates, but for simple games it usually doesn't matter. Some physics engines use much more complicated (and precise) ways of doing this, too.

This naturally results in a curving path when acceleration is non-zero.


b) With the per-frame integration method, you perform a quick check every frame to see if two objects are actually intersecting at that time (which is very simple unless you have lots of objects). I've never actually tried to use splines, so I don't know how to test them for intersection(s).
Thanks for the answers.


I've tried overriding OnPaint and
a) using an empty rectangle
b) using a small image instead of making him draw new stuff

It got a little bit faster, but it still doesn't reach 25 fps :[ I guess its time to move to C++.

Does ogl or dx have a 2d support? I heard that dx had DirectDraw, but it became "decapitated" (or something like that).
Quote:Original post by Ray J
It got a little bit faster, but it still doesn't reach 25 fps :[ I guess its time to move to C++.


There is no need to move to C++ to get framerates higher than 25fps. Overriding OnPaint in C++ will give you the same results, what you need to do is move away from overriding OnPaint.

Try switching to XNA and using SpriteBatch to draw your shapes.
Quote:Original post by adt7
Quote:Original post by Ray J
It got a little bit faster, but it still doesn't reach 25 fps :[ I guess its time to move to C++.


There is no need to move to C++ to get framerates higher than 25fps. Overriding OnPaint in C++ will give you the same results, what you need to do is move away from overriding OnPaint.

Try switching to XNA and using SpriteBatch to draw your shapes.



I've read up a bit on XNA and i am just wondering is it really a good... "tool"?

Just out of curiosity - Are there any commercial games made with the help of XNA? Sorry, it's just an old stereotype towards Microsoft...

This topic is closed to new replies.

Advertisement