Posted: 28th Sep 2021 7:07
Ok so moving onto the platform for my platform game.

There is no platform game if I fall of the platform lol.

I tried this but well, its poorly written, just trying to do something

+ Code Snippet
for Platforms=6 to 7
	
px=GetSpriteX(player)
py=GetSpriteY(player)
plx=GetSpriteX(Platforms)
ply=GetSpriteY(Platforms)	
	
if GetPhysicsCollision(player,Platforms)=1
SetSpritePosition(player,plx,py)
print(hit)
endif

next Platforms
Posted: 28th Sep 2021 11:03
Box2D takes care of this for you, your player should be knimatic, your platform should be static, box2D does the the rest, you only need to check for collisions if you want a reaction from that collision.
Posted: 28th Sep 2021 21:36
Box2D?

Where to buy this at?

Thank you
Posted: 28th Sep 2021 21:46
a version of Box2D is implemented in AGK. it's the 2D and Sprite Physics system.
Posted: 28th Sep 2021 22:03
Virtual Nomad

So what is the box 2d code for what I am needing?

All i want to do is make my player sprite stay on a platform.

I am setting up trigger boxes but this should not have to be done, Im guessing.
Posted: 28th Sep 2021 23:12
all of THIS is giving me deja vu, and everyone has a different approach but basic platformer mechanic using built-in physics:
+ Code Snippet
// Project: Jump 
// Created: 2021-09-28
// By: Virtual Nomad
// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Jump" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 60, 0 ) 
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 ) 

SetPhysicsScale(0.1)

Player = CreateSprite(0)
	SetSpriteSize(Player,32,32)
	SetSpritePositionByOffset(Player,200,600)
	SetSpritePhysicsOn(Player,2)
	SetSpritePhysicsCanRotate(Player,0)
	
Box = CreateSprite(0)
	SetSpriteSize(Box,64,64)
	SetSpritePositionByOffset(Box,500,600)
	SetSpritePhysicsOn(Box,2)
	SetSpritePhysicsCanRotate(Box,0)
	SetSpriteColor(Box,255,0,0,255)

Plat1 = CreateSprite(0)
	SetSpriteSize(Plat1, 300,32)
	SetSpritePositionByOffset(Plat1,840,600)
	SetSpritePhysicsOn(Plat1,1)
	
Plat2 = CreateSprite(0)
	SetSpriteSize(Plat2, 300,32)
	SetSpritePositionByOffset(Plat2,500,500)
	SetSpritePhysicsOn(Plat2,1)

Coin = CreateSprite(0)
	SetSpriteSize(Coin,32,32)
	SetSpritePositionByOffset(Coin,500,400)
	SetSpriteShapeBox(Coin,-8,-8,8,8,0)
	SetSpriteColor(Coin,255,255,0,255)

