Advertisement
Guest User

Particle Physics made by Syharaa

a guest
Dec 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Players, RunService, Workspace} from "@rbxts/services";
  2.  
  3. //Classes
  4. class Particle{
  5.     object: Frame;
  6.     x_pos: number
  7.     y_pos : number;
  8.     prev_ypos: number;
  9.     prev_xpos: number;
  10.     bounce = 0.9;
  11.     gravity = 0.4;
  12.     constructor(Parent: undefined | Instance){
  13.         this.object = new Instance("Frame");
  14.         this.object.Parent = Parent;
  15.         this.object.Size = new UDim2(new UDim(0, 10), new UDim(0, 10));
  16.         this.object.BackgroundColor3 = new Color3(0, 0, 0);
  17.         this.x_pos = 10;
  18.         this.y_pos = 10;
  19.         this.prev_xpos = 0;
  20.         this.prev_ypos = 0;
  21.     }
  22.  
  23.     updatePoint(){
  24.         let vx = (this.x_pos - this.prev_xpos);
  25.         let vy = (this.y_pos - this.prev_ypos);
  26.         this.prev_xpos = this.x_pos;
  27.         this.prev_ypos = this.y_pos;
  28.         this.x_pos += vx;
  29.         this.y_pos += vy;
  30.         this.y_pos += this.gravity;
  31.        
  32.         if(this.x_pos > VIEWPORT_SIZE.X){
  33.             this.x_pos = VIEWPORT_SIZE.X;
  34.             this.prev_xpos = this.x_pos + (vx*this.bounce);
  35.         }else if(this.x_pos < 1/60){
  36.             this.prev_xpos = this.x_pos + (vx*this.bounce);
  37.         }
  38.  
  39.         if (this.y_pos > VIEWPORT_SIZE.Y){
  40.             this.y_pos = VIEWPORT_SIZE.Y ;
  41.             this.prev_ypos = this.y_pos + (vy*this.bounce);
  42.         }else if(this.y_pos < 1/60) {
  43.             this.prev_ypos = this.y_pos + (vy*this.bounce);
  44.         }
  45.     }
  46.  
  47.     registerPont(){
  48.         this.object.Position = new UDim2(new UDim(0, this.x_pos), new UDim(0, this.y_pos));
  49.     }
  50. }
  51. //Objects
  52. let player = Players.LocalPlayer;
  53. let camera = Workspace.CurrentCamera;
  54. let main_ui: ScreenGui | undefined = player.WaitForChild("PlayerGui").WaitForChild<ScreenGui>("Main");
  55. let frame: Instance | undefined = main_ui!.FindFirstChild("Frame");
  56. let particle = new Particle(frame);
  57.  
  58. //Constants
  59. const VIEWPORT_SIZE = camera!.ViewportSize;
  60.  
  61. //Functions
  62. function update(){
  63.     particle.updatePoint();
  64.     particle.registerPont();
  65. }
  66.  
  67. function sortOrderIn(Object: Instance[]){
  68.     Object.forEach((element, index, array) => {
  69.         element.Name = `Particle_${index+1}`;
  70.     });
  71.    
  72. }
  73. //Events
  74. RunService.RenderStepped.Connect(update);
  75. sortOrderIn(frame!.GetChildren());
  76.  
  77.  
  78. export {};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement