Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dot Animation Issue
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
iris87




PostPosted: Thu Jun 01, 2023 2:04 pm   Post subject: Dot Animation Issue

What is it you are trying to achieve?
A simple moving dots animation

What is the problem you are having?
Sometimes the dots sort of "stick" to a wall, because I'm just reversing the direction of the dots on collision it will always stick to walls.


Describe what you have tried to solve this problem
I attempted to add a randomness to the reversed angle, this works but not great, it still takes quite a few collisions while stuck on a wall to bounce away.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

(ignore the repel from mouse related things, not implemented yet)

Turing:


setscreen ("graphics:max;max;nobuttonbar;offscreenonly;title:Connecting Dots")
colorback (black)
const AmountOfDots := 100
const RepelFromMouse := true

var DebugChar : string (1)

var ColourArray : array 1 .. 100 of int

for Colour : 1 .. 100
    ColourArray (Colour) := RGB.AddColour (Colour / 100, Colour / 100, Colour / 100)
end for

type Dot :
    record
        X : int
        Y : int
        Speed : real
        Direction : int
    end record

var DotArray : array 1 .. AmountOfDots of Dot

for DotIndex : 1 .. AmountOfDots
    DotArray (DotIndex).X := Rand.Int (1, maxx - 1)
    DotArray (DotIndex).Y := Rand.Int (1, maxy - 1)

    DotArray (DotIndex).Speed := Rand.Int (20, 40) / 10

    DotArray (DotIndex).Direction := Rand.Int (1, 360)
end for

function DegreeToRadian (degree : int) : real
    result degree * (Math.PI / 180)
end DegreeToRadian

% Render loop

var lastFrameTime : int := Time.Elapsed
var MouseX : int
var MouseY : int
var MouseDown : int
var DistanceBetweenPoints : real
var LineColour : int
color(brightgreen)
loop
    cls
    Mouse.Where (MouseX, MouseY, MouseDown)
    put "FPS: ", round (1000 / (Time.Elapsed - lastFrameTime))
    put "X:Y ", MouseX, ":", MouseY
    lastFrameTime := Time.Elapsed
    Time.DelaySinceLast (17)

    for DotIndex : 1 .. AmountOfDots

        if DotArray (DotIndex).X >= maxx then
            DotArray (DotIndex).X := maxx - 1
            DotArray (DotIndex).Speed := Rand.Int (20, 40) / 10
            DotArray (DotIndex).Direction := 180 - DotArray (DotIndex).Direction + Rand.Int(-20, 20)
        end if

        if DotArray (DotIndex).Y >= maxy then
            DotArray (DotIndex).Y := maxy - 1
            DotArray (DotIndex).Speed := Rand.Int (20, 40) / 10
            DotArray (DotIndex).Direction := -1 * DotArray (DotIndex).Direction + Rand.Int(-20, 20)
        end if

        if DotArray (DotIndex).X < 0 then
            DotArray (DotIndex).X := 1
            DotArray (DotIndex).Speed := Rand.Int (20, 40) / 10
            DotArray (DotIndex).Direction := 180 - DotArray (DotIndex).Direction + Rand.Int(-20, 20)
        end if

        if DotArray (DotIndex).Y < 0 then
            DotArray (DotIndex).Y := 1
            DotArray (DotIndex).Speed := Rand.Int (20, 40) / 10
            DotArray (DotIndex).Direction := -1 * DotArray (DotIndex).Direction + Rand.Int(-20, 20)
        end if

        DotArray (DotIndex).X += round (Math.cos (DegreeToRadian (DotArray (DotIndex).Direction)) * DotArray (DotIndex).Speed)
        DotArray (DotIndex).Y += round (Math.sin (DegreeToRadian (DotArray (DotIndex).Direction)) * DotArray (DotIndex).Speed)
    end for
    for DotIndex : 1 .. AmountOfDots
        for OtherDot : 1 .. AmountOfDots
            if OtherDot = DotIndex then
                exit
            end if
            DistanceBetweenPoints := sqrt ((DotArray (OtherDot).X - DotArray (DotIndex).X) ** 2 + (DotArray (OtherDot).Y -
                DotArray (DotIndex).Y) ** 2)
            if DistanceBetweenPoints < 150 then
                LineColour := ColourArray (100 - floor (DistanceBetweenPoints / 1.5))
                drawline (DotArray (DotIndex).X, DotArray (DotIndex).Y, DotArray (OtherDot).X, DotArray (OtherDot).Y, LineColour)
            end if
        end for
    end for
    for DotIndex : 1 .. AmountOfDots
        drawfilloval (DotArray (DotIndex).X, DotArray (DotIndex).Y, 4, 4, white)
    end for
    View.Update
end loop




Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
scholarlytutor




PostPosted: Sat Jun 03, 2023 8:13 am   Post subject: RE:Dot Animation Issue

Hmm did you try just reversing the direction when a dot bumps the wall? One simple way to do that would be to add 180 degrees. That would just make it go in the exact opposite direction.

Of course, if you wanted it to be more realistic based on the laws of physics, you'd have to consider what angle the dot is bumping the wall at. For example, if it bumps the wall at a 30 degree angle, then it should move away from the wall at 150 degrees (180-30).

A couple of unrelated tips:
You can make the mouse into a type as well. That way it doesn't need 3 separate variables.

There are degree versions of the trig functions. For example, sind and cosd. That saves you the trouble of converting from degrees to radians.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: