Create a gist now

Instantly share code, notes, and snippets.

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
float timetaken;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
ref class Allformvariables
{
public:
Allformvariables(void);
//variables used on multiple forms
Point initialVelocity;
float WindSpeed;
float Gravity;
float restitution;
char WindDirection;
Point MouseStart;
Point MouseEnd;
Point Velocity;
float AirResistance;
float Area;
//Set items
public: void Setfloats(float x, int change ) {
switch (change)
{
case 1:
WindSpeed = x;
break;
case 2:
Gravity = x;
break;
case 3:
restitution = x;
break;
}
}
public: void Setchar(char n, int change) {
switch(change)
{
case 1:
WindDirection = n;
break;
}
}
public: float TextintValidation(float Min, float Max, String^ text){
float x;
String^ strMin = Convert::ToString(Min);
String^ strMax = Convert::ToString(Max);
try{
x = Convert::ToDouble(text);
if(x < Min){
MessageBox::Show("Please enter a value greater than {0}", strMin);
}
else if(x > Max){
MessageBox::Show("Please enter a value smaller than {0}", strMax);
}
else{
return(x);}
}
catch(FormatException^ e){
if(text==""){
}
else{
MessageBox::Show("Please enter a Decimal value");
}}
}
public: Point CalcinitalVelocity(){
initialVelocity.X = (MouseEnd.X -MouseStart.X)/100;
initialVelocity.Y = (MouseStart.Y - MouseEnd.Y)/100;
return Velocity;
}
public: float CalcAirresistance(float PFF){
AirResistance = (WindSpeed*WindSpeed)*PFF*Area;
return AirResistance;
}
public: float CalcCurrentVelocity(Object^ data){
Velocity.X = initialVelocity.X + (AirResistance/Area)*timetaken;
Velocity.Y = initialVelocity.Y + Gravity*timetaken;
return Velocity.X;
}
};
#pragma once
#include <time.h>
using namespace System::Threading;
using namespace System::Drawing;
ref class MotionThread
{
public:
MotionThread(void);
Point Velocity;
Point ProjectilePos;
public: void MoveProjectile(Object^ data){
//ProjectilePos1.X = ProjectilePos1.X += Velocity1.X;
//ProjectilePos1.Y = ProjectilePos1.Y += Velocity1.Y;
/*return ProjectilePos1;*/
}
};
#pragma once
using namespace System::Drawing;
ref class Projectile
{
public:
Projectile(void);
int width;
int height;
float PFF;
float projectileRest;
float SurfaceArea;
char Type;
Point ProjectilePos;
public: void setProjectile(int chosen){
switch (chosen){
case 0: //Arrow
width =25;
height =5;
PFF = 0.2;
projectileRest = 0;
Type = 'Rect';
break;
case 1: //Basketball
width =20;
height =20;
PFF=0.6;
projectileRest = 0.6;
Type = 'Circ';
break;
case 2: //BouncyBall
width =10;
height =10;
PFF=0.1;
projectileRest = 0.8;
Type = 'Circ';
break;
case 3: //Cannonball
width = 18;
height = 18;
PFF = 0.8;
projectileRest = 0.001;
Type = 'Circ';
break;
case 4: //Football
width = 19;
height = 19;
PFF = 0.5;
projectileRest = 0.4;
Type = 'Circ';
break;
case 5: //frisby
width = 19;
height = 5;
PFF = 0.05;
projectileRest = 0.001;
Type = 'Rect';
break;
case 6: //tennisball
width = 13;
height = 13;
PFF = 0.3;
projectileRest = 0.2;
Type = 'Circ';
break;
}
SurfaceArea = (width*height)/100;
}
};
#pragma once
#include "Allformvariables.h"
#include "Projectile.h"
#include "MotionThread.h"
#include <time.h>
namespace ProjectileMotion {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
this->SetStyle(static_cast<ControlStyles>(
ControlStyles::AllPaintingInWmPaint |
ControlStyles::UserPaint |
ControlStyles::OptimizedDoubleBuffer) , true);
this->UpdateStyles();
//initialize the start position of circle
Projectile1.ProjectilePos = Point(0,(Screen::PrimaryScreen->WorkingArea.Height-51));
Screen_width = Screen::PrimaryScreen->WorkingArea.Width;
Screen_height = Screen::PrimaryScreen->WorkingArea.Height;
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ txtWindS;
protected:
private: System::Windows::Forms::TextBox^ txtGravity;
private: System::Windows::Forms::TextBox^ txtResti;
private: System::Windows::Forms::TextBox^ txtWindD;
private: System::Windows::Forms::Button^ Exit;
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::Label^ lblWindV;
private: System::Windows::Forms::Label^ lblRestitution;
private: System::Windows::Forms::Label^ lblGravity;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->txtWindS = (gcnew System::Windows::Forms::TextBox());
this->txtGravity = (gcnew System::Windows::Forms::TextBox());
this->txtResti = (gcnew System::Windows::Forms::TextBox());
this->txtWindD = (gcnew System::Windows::Forms::TextBox());
this->Exit = (gcnew System::Windows::Forms::Button());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->lblWindV = (gcnew System::Windows::Forms::Label());
this->lblRestitution = (gcnew System::Windows::Forms::Label());
this->lblGravity = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// txtWindS
//
this->txtWindS->Location = System::Drawing::Point(12, 22);
this->txtWindS->Name = L"txtWindS";
this->txtWindS->Size = System::Drawing::Size(71, 20);
this->txtWindS->TabIndex = 0;
this->txtWindS->Text = L"WindSpeed";
this->txtWindS->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::txtWindS_KeyPress);
this->txtWindS->Leave += gcnew System::EventHandler(this, &MyForm::txtWindS_Leave);
//
// txtGravity
//
this->txtGravity->Location = System::Drawing::Point(118, 22);
this->txtGravity->Name = L"txtGravity";
this->txtGravity->Size = System::Drawing::Size(49, 20);
this->txtGravity->TabIndex = 1;
this->txtGravity->Text = L"Gravity";
this->txtGravity->Leave += gcnew System::EventHandler(this, &MyForm::txtGravity_Leave);
//
// txtResti
//
this->txtResti->Location = System::Drawing::Point(173, 22);
this->txtResti->Name = L"txtResti";
this->txtResti->Size = System::Drawing::Size(65, 20);
this->txtResti->TabIndex = 2;
this->txtResti->Text = L"Restitution";
this->txtResti->Leave += gcnew System::EventHandler(this, &MyForm::txtResti_Leave);
//
// txtWindD
//
this->txtWindD->Location = System::Drawing::Point(89, 22);
this->txtWindD->Name = L"txtWindD";
this->txtWindD->Size = System::Drawing::Size(23, 20);
this->txtWindD->TabIndex = 3;
this->txtWindD->Text = L"N";
//
// Exit
//
this->Exit->Location = System::Drawing::Point(678, 1);
this->Exit->Name = L"Exit";
this->Exit->Size = System::Drawing::Size(25, 23);
this->Exit->TabIndex = 4;
this->Exit->Text = L"X";
this->Exit->UseVisualStyleBackColor = true;
this->Exit->Click += gcnew System::EventHandler(this, &MyForm::Exit_Click);
//
// comboBox1
//
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^ >(7) {L"Arrow", L"Basketball", L"Bouncy Ball", L"Cannon Ball",
L"Football", L"Frisby", L"Tennisball"});
this->comboBox1->Location = System::Drawing::Point(244, 22);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 21);
this->comboBox1->TabIndex = 5;
this->comboBox1->Text = L"Select Projectile";
this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::comboBox1_SelectedIndexChanged);
//
// lblWindV
//
this->lblWindV->AutoSize = true;
this->lblWindV->Location = System::Drawing::Point(12, 6);
this->lblWindV->Name = L"lblWindV";
this->lblWindV->Size = System::Drawing::Size(72, 13);
this->lblWindV->TabIndex = 6;
this->lblWindV->Text = L"Wind Velocity";
//
// lblRestitution
//
this->lblRestitution->AutoSize = true;
this->lblRestitution->Location = System::Drawing::Point(170, 6);
this->lblRestitution->Name = L"lblRestitution";
this->lblRestitution->Size = System::Drawing::Size(57, 13);
this->lblRestitution->TabIndex = 7;
this->lblRestitution->Text = L"Restitution";
//
// lblGravity
//
this->lblGravity->AutoSize = true;
this->lblGravity->Location = System::Drawing::Point(115, 6);
this->lblGravity->Name = L"lblGravity";
this->lblGravity->Size = System::Drawing::Size(40, 13);
this->lblGravity->TabIndex = 8;
this->lblGravity->Text = L"Gravity";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->AutoScroll = true;
this->ClientSize = System::Drawing::Size(703, 381);
this->ControlBox = false;
this->Controls->Add(this->lblGravity);
this->Controls->Add(this->lblRestitution);
this->Controls->Add(this->lblWindV);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->Exit);
this->Controls->Add(this->txtWindD);
this->Controls->Add(this->txtResti);
this->Controls->Add(this->txtGravity);
this->Controls->Add(this->txtWindS);
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = L"MyForm";
this->Text = L"Simulation";
this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::MyForm_MouseDown);
this->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::MyForm_MouseUp);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
int SelectedIndex;
MotionThread MoveProj;
Allformvariables Variables;
Projectile Projectile1;
float Screen_width;
float Screen_height;
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
Randomize();
txtWindS->Text=Variables.WindSpeed.ToString();
txtWindD->Text=Variables.WindDirection.ToString();
txtGravity->Text=Variables.Gravity.ToString();
txtResti->Text=Variables.restitution.ToString();
}
private: void Randomize(){
float x;
char n;
x = randomnum1(0.1,80.0,80); //WindSpeed
Variables.Setfloats(x, 1);
x = 10; //randomnum1(0.1,18.0,18); //Gravity
Variables.Setfloats(x, 2);
x = -(randomnum1(0.001, 1.0, 1)); //Restitution
Variables.Setfloats(x, 3);
n =(randomchar(2)); // WindDirection
Variables.Setchar(n, 1);
}
//Random Number Generator
float randomnum1(float min, float max, int randmax)
{
float RandomVal;
srand( unsigned(time(NULL)));
for(int i = 0;i<= max ; i++)
{
RandomVal=(float)rand()/(RAND_MAX + 1)+(rand()%randmax);
RandomVal=round_num(RandomVal);
}
return RandomVal;
}
///////
//Set WindDirection
char randomchar(int max){
char y;
srand(time(NULL));
int RandomVal = rand() % (max)+1;
switch (RandomVal)
{
case 1:
y = 'N';
break;
case 2:
y = 'S';
break;
}
return y;
}
//Float round
//
//
float round_num(float value){
float nearest = floorf(value * 100 + 0.5) / 100.00;
return nearest;
}
//
//Randomizeing ends here
private: System::Void Exit_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
SelectedIndex = comboBox1->SelectedIndex;
Projectile1.setProjectile(SelectedIndex);
this->Invalidate();
this->Update();
}
// Display the chosen Projectile
public:
virtual void OnPaint(PaintEventArgs^ e) override {
Form::OnPaint( e );
Graphics^ g = e->Graphics;
g->SmoothingMode = Drawing2D::SmoothingMode::HighQuality;
// Place holder
g->DrawEllipse(Pens::Black,Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, 25, 25);
g->FillEllipse(Brushes::Red, Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, 25, 25);
//place holder end
/*switch(Projectile1.Type){
case 'Rect':
g->DrawRectangle(Pens::Black, Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, Projectile1.width, Projectile1.height);
g->FillRectangle(Brushes::Red, Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, Projectile1.width, Projectile1.height);
break;
case 'Circ':
g->DrawEllipse(Pens::Black,Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, Projectile1.width, Projectile1.height);
g->FillEllipse(Brushes::Red, Projectile1.ProjectilePos.X, Projectile1.ProjectilePos.Y, Projectile1.width, Projectile1.height);
break;
}*/
}
private: System::Void txtWindS_Leave(System::Object^ sender, System::EventArgs^ e) {
Variables.WindSpeed = Variables.TextintValidation(0.1,80.0,txtWindS->Text);
}
private: System::Void txtGravity_Leave(System::Object^ sender, System::EventArgs^ e) {
Variables.Gravity = Variables.TextintValidation(0.1,18.0,txtGravity->Text);
}
private: System::Void txtResti_Leave(System::Object^ sender, System::EventArgs^ e) {
Variables.restitution = - Variables.TextintValidation(-0.001, -1.00, txtResti->Text);
}
private: System::Void txtWindS_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
if (e->KeyChar == (char)13){
}
}
//ALL CALCULATIONS START HERE
private: System::Void MyForm_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
Variables.MouseStart = MousePosition;
}
private: System::Void MyForm_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
Variables.MouseEnd = MousePosition;
Variables.Area = Projectile1.SurfaceArea;
Variables.Velocity = Variables.CalcinitalVelocity();
Variables.AirResistance =Variables.CalcAirresistance(Projectile1.PFF);
}
private: Void startThread(){
MoveProj.Velocity = Variables.Velocity;
MoveProj.ProjectilePos = Projectile1.ProjectilePos;
Thread^ MotionThread1 = gcnew
Thread(gcnew ParameterizedThreadStart(MoveProj ,&MotionThread::MoveProjectile));
Thread^ MainThread = gcnew Thread(gcnew ThreadStart(this, &Allformvariables::CalcCurrentVelocity));
}
};
};
//// Drawing basic ball until find workaround for combo box projectile select.
//sorting out text boxes so ENTER key leaves focus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment