why is this value shrinking?

Dubbsta

Active Member
Joined
Jul 13, 2017
im copying nature of code physics accumulation and using a wind vector for 2 objects but when they share the vector they both move the same pace
if i use seperate vectors for each object the second objects vector keeps shrinking. the only solution i found was to constantly update the second wind:vector.x = .3
can someone tell my why the second value keeps shrinking and how to share the same value thx
Cerberus X:
Class Vector

 Field x:Float
 Field y:Float
 
 
        
            Method New(x:Float=0,y:Float=0)
                Self.x = x
                Self.y = y
            End
            
            
            Method set:Void(x:Float,y:Float)
                Self.x = x
                Self.y = y
            End
            
            
            Method add:Void(v:Vector)
                x += v.x
                y += v.y
            End
            
            
            Method sub:Void(v:Vecto)
                x -= v.x
                y -= v.y
            End
            
            Method div:Void(num:Float)
                x /= num
                y /= num
            End
            
            Method mult:Void(v:Vector)
                x *= v.x
                y *= v.y
            End
            
            Method mag:Void(num:Int)
                x *= num
                y *= num
            End
            
            Method normalize:Void()
                x /= x
                y /= y
            End
            
            
            Method dot:Void()
            
            End
            
            Method cross:Void()
            
            End
End




Class Ball
 Field pos:Vector
 Field vel:Vector
 Field accel:Vector
 Field size:Int = 10
 Field mass:float
 Field f:Vector
 
 
         Method New(x:Float,y:Float,mass:Float)
             pos = New Vector(x,y)
             vel = New Vector()
             accel = New Vector()
              f = New Vector()
             Self.mass = mass
         End
        
        
        
         Method applyforce:Void(force:Vector)
                f  = force
            
             f.div(mass)
            
             accel.add(f)
         End
        
        
        
         Method update:Void()
        
             vel.add(accel)
                    
            
             pos.add(vel)     
              
            
             accel.mag(0)     
         End
        
        
        
        
        
         Method edges:Void()
             If pos.y > DeviceHeight() - mass * size/2
                 pos.y = pos.y
                 vel.y *= -1
             Elseif
                 pos.y < 0 + mass * size/2
                     pos.y = pos.y
                     vel.y *= -1
             End
            
             If pos.x > DeviceWidth() - mass * size/2
                 pos.x = pos.x
                 vel.x *= -1
             Elseif
                 pos.x < 0 + mass * size/2
                    
                 vel.x *= -1
                 pos.x = pos.x
             End
         End
        
        
         Method draw:Void()
             DrawCircle(pos.x,pos.y,mass * size)
              
         End
End


 

Class gamename Extends App
 
 Field ball:Ball
 Field ball2:Ball
 
 Field wind:Vector
 Field wind2:Vector 'i only want to use one vector'
 Field grav1:Vector
 Field grav2:Vector
 
 
        Method OnCreate:int()
            SetUpdateRate(60)
            
            
            cir = New MakeCircle(150,150,70)
            ball = New Ball(100,50,1)
            ball2 = New Ball( 200,50,5)
            wind = New Vector(.3,0)
            wind2 = New Vector(.3,0)
            grav1 = New Vector(0,0.2)
            grav2 = New Vector(0,0.2)
            time = New Time()
            
            Return 0
        End
        
        
        Method OnUpdate:int()
            grav1.y *= ball.mass
            grav2.y *= ball2.mass
            
            ball.applyforce(grav1)
            ball2.applyforce(grav2)
            
            'wind2.x = .3 'solution that works but doesnt seem right'
            
            If MouseDown(MOUSE_LEFT)
            
                ball.applyforce(wind)
                
                'ball2.applyforce(wind) this will change both values'
                
                ball2.applyforce(wind2) 'this value keeps changing'
            End
            
            ball.update()
            ball2.update()
            ball.edges()
            ball2.edges()
            time.getsecs()
            Return 0
        End
        
        
        Method OnRender:int()
            Cls 0,0,0
            cir.draw()
            ball.draw()
            ball2.draw()
            time.draw()
            'DrawText(ball.f.y,10,10)
            DrawText(ball.f.x,10,20)
            DrawText(ball.vel.y,10,30)
            DrawText(ball2.f.x,10,40)
            Return 0
        End
End





Function Main:int()

    New gamename()
    
    Return 0
    
End
 

Dubbsta

