public
Last active — forked from /Allformvariables.h

  • Download Gist
Allformvariables.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#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;
}
 
};
MotionThread.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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;*/
 
}
};
Projectile.h
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#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;
 
}
 
 
 
 
};
Simulation.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.