Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Help

Options
  • 06-10-2007 2:27am
    #1
    Registered Users Posts: 3,803 ✭✭✭


    Hi, I have a assignment for college and I need a some help. The assignment is to show the movement of an object after it has be thrown/shot into the air. I have very little experience in drawing graphics in c++.

    I am using a MFC project.

    This is what I have in OnDraw() in the ProjectNameView.cpp (forgive my coding, I have been adding, removing, changing constantly trying different methods):
    int u, a, xVel, yVel, angle, angle2;
            double gravity = 9.81, t;
    
    	float x = 250;
    	float y = 250;
    
    	u = 2;
    	a = 6;
    	xVel = 25;
    	yVel = 50;
    	angle = 75;
    	angle2 = 45;
    	t = 0;
    
    	CString str1 = "T      X     Y\n------------------------------\n";
    	char let[10];
    
    	CRect textBox;
    	textBox.top = 25; // the area where text will be display;
    	textBox.left = 400;
    	textBox.bottom = 425;
    	textBox.right = 600;
    
    	Matrix m( cos(angle2), sin(angle2), 0, 0,
    				-sin(angle2), cos(angle2), 0, 0,
    				0, 0, 1, 0,
    				0, 0, 0, 1);
    	Vector v(0,0,1);
    	
        for (t=0; t <= 10; t+=0.5)
        {
            x = (xVel*cos(angle*PI/180))*t;
            y = (yVel*sin(angle*PI/180))*t - 0.5*gravity*(t*t);
    
    // Move the x and y points back to the orign to rotate them.
    		v.x = x - x; 
    		v.y = y - y;
    		v = v.VectorRotation(m);
    
    		sprintf(let, "%.5f", t); //get and store the time, x and y coords in a string
    		str1 += let;
    		str1 += ", ";
    
    		sprintf(let, "%.5f", x);
    		str1 += let;
    		str1 += ", ";
    
    		sprintf(let, "%.5f", y);
    		str1 += let;
    		str1 += "\n "; 
    
    		BallFunctions::DrawBall(pDC, (x + 250), (y + 250) );
        }
    
    	pDC->DrawText(str1, textBox, DT_NOPREFIX); // print the time, x and y coords to the text area
    }
    

    And this is what I have got in BallFunctions.cpp:
    float radius = 20, r2;
    	float y, x; // used for the algorithm to draw points on the border
    
    	int angle2 = 45;
    
    	float tempX, tempY;
    
    	r2 = radius * radius; // the radius sqaured
    
    	pDC->SetPixel( x_center, y_center + radius, 0x00000000 );
    	pDC->SetPixel( x_center, y_center - radius, 0x00000000 );
    	pDC->SetPixel( x_center + radius, y_center, 0x00000000 );
    	pDC->SetPixel( x_center - radius, y_center, 0x00000000 );
    
    	x = 1;
    	y = ( sqrt( r2 - 1 ) + 0.5 );
    
    	while( x < y ) 
    	{
    		pDC->SetPixel( x_center + x, y_center + y, 0x00000000 );
    		pDC->SetPixel( x_center + x, y_center - y, 0x00000000 );
    		pDC->SetPixel( x_center - x, y_center + y, 0x00000000 );
    		pDC->SetPixel( x_center - x, y_center - y, 0x00000000 );
    		pDC->SetPixel( x_center + y, y_center + x, 0x000000ff );
    		pDC->SetPixel( x_center + y, y_center - x, 0x00000000 );
    		pDC->SetPixel( x_center - y, y_center + x, 0x00000000 );
    		pDC->SetPixel( x_center - y, y_center - x, 0x00000000 );
    
    		++x;
    		y = ( sqrt( r2 - x * x ) + 0.5 );
    	}
    	if (x == y) 
    	{
    		pDC->SetPixel( x_center + x, y_center + y, 0x000000ff );
    		pDC->SetPixel( x_center + x, y_center - y, 0x000000ff );
    		pDC->SetPixel( x_center - x, y_center + y, 0x000000ff );
    		pDC->SetPixel( x_center - x, y_center - y, 0x000000ff );
    	}
    }
    

    What I want to do is make the circle visually rotate. From the method that I use above to draw the circle, I think that I would have to rotate each point that makes up the circle, but that is a long process. I was also thinking on drawing a line in the centre of the circle, and rotate it, but I dont know how I would do that.

    Also, I need to be able to take user input (let the user set the gravity etc.)

    So with all that in mind, here are a few questions i have:

    1) I am using a MFC document to make the program, is there a better document type for what I want? Again, I have dont very little in the form of graphics

    2) Any suggestions on how to make the circle rotate visually?

    3) Are there functions to allow the user to enter in values in a MFC document?

    Any help is appreciated, as this is beginning to annoy me :mad:


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    1) I am using a MFC document to make the program, is there a better document type for what I want? Again, I have dont very little in the form of graphics

    2) Any suggestions on how to make the circle rotate visually?

    3) Are there functions to allow the user to enter in values in a MFC document?
    OK a document/view is fine for your purposes, if you want user input you can either have a new class inherited from a CFormView and use a splitter control to split that and the view. Then as you enter your values into the CFormView (which is just a form that you design) you can get the values you enter and pass them into your drawing algorithm. There's functions for this, such as GetDlgItem, give the MSDN a good look at and you should find more about these functions. The idea of the document is to store data central to your application, so you can associate a member variable in the document with a control in your FormView/Dialog, like a text box for example. So you can easily get access to the data at any stage, by gaining a pointer to your document.

    OK looking at your circle problem, you seem to be over complicating things a bit, there's plenty of code in the windows API to simplify things for you. Personally I'd load a bitmap image of a ball, put it in a resource, call LoadBitmap, then use the GDI functions to render it to the screen and move it accordingly along a path which your algorithm is generating anyway. I'd store this information in a vector of CPoints or any other structure you prefer (MFC has some templated containers which might also be useful).

    Have a look at the BitBlt function, it may be of help, and get it to slowly move the bitmap across the path generated.


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    hmm, that seems a bit too complicated for me im afraid :o We have been taught very little in c++, focusing on java instead, and even in java we have done no graphics. Never the less, I take on board what you said and will check out the MSDN.

    Thanks for your help.


  • Registered Users Posts: 981 ✭✭✭fasty


    I wouldn't bother with splitter windows, they're a bit unintuitive. Just make a project whose view derives from CFormView and look up how to add custom controls into dialogs.

    You could make a custom canvas class from CWnd, override it's draw method and give it a timer that updates it at certain times and add that to your form. You will also need to look into double buffering or it'll flicker like made but there's tonnes of sample code on this too.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Ok fair enough, MFC can be a bit of a pain in the arse to get used to. If you're interested in learning it though, there's one book I'd recommend getting: "The MFC Answer book" or "MFC FAQ" can't remember the exact title now. I'm pretty sure the author is Eugene Kane. Have a search for it on amazon, its brilliant at explaining stuff in a FAQ style, and stuff that a lot of programmers would find useful.

    [EDIT]: [url="http://www.amazon.co.uk/MFC-Answer-Book-Solutions-Applications/dp/0201185377/ref=sr_1_1/026-1739598-2895607?ie=UTF8&s=books&qid=1191785206&sr=8-1] link[/url]


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    Thanks for all the help. I was able to make the dialogs appear. Thanks again.


  • Advertisement
Advertisement