Active Member
Joined
Jul 13, 2017
forgot to import mojo and deleted a couple of lines, this should work
thx
seems to keep dividing...

Cerberus X:
Import mojo


Class Vector

Field x:Float
Field y:Float


       
            Method New(x:Float=0,y:Float=0)
                Self.x = x
                Self.y = y
            End
           
           
            Method set:Void(x:Float,y:Float)
                Self.x = x
                Self.y = y
            End
           
           
            Method add:Void(v:Vector)
                x += v.x
                y += v.y
            End
           
           
            Method sub:Void(v:Vecto)
                x -= v.x
                y -= v.y
            End
           
            Method div:Void(num:Float)
                x /= num
                y /= num
            End
           
            Method mult:Void(v:Vector)
                x *= v.x
                y *= v.y
            End
           
            Method mag:Void(num:Int)
                x *= num
                y *= num
            End
           
            Method normalize:Void()
                x /= x
                y /= y
            End
           
           
            Method dot:Void()
           
            End
           
            Method cross:Void()
           
            End
End




Class Ball
Field pos:Vector
Field vel:Vector
Field accel:Vector
Field size:Int = 10
Field mass:float
Field f:Vector


         Method New(x:Float,y:Float,mass:Float)
             pos = New Vector(x,y)
             vel = New Vector()
             accel = New Vector()
              f = New Vector()
             Self.mass = mass
         End
       
       
       
         Method applyforce:Void(force:Vector)
                f  = force
           
             f.div(mass)
           
             accel.add(f)
         End
       
       
       
         Method update:Void()
       
             vel.add(accel)
                   
           
             pos.add(vel)    
             
           
             accel.mag(0)    
         End
       
       
       
       
       
         Method edges:Void()
             If pos.y > DeviceHeight() - mass * size/2
                 pos.y = pos.y
                 vel.y *= -1
             Elseif
                 pos.y < 0 + mass * size/2
                     pos.y = pos.y
                     vel.y *= -1
             End
           
             If pos.x > DeviceWidth() - mass * size/2
                 pos.x = pos.x
                 vel.x *= -1
             Elseif
                 pos.x < 0 + mass * size/2
                   
                 vel.x *= -1
                 pos.x = pos.x
             End
         End
       
       
         Method draw:Void()
             DrawCircle(pos.x,pos.y,mass * size)
             
         End
End




Class gamename Extends App

Field ball:Ball
Field ball2:Ball

Field wind:Vector
Field wind2:Vector 'i only want to use one vector'
Field grav1:Vector
Field grav2:Vector


        Method OnCreate:int()
            SetUpdateRate(60)
           
           
           
            ball = New Ball(100,50,1)
            ball2 = New Ball( 200,50,5)
            wind = New Vector(.3,0)
            wind2 = New Vector(.3,0)
            grav1 = New Vector(0,0.2)
            grav2 = New Vector(0,0.2)
           
           
            Return 0
        End
       
       
        Method OnUpdate:int()
            grav1.y *= ball.mass
            grav2.y *= ball2.mass
           
            ball.applyforce(grav1)
            ball2.applyforce(grav2)
           
            'wind2.x = .3 'solution that works but doesnt seem right'
           
            If MouseDown(MOUSE_LEFT)
           
                ball.applyforce(wind)
               
                'ball2.applyforce(wind) this will change both values'
               
                ball2.applyforce(wind2) 'this value keeps changing'
            End
           
            ball.update()
            ball2.update()
            ball.edges()
            ball2.edges()
         
            Return 0
        End
       
       
        Method OnRender:int()
            Cls 0,0,0
           
            ball.draw()
            ball2.draw()
           
            'DrawText(ball.f.y,10,10)
            DrawText(ball.f.x,10,20)
            DrawText(ball.vel.y,10,30)
            DrawText(ball2.f.x,10,40)
            Return 0
        End
End





Function Main:int()

    New gamename()
   
    Return 0
   
End
 
Last edited:

Phil7

Moderator
Joined
Jun 26, 2017
Did you notice, that you are working with references i.e. pointers in applyforce() and not values?
So f = force sets f to the handle of force and with f.div you change the values in force alias wind indirectly.

I did not read through all your code, so maybe I don't adress your problem correctly.
 

Dubbsta

Active Member
Joined
Jul 13, 2017
thx Phil7 that was the problem. i was sure by doing that i copied over the values...
that was really driving me crazy o_O
 
Top Bottom