TheGameCreators / AGK-Studio

3 stars 1 forks source link

Gravity not working properly in AGK Studio #1111

Open JamesClent opened 11 months ago

JamesClent commented 11 months ago

When compiling my code in Studio the gravity does not work, while the same code in classic works fine. However, since I need to publish on the iPhone I cannot use classic until fixed, so this is a HIGH priority problem for me.

Will try to make a small program as well.

JamesClent commented 11 months ago

OK Here is the code that shows that CloneSprite messes up something in physics when compiled with AGK Studio, but NOT when compiled with AGK Classic.

The code uses both createsprite and clone sprite so the difference is very evident. //=========================================================== // simple example showing plenty of sprites using box2d

SetWindowSize           ( 640, 1136, 0 )
SetVirtualResolution    ( 640, 1136 )
// display a background
backdrop = CreateSprite ( LoadImage ( "background1.jpg" ) )
SetSpriteColorAlpha     ( backdrop, 40 )
SetSpriteSize           ( backdrop, 640, 1136 )

// load crate image
LoadImage               ( 2, "silver.png" )
SetPhysicsGravity       ( 0, 650)

// set up time and index variables
time                    = 0

// Create a sprite to be cloned later.
sID                     = 13
createsprite            ( sID, 2 )
SetSpritePosition       ( sID, -50, -50 )
SetSpritePhysicsOn      ( sID, 2 )
SetSpriteGroup          ( sID, 0-sID )

// set properties
size                    = 20 + Random ( 0, 30 ) 
SetSpriteSize           ( sID, size, size )
SetSpriteShape          ( sID, 2 )

// sprite number to use from now on forward
sID                     = sID + 1

// main loop
do
    // increment time
    time = time + 1
    // release a new sprite every so often
    if ( time >= 75 )
        // when sID = even, create the sprite and release it from 0,0
        // if odd, then CLONE the sprite and release it from position 600,0
        if mod(sID,2) = 0
            CreateSprite                    ( sID, 2 )
            SetSpritePosition               ( sID, 0, 0 )
            SetSpritePhysicsOn              ( sID, 2 )
            SetSpriteGroup                  ( sID, 0-sID )

            // set properties
            size                            = 20 + Random ( 0, 30 )
            SetSpriteSize                   ( sID, size, size )
            SetSpriteShape                  ( sID, 2 )
            SetSpritePhysicsAngularImpulse  ( sID, 10000 + Random ( 0, 350 ) )
            SetSpritePhysicsVelocity        ( sID, 100 + Random ( 0, 100 ), 100 + Random ( 0, 100 ) )
            SetSpriteColor                  ( sID, Random   ( 120, 255 ), Random ( 120, 255 ), Random ( 120, 255 ), 255 )
            SetSpritePhysicsRestitution     ( sID, 0.4 )
        else

            CloneSprite                     ( sID, 13 )     // SID_MARBLES )
            SetSpritePosition               ( sID, 600, 0 )
            SetSpritePhysicsOn              ( sID, 2 )  // PHY_MODE_DYNAMIC )
            SetSpriteGroup                  ( sID, 0-sID )

            SetSpriteSize                   ( sID, -1, -1 )
            SetSpriteShape                  ( sID, 2 )
            SetSpritePhysicsAngularImpulse  ( sID, 10000 + Random ( 0, 350 ) )
            vX                              = ( 100 + Random2( 0, 100 ) ) *  -1
            vY                              = 100 + Random2( 0, 100 )
            SetSpritePhysicsVelocity        ( sID, vX, vY )
            SetSpriteColor ( sID, Random    ( 120, 255 ), Random ( 120, 255 ), Random ( 120, 255 ), 255 )
            SetSpritePhysicsRestitution     ( sID, 0.4 )
        endif

        // increment index and reset time
        sID = sID + 1
        time = 0
    endif

    // update the screen
    Sync ( )
loop

//===============================================================

JamesClent commented 11 months ago

In this video you clearly see the difference between Created and Cloned Sprites. Gravity does not work for Cloned Sprites https://github.com/TheGameCreators/AGK-Studio/assets/53790150/6caabb59-f82d-45fd-a58a-c3e69b0b5dd4

In this video you see that for Classic everything is the same, CretaSprite and CloneSprite https://github.com/TheGameCreators/AGK-Studio/assets/53790150/3e6a38cb-9145-4117-8f6f-c2ad51bdca11

