seangriffin Posted May 26, 2017 Posted May 26, 2017 (edited) This is my first release of Box2D for AutoIT. A very fun and exciting UDF to be bringing to you all. For those who don't know, Box2D is a physics engine (see http://box2d.org/). It simulates physical systems using rigid bodies and collision detection. It is arguably the most prevalent physics engine in existence, within popular physics-based games like Angry Birds using it, and is also at the core of other advanced physics engines such as Unity. For a quick demonstration of this UDF in action see the following YouTube video -> https://youtu.be/h5QH1O63Wik Or play "Angry Nerds", the demo game, for yourself. Visit the EXAMPLES section below. Box2D is purely a mathematical engine. It can model physics numerically. This in itself is quite interesting, however it really shines when applied to a good graphics engine. Recently I posted my SFML UDF over here ... After benchmarking several popular graphics engines (i.e. GDI+, Direct2D, Irrlicht and SFML) with this Box2D UDF I've selected SFML as my favourite, and the engine that I believe performs the best (fastest) with a rich set of functions appropriate for any physics engine. With Box2D married with SFML, and running on AutoIT, the results are stunning. A HUGE THANK-YOU to jRowe and linus (for the Irrlicht UDF) and UEZ for his post on GDI+ loading screens and trancexx and Eukalyptus for their work on Direct2D. Without their talents I would not have been able to reach this point on my own. The Box2D library is available for C++. Way back in 2010 it was ported to Pure C by "Paril" and made available on Google Code and known as "Box2C". Google Code has since been shut down but the but the archive of Box2C still exists to this day here -> https://code.google.com/archive/p/box2c. This is the library which I have ported to AutoIT in this UDF. SFML I am also porting to AutoIT under a separate UDF, as linked above. Building this UDF has been a dream come true. I've been fascinated by physics-based games as far back as the golden age of gaming in the 80's, with thrust style games like Asteroids and platformers like Donkey Kong. I admired game developers like Jeremy Smith who created what may have been the first true game physics engines for the home computers. I was astonished by their talents in games like Thrust and Exile for the C64. Over the years I've attempted to mimic at least some of their success in my own games but alas I can not match their skills. Now much older automation tools have become my game. I use them almost every day, AutoIT included. I've dabbled in other languages for physics game development, like Scratch and Unity, but sadly I had wondered why AutoIT, with all it's glorious capabilities and rapid scripting features, didn't have such a feature. Hence this UDF. This UDF demands a big time investment, and I am time poor, but I have a keen interest in the topic and will do my best to continue it's development. I am only a hobbyist game developer and welcome comments and suggestions from those far smarter than I on this topic. REQUIREMENTS: AutoIt3 3.2 or higher LIST OF FUNCTIONS (in progress): I've split this UDF into two halves. "Box2C.au3" is a UDF specifically for Box2C, the C API for Box2D. It provides the mathematics behind the engine. Additionally I'm providing "Box2CEx.au3" as an Extension that provides the graphics and gaming functions for Box2D. Within the core "Box2C.au3" UDF: _Box2C_Startup _Box2C_Shutdown _Box2C_b2Vec2_Constructor _Box2C_b2Vec2_Length _Box2C_b2Vec2_Distance _Box2C_b2World_Constructor _Box2C_b2World_CreateBody _Box2C_b2World_DestroyBody _Box2C_b2World_CreateFixture _Box2C_b2World_CreateFixtureFromShape _Box2C_b2World_Step _Box2C_b2BoxShape_Constructor _Box2C_b2CircleShape_Constructor _Box2C_b2PolygonShape_Constructor _Box2C_b2PolygonShape_Set _Box2C_b2PolygonShape_CrossProductVectorScalar _Box2C_b2PolygonShape_CrossProductVectorVector _Box2C_b2PolygonShape_Normalize _Box2C_b2PolygonShape_ComputeCentroid _Box2C_b2BodyDef_Constructor _Box2C_b2Body_DestroyFixture _Box2C_b2Body_GetPosition _Box2C_b2Body_SetPosition _Box2C_b2Body_GetAngle _Box2C_b2Body_SetAngle _Box2C_b2Body_SetAwake _Box2C_b2Body_SetTransform _Box2C_b2Body_GetLinearVelocity _Box2C_b2Body_SetLinearVelocity _Box2C_b2Body_GetAngularVelocity _Box2C_b2Body_SetAngularVelocity _Box2C_b2Body_ApplyForce _Box2C_b2Body_ApplyForceAtBody _Box2C_b2Body_ApplyDirectionalForceAtBody _Box2C_b2Body_ApplyTorque _Box2C_b2Fixture_GetShape _Box2C_b2Fixture_GetDensity _Box2C_b2Fixture_SetDensity _Box2C_b2Fixture_GetRestitution _Box2C_b2Fixture_SetRestitution _Box2C_b2Fixture_GetFriction _Box2C_b2Fixture_SetFriction _Box2C_b2Fixture_SetSensor Within y "Box2CEx.au3" extension: x_metres_to_gui_x y_metres_to_gui_y metres_to_pixels atan2 radians_to_degrees degrees_to_radians _Box2C_Setup_SFML _Box2C_b2Vec2_GetGUIPosition _Box2C_b2World_Setup _Box2C_b2World_GDIPlusSetup _Box2C_b2World_SetPixelsPerMetre _Box2C_b2World_SetGUIArea _Box2C_b2World_GetGUIArea _Box2C_b2World_GetGUIAreaCenter _Box2C_b2World_Create _Box2C_b2World_Step_Ex _Box2C_b2World_StartAnimation _Box2C_b2World_Animate_SFML _Box2C_b2World_Animate_GDIPlus _Box2C_b2World_WaitForAnimateEnd _Box2C_b2ShapeArray_AddItem_SFML _Box2C_b2ShapeArray_SetItem_SFML _Box2C_b2ShapeArray_GetItemImagePath_SFML _Box2C_b2PolygonShape_ArrayAdd_Irrlicht _Box2C_b2PolygonShape_ArrayAdd_GDIPlus _Box2C_b2BodyDefArray_AddItem _Box2C_b2FixtureArray_SetItemSensor _Box2C_b2FixtureArray_GetItemDensity _Box2C_b2FixtureArray_SetItemDensity _Box2C_b2FixtureArray_GetItemRestitution _Box2C_b2FixtureArray_SetItemRestitution _Box2C_b2FixtureArray_GetItemFriction _Box2C_b2FixtureArray_SetItemFriction _Box2C_b2BodyArray_AddItem_SFML _Box2C_b2Body_ArrayAdd_Irrlicht _Box2C_b2Body_ArrayAdd_GDIPlus _Box2C_b2BodyArray_GetItemCount _Box2C_b2BodyArray_GetItemPosition _Box2C_b2BodyArray_SetItemPosition _Box2C_b2BodyArray_GetItemAngle _Box2C_b2BodyArray_SetItemAngle _Box2C_b2BodyArray_GetItemLinearVelocity _Box2C_b2BodyArray_SetItemActive _Box2C_b2BodyArray_SetItemAwake _Box2C_b2BodyArray_SetItemImage_SFML _Box2C_b2BodyArray_SetItemDraw _Box2C_b2BodyArray_ApplyItemForceAtBody _Box2C_b2BodyArray_ApplyItemDirectionalForceAtBody _Box2C_b2BodyArray_Transform_SFML _Box2C_b2Body_Transform_GDIPlus _Box2C_b2BodyArray_Draw_SFML _Box2C_b2Body_ArrayDrawDisplay_SFML _Box2C_b2Body_Destroy _Box2C_b2Body_Destroy_SFML _Box2C_b2Body_Rotate_GDIPlus The SFML functions used in the tests and demos will be available in the SFML UDF post (see reference above). EXAMPLES: For such a powerful physics engine coupled to such a powerful graphics engine (SFML) it's sad that I've only had time to build one functional game, thus far. But it's a start. My self-titled "Angry Nerds" is merely a demo of the same game concept as the ever-so-popular Angry Birds game. Angry Birds itself is built on top of Box2D. Likewise Angry Nerds. AutoIT + Box2D + SFML to be exact. I've compiled Angry Nerds and provided an installer also, so you can quickly run the demo for yourself. From the Github site below (in the DOWNLOAD section) run "Box2C_Angry_Nerds_Game_SFML_installer.exe" to install the demo to a location on your computer (desktop by default). Go into this folder and run "Box2C_Angry_Nerds_Game_SFML.exe". All instructions are displayed in-game. Should be quite easy to work out. Aside from Angry Nerds there are also two test scripts: Box2C_linear_forces_test_SFML.au3 Box2C_angular_forces_test_SFML.au3 Feel free to run these for a basic demonstration of rigid bodies, forces and collisions. The heart of Box2D and any physics engine. Lastly I also have four speed tests as follows: Box2C_speed_test_SFML.au3 Box2C_speed_test_Irrlicht.au3 Box2C_speed_test_D2D.au3 Box2C_speed_test_GDIPlus.au3 These were my initial evaluations into a suitable graphics engine for Box2D. I've since settled on SFML, but feel free to execute these. Note they may become quick defective over time as the SFML functions slowly take over. DOWNLOADS: You can download this UDF, including the examples above and associated files, from the following GitHub page: https://github.com/seanhaydongriffin/Box2C-UDF Hope you all enjoy! I certainly am :-) Cheers, Sean. Edited May 26, 2017 by seangriffin UEZ, Danyfirex and tarretarretarre 3 Cheers, Sean. See my other UDFs: Chrome UDF - Automate Chrome | SAP UDF - Automate SAP | Java UDF - Automate Java Applications & Applets | Tesseract (OCR) UDF - Capture text from applications, controls and the desktop | Textract (OCR) UDF - Capture text from applications and controls | FileSystemMonitor UDF - File, Folder, Drive and Shell Monitoring | VLC (Media Player) UDF - Creating and controlling a VLC control in AutoIT | Google Maps UDF - Creating and controlling Google Maps (inc. GE) in AutoIT | SAPIListBox (Speech Recognition) UDF - Speech Recognition via the Microsoft Speech (SAPI) ListBox | eBay UDF - Automate eBay using the eBay API | ChildProc (Parallel Processing) UDF - Parallel processing functions for AutoIT | HyperCam (Screen Recording) UDF - Automate the HyperCam screen recorder | Twitter UDF - Automate Twitter using OAuth and the Twitter API | cURL UDF - a UDF for transferring data with URL syntax See my other Tools: Rapid Menu Writer - Add menus to DVDs in seconds | TV Player - Automates the process of playing videos on an external TV / Monitor | Rapid Video Converter - A tool for resizing and reformatting videos | [topic130531]Rapid DVD Creator - Convert videos to DVD fast and for free | ZapPF - A tool for killing processes and recycling files | Sean's eBay Bargain Hunter - Find last minute bargains in eBay using AutoIT | Sean's GUI Inspector - A scripting tool for querying GUIs | TransLink Journey Planner with maps - Incorporating Google Maps into an Australian Journey Planner | Automate Qt and QWidgets | Brisbane City Council Event Viewer - See what's going on in Brisbane, Australia
UEZ Posted May 26, 2017 Posted May 26, 2017 Very impressive work! The physic engine is really nice implemented! Just for my curiosity - how did "GDI+ loading screens" help you? Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
seangriffin Posted May 26, 2017 Author Posted May 26, 2017 (edited) Thanks UEZ. I had never developed graphics in Windows as low level as GDI before. Always preferring to use something simpler and higher level such as what Unity provides. I needed a graphics engine for Box2D and I knew that GDI might be my answer but I'd absolutely no clue where to start. Because of the popularity of your GDI loading screens topic I was immediately drawn to your work. Having fully functional GDI animation examples was the perfect way to explain to me, a novice, how GDI works. I've also admired your scripting skill for some time and knew it was going to be impressive, and it was. Your optimisation of the GDI calls is precisely whats required for a physics engine. also you introduced me to the low level and efficient timers in the Windows API that i didn't know existed. they help me run non time critical script away from the time critical physics animation loop in my script. If it weren't for your demos I would not be this far advanced with Box2D. A very big thanks to you UEZ you are brilliant! I really love the AutoIT community. Edited May 26, 2017 by seangriffin mLipok 1 Cheers, Sean. See my other UDFs: Chrome UDF - Automate Chrome | SAP UDF - Automate SAP | Java UDF - Automate Java Applications & Applets | Tesseract (OCR) UDF - Capture text from applications, controls and the desktop | Textract (OCR) UDF - Capture text from applications and controls | FileSystemMonitor UDF - File, Folder, Drive and Shell Monitoring | VLC (Media Player) UDF - Creating and controlling a VLC control in AutoIT | Google Maps UDF - Creating and controlling Google Maps (inc. GE) in AutoIT | SAPIListBox (Speech Recognition) UDF - Speech Recognition via the Microsoft Speech (SAPI) ListBox | eBay UDF - Automate eBay using the eBay API | ChildProc (Parallel Processing) UDF - Parallel processing functions for AutoIT | HyperCam (Screen Recording) UDF - Automate the HyperCam screen recorder | Twitter UDF - Automate Twitter using OAuth and the Twitter API | cURL UDF - a UDF for transferring data with URL syntax See my other Tools: Rapid Menu Writer - Add menus to DVDs in seconds | TV Player - Automates the process of playing videos on an external TV / Monitor | Rapid Video Converter - A tool for resizing and reformatting videos | [topic130531]Rapid DVD Creator - Convert videos to DVD fast and for free | ZapPF - A tool for killing processes and recycling files | Sean's eBay Bargain Hunter - Find last minute bargains in eBay using AutoIT | Sean's GUI Inspector - A scripting tool for querying GUIs | TransLink Journey Planner with maps - Incorporating Google Maps into an Australian Journey Planner | Automate Qt and QWidgets | Brisbane City Council Event Viewer - See what's going on in Brisbane, Australia
UEZ Posted May 26, 2017 Posted May 26, 2017 (edited) Thanks for your feedback. ^^ Well, GDI is much faster than GDI+ but I like the functions of GDI+ more to create better looking graphical stuff. But GDI+ is mostly too slow for really cool stuff. Probably much better is DirectX / OpenGL / OpenCL / ... Your solution looks also very good. Nice to read that my GDI+ examples were useful. Edited May 26, 2017 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
seangriffin Posted May 27, 2017 Author Posted May 27, 2017 Really useful UEZ. I'm sure you are helping many people in need such as myself. Cheers, Sean. See my other UDFs: Chrome UDF - Automate Chrome | SAP UDF - Automate SAP | Java UDF - Automate Java Applications & Applets | Tesseract (OCR) UDF - Capture text from applications, controls and the desktop | Textract (OCR) UDF - Capture text from applications and controls | FileSystemMonitor UDF - File, Folder, Drive and Shell Monitoring | VLC (Media Player) UDF - Creating and controlling a VLC control in AutoIT | Google Maps UDF - Creating and controlling Google Maps (inc. GE) in AutoIT | SAPIListBox (Speech Recognition) UDF - Speech Recognition via the Microsoft Speech (SAPI) ListBox | eBay UDF - Automate eBay using the eBay API | ChildProc (Parallel Processing) UDF - Parallel processing functions for AutoIT | HyperCam (Screen Recording) UDF - Automate the HyperCam screen recorder | Twitter UDF - Automate Twitter using OAuth and the Twitter API | cURL UDF - a UDF for transferring data with URL syntax See my other Tools: Rapid Menu Writer - Add menus to DVDs in seconds | TV Player - Automates the process of playing videos on an external TV / Monitor | Rapid Video Converter - A tool for resizing and reformatting videos | [topic130531]Rapid DVD Creator - Convert videos to DVD fast and for free | ZapPF - A tool for killing processes and recycling files | Sean's eBay Bargain Hunter - Find last minute bargains in eBay using AutoIT | Sean's GUI Inspector - A scripting tool for querying GUIs | TransLink Journey Planner with maps - Incorporating Google Maps into an Australian Journey Planner | Automate Qt and QWidgets | Brisbane City Council Event Viewer - See what's going on in Brisbane, Australia
seangriffin Posted August 4, 2017 Author Posted August 4, 2017 (edited) To give everyone an update on this UDF. I came to the realization that this was a little ambitious for a scripting language like AutoIT. I still love the performance of AutoIT for most endevours, but in this case (for realtime graphics) I needed something closer to the speed of a compiled language like C. Partly due to some posts by UEZ I was drawn towards several basic style languages, which seemed to offer the speed I was after with the simplicity of a basic language that is similar to that of AutoIT. These included FreeBasic, BlitzBasic and PureBasic. My attempts to get Box2D working in FreeBasic failed at the DLL level. I'm still not sure why to this day. I then moved to PureBasic and had immediate success there. PureBasic also has native OpenGL support. I initially tried the SFML library there. It was too slow. But after moving to direct OpenGL calls I got the great preformance I was looking for. I then attempted to integrate LiquidFun (http://google.github.io/liquidfun) into PureBasic and to my excitment that was successful also. So for now I'm continuing my developments of LiquidFun / Box2D physics within PureBasic. If anyone is interested in this then check out my recent post in the PureBasic forums here -> http://www.purebasic.fr/english/viewtopic.php?f=27&t=68874 The Github repository is also here -> https://github.com/seanhaydongriffin/PB-LiquidFun-C I'm really happy with where this is going, and perhaps I could later back-port my work and lessons learned from PureBasic to AutoIT. Edited August 4, 2017 by seangriffin Cheers, Sean. See my other UDFs: Chrome UDF - Automate Chrome | SAP UDF - Automate SAP | Java UDF - Automate Java Applications & Applets | Tesseract (OCR) UDF - Capture text from applications, controls and the desktop | Textract (OCR) UDF - Capture text from applications and controls | FileSystemMonitor UDF - File, Folder, Drive and Shell Monitoring | VLC (Media Player) UDF - Creating and controlling a VLC control in AutoIT | Google Maps UDF - Creating and controlling Google Maps (inc. GE) in AutoIT | SAPIListBox (Speech Recognition) UDF - Speech Recognition via the Microsoft Speech (SAPI) ListBox | eBay UDF - Automate eBay using the eBay API | ChildProc (Parallel Processing) UDF - Parallel processing functions for AutoIT | HyperCam (Screen Recording) UDF - Automate the HyperCam screen recorder | Twitter UDF - Automate Twitter using OAuth and the Twitter API | cURL UDF - a UDF for transferring data with URL syntax See my other Tools: Rapid Menu Writer - Add menus to DVDs in seconds | TV Player - Automates the process of playing videos on an external TV / Monitor | Rapid Video Converter - A tool for resizing and reformatting videos | [topic130531]Rapid DVD Creator - Convert videos to DVD fast and for free | ZapPF - A tool for killing processes and recycling files | Sean's eBay Bargain Hunter - Find last minute bargains in eBay using AutoIT | Sean's GUI Inspector - A scripting tool for querying GUIs | TransLink Journey Planner with maps - Incorporating Google Maps into an Australian Journey Planner | Automate Qt and QWidgets | Brisbane City Council Event Viewer - See what's going on in Brisbane, Australia
UEZ Posted August 4, 2017 Posted August 4, 2017 Unfortunately free version of PureBasic is limited and cannot execute larger code via the editor but it look really awesome. I cannot tell you why FreeBasic didn't work for you but also FB has OpenGL native support. There is also a FreeBasic version of Particle Fluid Simulation: expandcollapse popup'' rough smoothed particle hydrodynamics (SPH) draft '' Michael "h4tt3n" Nissen, july 5th 2010 '' '' todo: '' predefined cell neighbors '' better kernel (less bouncy liquid) '' better broad phase collision detection '' better mouse interaction '' better code structure '' includes #Include Once "GL/gl.bi" '' constants Const as single dt = 0.01 '' timestep Const as single half_dt = 0.5*dt '' half timestep const as single r = 10 '' interaction radius const as single r_2 = 2*r '' two radius (for grid) const as single r_sqrd = r*r '' radius squared Const as single k = 400 '' global "spring stiffnes" Const as single k_near = 3000 '' local "spring stiffnes" Const as single viscosity = 2 '' viscosity Const as single rest_dens = 6 '' rest density const as single grav_acc = 400 '' gravity Const as single pi = 4*atn(1) '' pi Const as single radius = 2 '' particle distance on startup const as integer block_width = 60 '' <--- change to reduce / increase num masses const as integer block_height = 100 '' Const as integer num_masses = block_width*block_height '' total number of masses const as integer neighbors = 48 '' max number of neighbors Const as integer screen_wid = 800 '' screen width Const as integer screen_hgt = 400 '' screen height Const As Single wall_friction = 1 '' wall friction (1 = full, 0 = off) Const As Single interaction_Radius = 8 '' mouse interaction radius Const As Integer cell_dia = r Const As Integer cell_max_masses = 24 const as integer num_cells_row = screen_wid/cell_dia const As integer num_cells_col = screen_hgt/cell_dia Const As Integer num_cells = num_cells_row*num_cells_col Const As Single cell_wid = screen_wid/num_Cells_row Const As single cell_hgt = screen_hgt/num_Cells_Col const As single half_cell_wid = cell_wid/2 '' types Type CellType As Integer Num_Masses, Mass(1 To cell_max_masses) End Type type vectortype as single x, y end Type Type NeighborType As Integer a As Single Dist As vectortype norm_dist End Type type masstype As Integer num_neighbors As NeighborType Neighbor(1 To neighbors) as vectortype frc, vel, psn as Single density, angvel, torque end Type '' dim variables Dim as integer center_x = screen_wid\2 Dim as integer center_y = screen_hgt\2 dim as integer mouse_x, mouse_y, mouse_x_old, mouse_y_old, mouse_vel_x, mouse_vel_y dim Shared As masstype mass(1 to num_masses) dim Shared As celltype cell(1 to num_cells_row, 1 To num_cells_col) '' narrow phase collision detection. Make particles neighbors if distance < r Sub CheckIfNeighbor(ByVal a_ As Integer, ByVal b_ As Integer) If mass(a_).num_neighbors = neighbors Then Exit sub If mass(b_).num_neighbors = neighbors Then Exit Sub Dim As vectortype dist dist.x = mass(b_).psn.x-mass(a_).psn.x: If Abs(dist.x) > r Then Exit Sub dist.y = mass(b_).psn.y-mass(a_).psn.y: If Abs(dist.y) > r Then Exit Sub Dim As Single dist_sqrd = dist.x*dist.x+dist.y*dist.y If dist_sqrd > r_sqrd Or dist_sqrd = 0 Then Exit Sub Dim As Single distance = Sqr(dist_sqrd) Dim As vectortype norm_dist = (dist.x/distance, dist.y/distance) Dim As Single density = 1 - distance / r '' create neighbors and save data With mass(a_) .num_neighbors += 1 .Neighbor(.num_neighbors).a = b_ .Neighbor(.num_neighbors).dist = distance .Neighbor(.num_neighbors).norm_dist.x = norm_dist.x .Neighbor(.num_neighbors).norm_dist.y = norm_dist.y .Density += density End With With mass(b_) .num_neighbors += 1 .Neighbor(.num_neighbors).a = a_ .Neighbor(.num_neighbors).dist = distance .Neighbor(.num_neighbors).norm_dist.x = -norm_dist.x .Neighbor(.num_neighbors).norm_dist.y = -norm_dist.y .Density += density End With End Sub Randomize Timer Dim As Integer n = 1 '' place particles For i As Integer = 1 To block_width For j As Integer = 1 To block_height With mass(n) .psn.x = i * radius * 2 .psn.y = screen_hgt - j * radius * 2 .psn.x += (Rnd-Rnd) .psn.y += (Rnd-Rnd) .vel.x += (Rnd-Rnd) .vel.y += (Rnd-Rnd) End With n += 1 Next Next '' create opengl screen ScreenRes screen_wid, screen_hgt, 32,, 2 glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, screen_wid, screen_hgt) glOrtho(0, screen_wid, screen_hgt, 0, 0, 32) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glClearColor (0.6, 0.7, 1.0, 0.0) 'glenable(GL_ALPHA) glEnable(GL_BLEND) glblendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 'glblendfunc(GL_DST_COLOR , GL_SRC_COLOR) getmouse(mouse_x, mouse_y) '' main loop Do '' save old mouse state mouse_x_old = mouse_x mouse_y_old = mouse_y '' get new mouse state getmouse(mouse_x, mouse_y) '' calc mouse velocity mouse_vel_x = (mouse_x-mouse_x_old)/dt mouse_vel_y = (mouse_y-mouse_y_old)/dt '' mouse interaction for i as integer = 1 to num_masses with mass(i) Dim As vectortype dst dst.x = .psn.x - mouse_x: if dst.x > interaction_radius then continue for dst.y = .psn.y - mouse_y: if dst.y > interaction_radius then continue for if dst.x*dst.x+dst.y*dst.y > interaction_Radius*interaction_Radius then continue for if mouse_vel_x then .vel.x += (mouse_vel_x-.vel.x) * 0.16 if mouse_vel_y then .vel.y += (mouse_vel_y-.vel.y) * 0.16 end with Next '' reset particles for i as integer = 1 to num_masses With mass(i) .density = 0 .num_neighbors = 0 End With Next '' reset cells For i As Integer = 1 To num_cells_row For j As Integer = 1 To num_cells_col cell(i, j).Num_Masses = 0 Next Next '' update grid (broad phase collision detection) For i as integer = 1 to num_masses Dim As Integer cell_row = (mass(i).Psn.y\cell_hgt)+1 Dim as integer cell_col = (mass(i).Psn.x\cell_wid)+1 With cell(cell_col, cell_row) If .num_masses = cell_max_masses Then Continue For .num_masses += 1 .Mass(.num_masses) = i End With Next '' narrow phase collision detection For i As Integer = 1 To num_cells_row For j As Integer = 1 To num_cells_col If cell(i, j).num_masses = 0 Then Continue For For k_ As Integer = 1 To cell(i, j).num_masses Dim As Integer a_ = cell(i, j).mass(k_) If mass(a_).num_neighbors = neighbors Then Continue For '' cell(x, y) - self For l_ As Integer = k_+1 To cell(i, j).num_masses CheckIfNeighbor(a_, cell(i, j).mass(l_)) Next '' cell(x, y) - cell(x+1, y) If i < num_cells_row Then For l_ As Integer = 1 To cell(i+1, j).num_masses CheckIfNeighbor(a_, cell(i+1, j).mass(l_)) Next End If If j = num_cells_col Then Continue For '' cell(x, y) - cell(x, y+1) For l_ As Integer = 1 To cell(i, j+1).num_masses CheckIfNeighbor(a_, cell(i, j+1).mass(l_)) Next '' cell(x, y) - cell(x+1, y+1) If i < num_cells_row Then For l_ As Integer = 1 To cell(i+1, j+1).num_masses CheckIfNeighbor(a_, cell(i+1, j+1).mass(l_)) Next End If '' cell(x, y) - cell(x-1, y+1) If i > 1 Then For l_ As Integer = 1 To cell(i-1, j+1).num_masses CheckIfNeighbor(a_, cell(i-1, j+1).mass(l_)) Next End If Next Next Next '' set pressure For i as integer = 1 to num_masses If mass(i).num_neighbors = 0 Then Continue For '' mass i global pressure Dim As Single pressure = -k * (mass(i).density - rest_dens) For j as integer = 1 to mass(i).num_neighbors Dim As Single gradient = 1 - mass(i).neighbor(j).dist / r Dim As Integer l = mass(i).neighbor(j).a '' mass i + l global pressure Dim As Single press = (pressure - k * (mass(l).density - rest_dens) ) '' mass i + l local pressure press -= k_near * gradient '' viscosity Dim As vectortype vel = (mass(i).vel.x-mass(l).vel.x, mass(i).vel.y-mass(l).vel.y) Dim As Single proj_vel = vel.x*mass(i).neighbor(j).norm_dist.x+vel.y*mass(i).neighbor(j).norm_dist.y press -= viscosity * proj_vel * gradient press *= gradient mass(i).frc.x += press * mass(i).neighbor(j).norm_dist.x mass(i).frc.y += press * mass(i).neighbor(j).norm_dist.y mass(l).frc.x -= press * mass(i).neighbor(j).norm_dist.x mass(l).frc.y -= press * mass(i).neighbor(j).norm_dist.y Next Next '' integrate (since particle mass = 1, force and acceleration are identical) for i as integer = 1 to num_masses with mass(i) .frc.y += grav_acc .vel.x += .frc.x * half_dt .vel.y += .frc.y * half_dt .angvel += .torque * half_dt .psn.x += .vel.x * dt + .frc.x * dt * half_dt .psn.y += .vel.y * dt + .frc.y * dt * half_dt .vel.x += .frc.x * half_dt .vel.y += .frc.y * half_dt .angvel += .torque * half_dt .frc.x = 0 .frc.y = 0 .torque = 0 end with Next '' keep particles inside screen For i as integer = 1 to num_masses with mass(i) if .psn.x < radius then .psn.x = radius: .vel.x = -.vel.x*wall_friction: .vel.y *= wall_friction if .psn.x > screen_wid - radius then .psn.x = screen_wid -radius: .vel.x = -.vel.x*wall_friction: .vel.y *= wall_friction If .psn.y < radius then .psn.y = radius: .vel.y = -.vel.y*wall_friction: .vel.x *= wall_friction If .psn.y > screen_hgt - radius Then .psn.y = screen_hgt -radius: .vel.y = -.vel.y*wall_friction: .vel.x *= wall_friction end with Next '' draw to screen glClear(GL_DEPTH_BUFFER_BIT Or GL_COLOR_BUFFER_BIT) glLoadIdentity() glenable(GL_POINT_SMOOTH) glpointsize(r) glcolor4f(0.0f, 0.0f, 0.0f, 0.25f) GLbegin(GL_POINTS) for i as integer = 1 to num_masses glvertex2f(mass(i).psn.x, mass(i).psn.y) Next GLend() gldisable(GL_POINT_SMOOTH) glpointsize(1) glcolor4f(1.0f, 1.0f, 0.0f, 0.75f) glbegin(GL_POINTS) for i as integer = 1 to num_masses glvertex2f(mass(i).psn.x, mass(i).psn.y) Next GLend() GLflush() Flip() sleep 1, 1 Loop Until MultiKey(1) End Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
tarretarretarre Posted August 5, 2017 Posted August 5, 2017 I have wanted to do something like this but never found the time, thank you for sharing this! Socket-IO - An event-driven TCP UDF (Realtime chat example) AutoIt-API-WS - An expressive HTTP server you can use to build your own API with (Screenshots)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now