Posted: 13th Jan 2022 3:37
Hello everyone ! I'm re-learning how to use AKG around these days, and as I've been away for many years, I'm kind of lost xD.
let's say I'm a "newbie" again, my question is the following, I'm trying to create simple games to get the "rhythm", I'm trying to create a mechanic that in agk "seems" not to be as complex to implement as in others engines.

I would like to make a mechanic similar to this game below: in which the sprite follows the angle of the mouse pointer and is stopped in a position ddo ViewPort but moves around the World, I'm a little confused on how to apply this correctly in AppGameKit .



+ Code Snippet
// Project: MyLearnGame
// Created: 2022-01-11
 
// show all errors
SetErrorMode(2)
 
// set window properties
SetWindowTitle( "MyLearnGame" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
 
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
 
 
global sprite
sprite = CreateSprite(LoadImage("sprite_00.png"))
//SetSpriteSize(sprite, 1, 100)
SetSpriteOffset(sprite, GetSpriteWidth(sprite)/2, GetSpriteheight(sprite)/2)
SetSpritePositionByOffset(sprite, 100, 100)
 
FixSpriteToScreen(sprite,1)
 
do
 
    Printc("Sprite Angle is ")
    Print(Str(GetSpriteAngle(sprite),2)+" Degrees")
 
    x# = GetPointerX()
    y# = GetPointerY()
 
    angle# = ATanFull(x#-GetSpriteXByOffset(sprite), y#-GetSpriteYByOffset(sprite))
    SetSpriteAngle(sprite, angle#)
 
    MovePlayer()
 
    Sync()
loop
 
function MovePlayer()
	x# = GetSpriteXByOffset(sprite)
	y# = GetSpriteYByOffset(sprite)
	angle# = GetSpriteAngle(sprite)
	speed# = 5.0
	
    if (GetRawKeyState ( 65 )) //LEFT
         inc x#, sin(angle#-90) * speed#       
         dec y#, cos(angle#-90) * speed#       
    endif      
 
    if (GetRawKeyState ( 87 )) //DOWN
         inc x#, sin(angle#) * speed#       
         dec y#, cos(angle#) * speed#       
    endif  
     
    if (GetRawKeyState ( 68 )) //RIGHT
         inc x#, sin(angle#+90) * speed#       
         dec y#, cos(angle#+90) * speed#       
    endif  
     
    if (GetRawKeyState ( 83 )) //UP
         inc x#, sin(angle#+180) * speed#       
         dec y#, cos(angle#+180) * speed#       
    endif  
	
// in this part if the player is inside the area in the center of the view the sprite image stops moving to the corners of the view, but should it continue to move in the world ? or not ?
      if (x# >= 400) and (x# <= GetVirtualWidth() - 400) 
     
     if (y# >= 200) and (y# <= GetVirtualHeight() - 200)
     
		SetSpritePositionByOffset(sprite, x#, y#)
    
     endif
    
    endif	
    // this line camera follow sprite, right ?
    SetViewOffset( x#, y# )

endfunction




would you have a tip or an example for me to get inspired and try to replicate and study?
thank you very much for everyone's attention!!
Posted: 13th Jan 2022 5:27
Constantly moving the sprite and pointing the sprite at the x and y position of the mouse.

GetPointerX ( ), GetPointerY

No example to show but that is basic easy set up.
Posted: 13th Jan 2022 9:10
I have not got time to code an example but heres a couple of functions that will kick start your idea

+ Code Snippet
Function PointSprite(spriteID,x#,y#)

   rem get display data
   dw# = getVirtualWidth()
   dh# = getVirtualHeight()
   aspect# = (dw# / dh#)
   if aspect# = 1.0
       aspect# = getDisplayAspect()
   else
       aspect# = 1.0
   endif

   dx# = x#-getSpriteXbyOffset(spriteID)
   dy# = y#-getSpriteYbyOffset(spriteID)
   setSpriteAngle(spriteID,atanfull(dx#,dy#/aspect#))

EndFunction

Function MoveSprite(spriteID, amount#)
    rem get display data
    dw# = getVirtualWidth()
    dh# = getVirtualHeight()
    aspect# = (dw# / dh#)
    if aspect# = 1.0
        aspect# = getDisplayAspect()
    else
        aspect# = 1.0
    endif
    a# = getSpriteAngle(spriteID)
    x# = getSpriteXbyOffset(spriteID)
    y# = getSpriteYbyOffset(spriteID)
    setSpritePositionByOffset(spriteID,x#+cos(a#)*amount#,y#+sin(a#)*amount#*aspect#)
EndFunction



PointSprite to the mouse location, MoveSprite by given amount, your 2/3's there
Posted: 13th Jan 2022 14:02
well, the examples work very well, however, I still have doubts about how to make the movement of the ship in relation to the world, I would need to leave it limited to a certain area of the ViewPort, but if it is fixed in the delimited area, the movement in the world still persists.
what do you suggest i do? something tells me I need to use the command : FixSpriteToScreen.

like this below :
Posted: 13th Jan 2022 14:52
You can do what you want with SetViewOffset()
You will then need to change the sprites positions relative to the world not the screen.
See:
WorldToScreenX()
WorldToScreenY()
ScreenToWorldX()
ScreenToWorldY()
Posted: 13th Jan 2022 16:04
Heres a small piece of code ripped from my asteroids test project space game thing!, you will need to edit it to suit your project, set the images and player sprite and stuff but this will handle the screen offsets and image background.

I make the background the 3* larger than the screen because I have zooming, but the principle is you move the background with the player sprite and view offset and move the background UV is the opposite direction to travel giving the illusion that its stationary, the benefit of this is a "infinite" space playing area

+ Code Snippet
Type tStarSystem
	name as string 
	back_id
EndType
Global gStarSystem as tStarSystem	

Function StarSystem_Load()

	gStarSystem.back_id=CreateSprite(gImage.background.space)
	SetSpriteUVScale(gStarSystem.back_id, 0.8, 0.7)
	FixSpriteToScreen(gStarSystem.back_id, 1)
	SetSpriteSize(gStarSystem.back_id, SCREEN_WIDTH*3, SCREEN_HEIGHT*3)
	SetSpriteOffset(gStarSystem.back_id, (SCREEN_WIDTH*3)/2, (SCREEN_HEIGHT*3)/2)
	SetSpritePositionByOffset(gStarSystem.back_id, RES_WIDTH/2, RES_HEIGHT/2)

EndFunction

Function StarSystem_Update()
	view_x# = GetViewOffsetX()
	view_y# = GetViewOffsetY()
	new_x#=GetSpriteXByOffset(gPlayer.sprite_id)-RES_WIDTH/2
	new_y#=GetSpriteYByOffset(gPlayer.sprite_id)-RES_HEIGHT/2
	lerp_x# = Lerp( view_x#, new_x# , 0.1 )
	lerp_y# = Lerp( view_y#, new_y# , 0.1 )
	SetViewOffset(lerp_x#, lerp_y#)
	SetSpriteUVOffset(gStarSystem.back_id, GetViewOffsetX()/2500, GetViewOffsetY()/2500 )
EndFunction

Function Lerp( startValue as float, endValue as float , value as float )
    result# = ((1.0 - value) * startValue) + (value * endValue)
EndFunction result#


with minimal edits you should be able to get that running
Posted: 13th Jan 2022 18:50
I managed to move around the map keeping the sprite fixed in a certain part, however, the algorithm is not " as efficient and fluid ", at some point the sprite gets stuck and doesn't leave the specific point anymore (although it still moves on the map )... (if anyone can help me improve this algorithm xD, I would be happy)

and also how do I get the position of this sprite in relation to the map? if I use the command: GetSpriteXByOffset and GetSpriteYByOffset, I get the sprite's position relative to the ViewPort...

I tried to use the following commands, but I believe I don't know how to use them, the values don't seem to be correct...
"


+ Code Snippet
// Project: MyLearnGame
// Created: 2022-01-11
 
// show all errors
SetErrorMode(2)
 
// set window properties
SetWindowTitle( "MyLearnGame" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
 
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
 
 
global sprite
sprite = CreateSprite(LoadImage("sprite_00.png"))
//SetSpriteSize(sprite, 1, 100)
SetSpriteOffset(sprite, GetSpriteWidth(sprite)/2, GetSpriteheight(sprite)/2)
SetSpritePositionByOffset(sprite, 100, 100)
 
FixSpriteToScreen(sprite,1)
 
do
 
    Printc("Sprite Angle is ")
    Print(Str(GetSpriteAngle(sprite),2)+" Degrees")
 
	MouseX# = GetPointerX()
	MouseY# = GetPointerY()
	PointSprite(sprite,MouseX#,MouseY#)

	MovePlayer()
 
    Sync()
loop
 
function MovePlayer()
    x# = GetSpriteXByOffset(sprite)
    y# = GetSpriteYByOffset(sprite)
    
    Vx# = GetViewOffsetX()
    Vy# = GetViewOffsetY()
    
    angle# = GetSpriteAngle(sprite)
    speed# = 2.0
     
    if (GetRawKeyState ( 65 )) //LEFT
		
		 if (x# >= 400) and (x# <= 800)
			 inc x#, sin(angle#-90) * speed#           
         endif	
         
         if (y# >= 200) and (y# <= 600)
			  dec y#, cos(angle#-90) * speed#   
         endif
         		
         inc Vx#, sin(angle#-90) * speed#  
		dec Vy#, cos(angle#-90) * speed#  		
         			 
    endif     
  
    if (GetRawKeyState ( 87 )) //DOWN
		
		 if (x# >= 400) and (x# <= 800)
			 inc x#, sin(angle#) * speed#           
         endif	
         
         if (y# >= 200) and (y# <= 600)
			 dec y#, cos(angle#) * speed#   
         endif
		   
		  inc Vx#, sin(angle#) * speed#  
		dec Vy#, cos(angle#) * speed#    
		   
    endif 
      
    if (GetRawKeyState ( 68 )) //RIGHT
		
		 if (x# >= 400) and (x# <= 800)
			inc x#, sin(angle#+90) * speed#            
         endif	
         
         if (y# >= 200) and (y# <= 600)
			dec y#, cos(angle#+90) * speed#     
         endif
    
		inc Vx#, sin(angle#+90) * speed#  
		dec Vy#, cos(angle#+90) * speed#  
    
    endif 
      
    if (GetRawKeyState ( 83 )) //UP
		
		 if (x# >= 400) and (x# <= 800)
			inc x#, sin(angle#+180) * speed#         
         endif	
         
         if (y# >= 200) and (y# <= 600)
			 dec y#, cos(angle#+180) * speed#    
         endif		
    
		inc Vx#, sin(angle#+180) * speed#  
		dec Vy#, cos(angle#+180) * speed#    
    endif 
     
	SetSpritePositionByOffset(sprite, x#, y#)
    
	SetViewOffset(Vx#,Vy#)

endfunction


Posted: 13th Jan 2022 19:00
Look up two posts
Posted: 13th Jan 2022 19:50
Solved Guys thanks Scraggle and PartTimeCoder by help !

+ Code Snippet
// Project: MyLearnGame
// Created: 2022-01-11
 
// show all errors
SetErrorMode(2)
 
// set window properties
SetWindowTitle( "MyLearnGame" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
 
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
 
 
global sprite
sprite = CreateSprite(LoadImage("sprite_00.png"))
//SetSpriteSize(sprite, 1, 100)
SetSpriteOffset(sprite, GetSpriteWidth(sprite)/2, GetSpriteheight(sprite)/2)
SetSpritePositionByOffset(sprite, 100, 100)
 
FixSpriteToScreen(sprite,1)
 
do
	SetWindowTitle("FPS = "+str(ScreenFPS()))
	Printc("Sprite Angle is ")
    Print(Str(GetSpriteAngle(sprite),2)+" Degrees")
	print(ScreenToWorldX(GetSpriteX(sprite)))
	print(ScreenToWorldY(GetSpriteY(sprite)))
	
	MouseX# = GetPointerX()
	MouseY# = GetPointerY()
	PointSprite(sprite,MouseX#,MouseY#)

	MovePlayer()

   Sync()
loop

function MovePlayer()
    x# = GetSpriteXByOffset(sprite)
    y# = GetSpriteYByOffset(sprite)
    
    Vx# = GetViewOffsetX()
    Vy# = GetViewOffsetY()

    angle# = GetSpriteAngle(sprite)
    speed# = 2.0
     
    if (GetRawKeyState ( 65 )) //LEFT
		
		 if (x# + (sin(angle#-90) * speed# ) >= 650) and (x# + (sin(angle#-90) * speed# ) <= 750)
			 inc x#, sin(angle#-90) * speed#           			
         endif	
         
         if (y# - (cos(angle#-90) * speed# ) >= 300) and (y# - (cos(angle#-90) * speed# ) <= 400)
			  dec y#, cos(angle#-90) * speed#   
         endif
         		
         inc Vx#, sin(angle#-90) * speed#  
		dec Vy#, cos(angle#-90) * speed#  		
         			 
    endif     
  
    if (GetRawKeyState ( 87 )) //DOWN
		
		 if (x# + (sin(angle#) * speed# ) >= 650) and (x# + (sin(angle#) * speed# ) <= 750)
			 inc x#, sin(angle#) * speed#           
         endif	
         
         if (y# - (cos(angle#) * speed#   ) >= 300) and (y# - (cos(angle#) * speed# ) <= 400)
			 dec y#, cos(angle#) * speed#   
         endif
		   
		  inc Vx#, sin(angle#) * speed#  
		dec Vy#, cos(angle#) * speed#    
		   
    endif 
      
    if (GetRawKeyState ( 68 )) //RIGHT
		
		 if (x# + (sin(angle#+90) * speed#  ) >= 650) and (x# + (sin(angle#+90) * speed#  ) <= 750)
			inc x#, sin(angle#+90) * speed#            
         endif	
         
         if (y# - (cos(angle#+90) * speed#  ) >= 300) and (y# - (cos(angle#+90) * speed#  ) <= 400)
			dec y#, cos(angle#+90) * speed#     
         endif
    
		inc Vx#, sin(angle#+90) * speed#  
		dec Vy#, cos(angle#+90) * speed#  
    
    endif 

    if (GetRawKeyState ( 83 )) //UP
		
		 if (x# + (sin(angle#+180) * speed# ) >= 650) and (x# + (sin(angle#+180) * speed# ) <= 750)
			inc x#, sin(angle#+180) * speed#         
         endif	
         
         if (y# - (cos(angle#+180) * speed# )  >= 300) and (y# - (cos(angle#+180) * speed#   ) <= 400)
			 dec y#, cos(angle#+180) * speed#    
         endif		
    
		inc Vx#, sin(angle#+180) * speed#  
		dec Vy#, cos(angle#+180) * speed#    
    endif 
     
	SetSpritePositionByOffset(sprite, x#, y#)

	SetViewOffset(Vx#,Vy#)

endfunction