VirtualNomad19 commented 11 months ago

definitely unreliable.

SetVirtualResolution( 640,360 )

This = CreateSprite(0)
    SetSpriteSize(This,64,64)
    SetSpritePositionByOffset(This,100,100)
    SetSpritePhysicsOn(This,2)
    SetSpritePhysicsGravityScale(This,1 ) //Does not help clone

That = CloneSprite(This)
    SetSpritePositionByOffset(That,300,100)
    SetSpritePhysicsOn(That,2)
    //SetSpritePhysicsGravityScale(That,1 ) //DOES set, but MAY still fail on EXECUTE
    //Sync()
do
    If GetPointerPressed() 
        SetSpritePhysicsGravityScale(This,1 )
        SetSpritePhysicsGravityScale(That,1 )
        SetSpritePositionByOffset(This,100,100)
        SetSpritePositionByOffset(That,300,100)
    EndIf
    Print(GetSpritePhysicsGravityScale(That))
    Sync()
loop

manually setting gravity scale MAY help but note seemingly random clone gravity scale which may be 0 or some wild # such as:

studio g

do note that Clicking does seem to consistently (re)Set scale?

WIN10 V2023.07.17

VirtualNomad19 commented 11 months ago

note: prefacing with #Renderer "Basic" does seem to consistently set gravity scale properly. since vulkan does handle images differently compared to opengl (the only Classic option), and some box2d physics properties are automatically set based on Size, (and Shape), this is where the disconnect appears to arise.

worth noting: SetSpritePhysicsGravityScale() was added relatively recently and may point to the fix locaion.

VirtualNomad19 commented 11 months ago

more testing (vulkan):

SetVirtualResolution( 640,360 )

GLOBAL This, LastGravScale#, LastAwake
    This = CreateSprite(0)
        SetSpriteSize(This,32,32)
        SetSpritePositionByOffset(This,100,100)
        SetSpritePhysicsOn(This,2)

GLOBAL Clones as Integer []