Score = 0
do

    If GetRawKeyState(27) then Exit

    PX# = GetSpriteXByOffset(Player)	:	PY# = GetSpriteYByOffset(Player)
	Grounded = PhysicsRayCast( PX#,PY#,PX#,PY#+17)
	
    If GetRawKeyState(ASC(" ")) and Grounded
		Jump# = -150.0
	Else
		Jump# = GetSpritePhysicsVelocityY(Player)
	Endif
	
	SetSpritePhysicsVelocity(Player,GetJoystickX()*50.0,Jump#)

	If GetSpriteCollision(Player,Coin)
		SetSpritePositionByOffset(Player,200,600)
		SetSpritePhysicsVelocity(Player,0,0)
		SetSpritePositionByOffset(Box,500,600)
		INC Score
	EndIf
	
	Print("[A/D] = Move, [Space] = Jump")
	Print("Score: "+STR(Score))
    Sync()
loop


PS the red box is push-able
Posted: 28th Sep 2021 23:29
Virtual Nomad

There is no moving platforms in this example that I'm standing on?
Posted: 28th Sep 2021 23:54
where did you say the platforms were moving?

regardless, if you're on a moving platform, add the platform's X/Y velocity to the player's velocity.
Posted: 29th Sep 2021 0:03
Virtual Nomad

I did, but it does not work, its in my code in the first post.

All it does is move my player sprite to the end of the level.

I believe its because of physics.

+ Code Snippet
for Platforms=6 to 7
     
px=GetSpriteX(player)
py=GetSpriteY(player)
plx=GetSpriteX(Platforms)
ply=GetSpriteY(Platforms)   
     
if GetPhysicsCollision(player,Platforms)=1
SetSpritePosition(player,plx,py)
print(hit)
endif
 
next Platforms
Posted: 29th Sep 2021 0:14
sorry but that code doesn't contain platform movement and "Sticking to a kinematic Sprite" could mean a few things.

again, since it seems you're moving forward with using physics, add the platform's X/Y velocity to the player's velocity.

even if you don't use physics, add the platform's x/y change to the player's x/y when the player is standing on a platform (it really doesn't matter if it's moving where the "x/y change" will only add 0,0 if it isn't).
Posted: 29th Sep 2021 0:22
Here is the function

all I know is when I add

SetSpritePosition(player,Playerx+plx,Playery)

my sprite ends at the end om my whole level

but when I just put

SetSpritePosition(player,plx,Playery)

I'm stuck to the platform and cant move

+ Code Snippet
function PlatFormControl()
	
for Platforms=6 to 7
for Player=playerRun  to playerDead	
	
//print(SetPlatFormTime)
platform1x=GetSpriteX(6)
platform1y=GetSpriteY(6)
platform2x=GetSpriteX(7)
platform2y=GetSpriteY(7)

Playerx=GetSpriteXByOffset(Player)
Playery=GetSpriteYByOffset(Player)


plx=GetSpriteX(Platforms)
ply=GetSpriteY(Platforms)	
	
if GetPhysicsCollision(player,Platforms)=1
SetSpritePosition(player,Playerx+plx,Playery)
print(hit)
endif

next Player	
next Platforms

SetPlatFormTime=SetPlatFormTime+1	
	
if 	SetPlatFormTime>0	and SetPlatFormTime<240
inc PlatformMove2#,-0.003
inc PlatformMove1#	,0.003
SetSpritePosition(6,platform1x+PlatformMove1#,30)	
SetSpritePosition(7,platform2x+PlatformMove2#,30)

endif
	
if 	SetPlatFormTime>240	and SetPlatFormTime<480	
inc PlatformMove2#,0.003
inc PlatformMove1#	,-0.003
SetSpritePosition(6,platform1x-PlatformMove1#,30)	
SetSpritePosition(7,platform2x-PlatformMove2#,30)

endif
	
	if 	SetPlatFormTime>480 then SetPlatFormTime=0
	

    
	
endfunction
Posted: 29th Sep 2021 0:55
A moving platform should also be kinematic and will move another kinematic sprite by its self DO NOT use SetSpritePosition while using physics let the physics engine do its work, when you call SetSpritePosition you confuse the physics simulation
Posted: 29th Sep 2021 1:02
when you call SetSpritePosition you confuse the physics simulation


Yes so I'm confused what to do

There is no examples
Posted: 29th Sep 2021 1:30
I don't get what you are not getting??

make a player, set it kinematic, create a platform, make it static if it does not move, kinematic if it does move.

have your player jump onto the platform, the physics engine (Box2D) does the rest ....

I cant explain it any simpler than that!
Posted: 29th Sep 2021 1:43
When I set my player to kinematic he gets no physics or movement from SetSpritePhysicsImpulse

Kinematic is a special case which can be thought of as a moving static body, it will not respond to collisions or forces and will continue at its specified velocity forever, but dynamic items will respond to it and effectively be pushed out of the way.

this is hw I'm moving my player sprite

SetSpritePhysicsImpulse( player, GetSpriteXByOffset(player), GetSpriteYByOffset(player), GetVirtualButtonState(2)/max_speed#, 0.0 )
SetSpritePhysicsImpulse( player, GetSpriteXByOffset(player), GetSpriteYByOffset(player), GetVirtualButtonState(3)/-max_speed#, 0.0 )
Posted: 29th Sep 2021 2:05
1st: i'm using Dynamic sprites, not Kinematic, for moveable stuff. see HERE.

2nd: you haven't been very clear in many of your questions which requires others to guess at what you want. when your title includes Kinematic, some will assume "moving" vs "static" not strict kinematic because i'm not sure why you'd use true kinematic unless you're trying to mix standard sprite stuff with physics sprite stuff (which you have been and it's not recommended to mix multiple methods of moving sprites - for example, using SetSpritePosition on a Physics sprite breaks its physics for that loop cycle)),

3rd: you've revealed that you're using a move method that i'm not using in the example above (impulses); see # 2. it all matters.

4th: i won't re-write this. the notions are in there (crudely implemented) so you can adapt it however you want:
+ Code Snippet
// Project: Jump 
// Created: 2021-09-28
// By: Virtual Nomad
// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Jump" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 60, 0 ) 
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 ) 

SetPhysicsScale(0.1)

Player = CreateSprite(0)
	SetSpriteSize(Player,32,32)
	SetSpritePositionByOffset(Player,200,600)
	SetSpritePhysicsOn(Player,2)
	SetSpritePhysicsCanRotate(Player,0)
	
Box = CreateSprite(0)
	SetSpriteSize(Box,64,64)
	SetSpritePositionByOffset(Box,500,600)
	SetSpritePhysicsOn(Box,2)
	SetSpritePhysicsCanRotate(Box,0)
	SetSpriteColor(Box,255,0,0,255)

Plat1 = CreateSprite(0)
	SetSpriteSize(Plat1, 300,32)
	SetSpritePositionByOffset(Plat1,840,600)
	SetSpritePhysicsOn(Plat1,1)
	
Plat2 = CreateSprite(0)
	SetSpriteSize(Plat2, 300,32)
	SetSpritePositionByOffset(Plat2,500,500)
	SetSpritePhysicsOn(Plat2,2)
	PJoint = CreatePrismaticJoint(Plat2, Plat1,0,0,1,0,0)
#Constant P2X# = GetSpriteXByOffset(Plat2)
PlatToggle = 1

Coin = CreateSprite(0)
	SetSpriteSize(Coin,32,32)
	SetSpritePositionByOffset(Coin,500,400)
	SetSpriteShapeBox(Coin,-8,-8,8,8,0)
	SetSpriteColor(Coin,255,255,0,255)

Score = 0

SetViewOffset(0,0) `just to break the walls

Ground = CreateSprite(0)		:	SetSpritePosition(Ground,0,720)
SetSpriteSize(Ground,1280, 32)	:	SetSpritePhysicsOn(Ground,1)

do

    If GetRawKeyState(27) then Exit
	If P2X# <= 400.0 and PlatToggle = -1 then PlatToggle = 1
	If P2X# >= 600.0 and PlatToggle = 1 then PlatToggle = -1
		SetSpritePhysicsVelocity(Plat2,PlatToggle*100.0,0)
	
    PX# = GetSpriteXByOffset(Player)	:	PY# = GetSpriteYByOffset(Player)
	Grounded = PhysicsRayCast( PX#,PY#,PX#,PY#+17)
	
    If GetRawKeyState(ASC(" ")) and Grounded
		Jump# = -150.0
	Else
		Jump# = GetSpritePhysicsVelocityY(Player)
	Endif
	If Grounded
		ThisGround = GetRayCastSpriteID()
		PMoveX# = GetSpritePhysicsVelocityX(ThisGround)
	Else
		PMoveX# = 0.0
	EndiF

	
	SetSpritePhysicsVelocity(Player,PMoveX# + (GetJoystickX()*50.0),Jump#)

	If GetSpriteCollision(Player,Coin)
		SetSpritePositionByOffset(Player,200,600)
		SetSpritePhysicsVelocity(Player,0,0)
		SetSpritePositionByOffset(Box,500,600)
		INC Score
	EndIf
	
	Print("[A/D] = Move, [Space] = Jump")
	Print("Score: "+STR(Score))
	Print(ThisGround)
    Sync()
loop

Posted: 29th Sep 2021 2:07
I think impulse and force only affect dynamic objects, for kinematic use velocity or maybe try setting the player as dynamic but you will need to add dampening and rotation settings to the sprite or it will just float around uncontrolled (Look for: dampening, angular damping and can rotate), usually things like players, enemy's and moving platforms are kinematic

A kinematic body should collide with a static body and another kinematic body that ^^ just means it will not move like a dynamic body does, a static body should stop a kinematics movement ...

I will play with some code tomorrow to conform this, now even I am confused!!!


Edit:: what ^^ he said!
Posted: 29th Sep 2021 2:11
Virtual Nomad

Looks like im going to have to change some code in my game

You are a big help, in moving my player I am not doing this

If Grounded
ThisGround = GetRayCastSpriteID()
PMoveX# = GetSpritePhysicsVelocityX(ThisGround)

Your a big help

Thank you
Posted: 29th Sep 2021 2:34
you're welcome.

and, i hope you're actually learning from the code and not just pasting. ie, why do you think i added a joint to the movable platform?
+ Code Snippet
PJoint = CreatePrismaticJoint(Plat2, Plat1,0,0,1,0,0)

we didn't used to be so quick to offer code examples because some new folks just expected others to write their games for them without increasing their own knowledge and someday returning the favor to the "next generation".

the best "thank yous" around here are truly taking what's offered and learning from it. some of us evaluate those we help. if we don't see "learning" and those "thank yous", we stop serving them code.

any code i post IS a big THANK YOU to the MANY that took time to "show me the way", and STILL DO (because i've a long way to go ).
Posted: 29th Sep 2021 3:29
Virtual Nomad

No, I am reading all of your example and learning a lot.

You did a lot of things way different then me and all on all I learned a lot just from a couple lines of code.

+ Code Snippet
    If GetRawKeyState(27) then Exit
	If P2X# <= 400.0 and PlatToggle = -1 then PlatToggle = 1
	If P2X# >= 600.0 and PlatToggle = 1 then PlatToggle = -1
		SetSpritePhysicsVelocity(Plat2,PlatToggle*100.0,0)


is a lot simpler then what I am doing

I will use this for a lot of things like jumping on enemy's to how fast I can fall from gravity

+ Code Snippet
    PX# = GetSpriteXByOffset(Player)	:	PY# = GetSpriteYByOffset(Player)
	Grounded = PhysicsRayCast( PX#,PY#,PX#,PY#+17)
	
    If GetRawKeyState(ASC(" ")) and Grounded
		Jump# = -150.0
	Else
		Jump# = GetSpritePhysicsVelocityY(Player)
	Endif
	If Grounded
		ThisGround = GetRayCastSpriteID()
		PMoveX# = GetSpritePhysicsVelocityX(ThisGround)
	Else
		PMoveX# = 0.0
	EndiF


Putting your platform on dynamic I didn't expect and then adding the joint, what is the joint for?


+ Code Snippet
Plat2 = CreateSprite(0)
	SetSpriteSize(Plat2, 300,32)
	SetSpritePositionByOffset(Plat2,500,500)
	SetSpritePhysicsOn(Plat2,2)
	PJoint = CreatePrismaticJoint(Plat2, Plat1,0,0,1,0,0)
#Constant P2X# = GetSpriteXByOffset(Plat2)
PlatToggle = 1