do
    If Last# + 0.5 <= Timer() and Clones.Length < 49
        AddClone(This)
        Last# = Timer()
    Endif
    Print(GetRendererName())
    Print(LastGravScale#)
    Print(LastAwake)
    Sync()
loop

Function AddClone(src)
    Clone = CloneSprite(src)
        SetSpritePositionByOffset(Clone,Random(100,500),100)
        SetSpritePhysicsOn(Clone,2)
        LastGravScale# = GetSpritePhysicsGravityScale(Clone)
        LastAwake = GetSpritePhysicsIsAwake(Clone)

        //Un-REM BOTH lines below to consistently(?) Clone properly, else Mix // & observe
        SetSpritePhysicsGravityScale(Clone,GetSpritePhysicsGravityScale(src) )
        SetSpritePhysicsAllowSleep(Clone,0)     

        //SetSpritePhysicsInitiallyAwake(Clone,1) //Does't matter in combo with the 2 lines above.

    Clones.Insert(Clone)
EndFunction

further tests show that including:

        SetSpritePhysicsGravityScale(Clone,GetSpritePhysicsGravityScale(src) ) 
        SetSpritePhysicsAllowSleep(Clone,0) 

...consistently Clones the sprite properly (i obseved hundreds). if you omit one or the other, there will be anomolies.

note the LastGravScale# pre-settings before "re-setting" which range across the spectrum.

including SetSpritePhysicsInitiallyAwake() is no help but worth noting that GetSpritePhysicsIsAwake(Clone) on creation is also random (0,1).

finally, when Clone gravity is "wrong", should another sprite contact it, the Clone seems to behave normally from then on (it was asleep). minimal testing there.

@JamesClent hopefully a small Clone function including those 2 lines keeps your project going?

JamesClent commented 11 months ago

Thanks for the further testing. So there are new commands that are supposed to be used when cloning, but those are applying only to AGK Studio, since Classic doesn't support vulcan?

On Sun, Jul 23, 2023 at 8:45 PM Virtual Nomad @.***> wrote:

more testing:

SetVirtualResolution( 640,360 )

GLOBAL This, LastGravScale#, LastAwake This = CreateSprite(0) SetSpriteSize(This,32,32) SetSpritePositionByOffset(This,100,100) SetSpritePhysicsOn(This,2)

GLOBAL Clones as Integer []

do If Last# + 0.5 <= Timer() and Clones.Length < 49 AddClone(This) Last# = Timer() Endif Print(GetRendererName()) Print(LastGravScale#) Print(LastAwake) Sync() loop

Function AddClone(src) Clone = CloneSprite(src) SetSpritePositionByOffset(Clone,Random(100,500),100) SetSpritePhysicsOn(Clone,2) LastGravScale# = GetSpritePhysicsGravityScale(Clone) LastAwake = GetSpritePhysicsIsAwake(Clone)

  //Un-REM BOTH lines below to consistently(?) Clone properly, else Mix // & observe
  SetSpritePhysicsGravityScale(Clone,GetSpritePhysicsGravityScale(src) )  // MIX
  SetSpritePhysicsAllowSleep(Clone,0)                                 // COMBOS

  SetSpritePhysicsInitiallyAwake(Clone,1) //Does't matter in combo with the 2 lines above.

Clones.Insert(Clone) EndFunction

further tests show that including:

  SetSpritePhysicsGravityScale(Clone,GetSpritePhysicsGravityScale(src) )
  SetSpritePhysicsAllowSleep(Clone,0) 

...consistently Clones the sprite properly. if you omit one or the other, there will be anomolies.

note the earily LastGravScale# settings before "re-setting" which range across the spectrum.

including SetSpritePhysicsInitiallyAwake() is no help but worth noting that GetSpritePhysicsIsAwake(Clone) on creation is also random (0,1).

finally, when Clone gravity is "wrong", should another sprite contact it, the Clone seems to behave normally from then on.

— Reply to this email directly, view it on GitHub https://github.com/TheGameCreators/AGK-Studio/issues/1111#issuecomment-1647078469, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM2MLRXL37MUJMB7GI4IGVTXRXHSRANCNFSM6AAAAAA2TAQT5E . You are receiving this because you authored the thread.Message ID: @.***>

VirtualNomad19 commented 11 months ago

i doubt they are meant to be required; CloneSprite should work the same on both Studio and Classic but there's an apparent issue when it comes to Vulkan. ie, a Bug. and, my offering is a work-around to manually address until a proper fix is applied while the testing offered may help the developers in that.

meanwhile, the additional 2D physics commands (there were a number added) was response to our desire for an update to Box2D where AGKs implementation is considered a functional yet "light" version and now well-behind the current version of Box2D.

i appreciate some of the commands added in the 2022 update where, for example, we couldn't set individual sprite gravity scales before (and it's nice to be able to do so now), and for TGCs response to the Request.

alas, it may have introduced this bug but as i prefer Classic, i'd not encountered it before (nor heard of anyone who had). it's still unkown if this issue was here "all along", introduced with the 2022 update, or otherwise. but, here we are :)

JamesClent commented 11 months ago

Yes, I prefer Classic as well, but had to compile in Studio, since that was the only version with the iOS fix. Thanks Virtual Nomad.

On Mon, Jul 24, 2023 at 8:44 AM Virtual Nomad @.***> wrote:

i doubt they are meant to be required; CloneSprite should work the same on both Studio and Classic but there's an apparent issue when it comes to Vulkan. ie, a Bug. and, my offering is a work-around to manually address until a proper fix is applied while the testing offered may help the developers in that.

meanwhile, the additional 2D physics commands (there were a number added) was response to our desire for an update to Box2D https://github.com/TheGameCreators/AGK-Studio/issues/965 where AGKs implementation is considered a functional yet "light" version and now well-behind the current version of Box2D https://box2d.org/.

i appreciate some of the commands added in the 2022 update where, for example, we couldn't set individual sprite gravity scales before (and it's nice to be able to do so now), and for TGCs response to the Request.

alas, it may have introduced this bug but as i prefer Classic, i'd not encountered it before (nor heard of anyone who had). it's still unkown if this issue was here "all along", introduced with the 2022 update, or otherwise. but, here we are :)

— Reply to this email directly, view it on GitHub https://github.com/TheGameCreators/AGK-Studio/issues/1111#issuecomment-1647944052, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM2MLRTSMD3T23S5GIDHHMTXRZ32NANCNFSM6AAAAAA2TAQT5E . You are receiving this because you were mentioned.Message ID: @.***>