Posted: 22nd Apr 2023 17:31
Here is some code from here. https://forum.thegamecreators.com/thread/228158

And could someone fix he tital? lol, Angle from Angel ha ha.

This is code I edited from Virtual Nomad and need some help finding out a way to stop my player if there is a wall or high slope. Im guessing I have to find the player Y pos and then limit the movement from there but everything I try stops the player but then moves him into the mesh, it stops the movement for the height but not for the x.

Im sure i am just overthinking this and it is probably simple but I have at least tried. I believe the check for my players Y is before the ObjectRayCast so there is confusion in the program.

This is my only movement code so there is nothing making it act like it is.

+ Code Snippet
	    MMX# = GetPointerX()-512.0	:	MMY# = GetPointerY()-384.0
	    CAMYA# = GetobjectAngleY(player)

	    SetObjectRotation(player, 0, CAMYA# + MMX#*Sensitivity#, 0)
    
	    anglea# = GetObjectAngleY(player)
	
        SetObjectRotation(player, 0, anglea#+GetJoystickX(), 0)
    
        SetRawMousePosition(512,384)
 
        angle# = GetobjectAngleY(player)
 
	    if GetJoystickX() <> 0.0 and attack=0  or GetJoystickY() <> 0.0 and attack=0 or GetRawKeyState(38) and attack=0
		if moving = 1 then cx# = cx# + SIN(angle#)* -GetJoystickY()*1.0 //Move
		if moving = 1 then cz# = cz# + COS(angle#)* -GetJoystickY()*1.0

		if moving = 2 then cx# = cx# + SIN(angle#)* -GetJoystickY()*3.0 //Move
		if moving = 2 then cz# = cz# + COS(angle#)* -GetJoystickY()*3.0

		cx# = cx# + SIN(angle#+90)* GetJoystickX()*1.0 //Strafe
		cz# = cz# + COS(angle#+90)* GetJoystickX()*1.0
		
		if moving = 1 then cx# = cx# + SIN(angle#)* GetRawKeyState(38)*1.0 //Move
		if moving = 1 then cz# = cz# + COS(angle#)* GetRawKeyState(38)*1.0
		
		if moving = 2 then cx# = cx# + SIN(angle#)* GetRawKeyState(38)*3.0 //Move
		if moving = 2 then cz# = cz# + COS(angle#)* GetRawKeyState(38)*3.0
		
		ray = ObjectRayCast(terrain,cx#,120,cz#,cx#,-16,cz#)
		cy# = GetObjectRayCastY(0)
		if jump=0 then SetobjectPosition(player,cx#,cy#+5,cz#)
		
    	Endif


Edit,

I even tried finding the GetObjectHeightMapHeight but then got confused.
Posted: 23rd Apr 2023 16:39
i would avoid using the slope from point a to b to determine if terrain is impassable.
if you're moving directly into/at the slope (ie, perpendicular to it), it would be fine but if you approached the slope at a slight angle (almost parallel) then the difference in elevation from point a to b would be much more gradual.

instead, consider using the Normals of the terrain at point b which are static/don't change no matter your angle of approach.

using your same ray:
+ Code Snippet
NX# =  ABS(GetObjectRayCastNormalX( 0 ))
NZ# =  ABS(GetObjectRayCastNormalZ( 0 )) 

...which return values between 0.0 and 1.0 (-1.0 to 1.0 without ABS()).

now say, for example, that anything more than 45 degrees is impassable terrain:
+ Code Snippet
MaxN# = 0.5

then you could check those normals against your threshold to determine if you can move to point b:
+ Code Snippet
If NX# < MaxN#  and NZ# < MaxN# then SetCameraPosition(1,cx#,cy#+10,cz#)


ie, try to keep an eye on the values of NX# & NZ#. if the terrain i'm trying to move to (point b) is impassable then the sky goes red, else green:


you could take this further and say it's impassable if you're going up the hill but not down it by first checking the elevation at point b and comparing it to your current elevation. IE, if down then skip the checks and simply move there.

another usage of the normals might determine how fast you can go up where the steeper the terrain, the slower you move.

pretty handy, eh?

i hope this helps

add: my first time implementing the theory. if i've stated anything wrong above, someone please let us know
Posted: 23rd Apr 2023 20:06
GetObjectRayCastNormalX in theory it makes a lot of sense but in practice my player stops then after a moment goes up the slope anyhow lol.

I think my dream of perfect collision is over at this point and i will just use regular ray casting but I really wanted to use your code because it gets perfect sliding collision.

Thank you for your help and understanding. But i need to just move forward as this collision thing is slowing me down. I will not use a controller, that I do know
Posted: 24th Apr 2023 0:22
Ok so this is what I am doing now, regular sliding collision .

The only thing I hate about it is the third person player never is on the ground so i have to keep the camera close and also camera collision is hard to do. This is why I am looking for other ways.

+ Code Snippet
 // Player movement and Camera placment for third person
 
	    MMX# = GetPointerX()-512.0	:	MMY# = GetPointerY()-384.0
	    CAMYA# = GetobjectAngleY(player)

	    SetObjectRotation(player, 0, CAMYA# + MMX#*Sensitivity#, 0)
    
	    anglea# = GetObjectAngleY(player)
	
        SetObjectRotation(player, 0, anglea#+GetJoystickX(), 0)
    
        SetRawMousePosition(512,384)
 
 
 rem Old object position
 oldx#=GetObjectX(player)
 oldy#=GetObjectY(player)-4.0
 oldz#=GetObjectZ(player)
 `
 rem Move camera wasd keys
 if ( GetRawKeyState( 87 ) ) and moving = 1 then MoveObjectLocalZ( player, 1.5 )
 if ( GetRawKeyState( 83 ) ) and moving = 1  then MoveObjectLocalZ( player, -1.5 )
 if ( GetRawKeyState( 65 ) ) and moving = 1  then MoveObjectLocalX( player, -1.5 )
 if ( GetRawKeyState( 68 ) ) and moving = 1  then MoveObjectLocalX( player, 1.5 )

 if ( GetRawKeyState( 87 ) ) and moving = 2 then MoveObjectLocalZ( player, 3 )
 if ( GetRawKeyState( 83 ) ) and moving = 2  then MoveObjectLocalZ( player, -3 )
 if ( GetRawKeyState( 65 ) ) and moving = 2  then MoveObjectLocalX( player, -3 )
 if ( GetRawKeyState( 68 ) ) and moving = 2  then MoveObjectLocalX( player, 3 )

 if ( GetRawKeyState( 87 ) ) and moving = 3 then MoveObjectLocalZ( player, 4 )
 if ( GetRawKeyState( 83 ) ) and moving = 3  then MoveObjectLocalZ( player, -4 )
 if ( GetRawKeyState( 65 ) ) and moving = 3  then MoveObjectLocalX( player, -4 )
 if ( GetRawKeyState( 68 ) ) and moving = 3  then MoveObjectLocalX( player, 4 )
 rem Move camera up down left right keys
 if ( GetRawKeyState( 38 ) ) and moving = 1 then MoveObjectLocalZ( player, 1.5 )
 if ( GetRawKeyState( 40 ) ) and moving = 1  then MoveObjectLocalZ( player, -1.5 )
 if ( GetRawKeyState( 45 ) ) and moving = 1  then MoveObjectLocalX( player, -1.5 )
 if ( GetRawKeyState( 46 ) ) and moving = 1  then MoveObjectLocalX( player, 1.5 )

 if ( GetRawKeyState( 38 ) ) and moving = 2 then MoveObjectLocalZ( player, 3 )
 if ( GetRawKeyState( 40 ) ) and moving = 2  then MoveObjectLocalZ( player, - 3)
 if ( GetRawKeyState( 45 ) ) and moving = 2  then MoveObjectLocalX( player, -3 )
 if ( GetRawKeyState( 46 ) ) and moving = 2  then MoveObjectLocalX( player, 3 )

 if ( GetRawKeyState( 38 ) ) and moving = 3 then MoveObjectLocalZ( player, 4 )
 if ( GetRawKeyState( 40 ) ) and moving = 3  then MoveObjectLocalZ( player, -4 )
 if ( GetRawKeyState( 45 ) ) and moving = 3  then MoveObjectLocalX( player, -4 )
 if ( GetRawKeyState( 46 ) ) and moving = 3  then MoveObjectLocalX( player, 4 )
 
 if ( getrawjoystickexists(1) )
    MoveobjectLocalZ(player,-getrawjoysticky(1)*4)
    MoveObjectLocalX(player,getrawjoystickx(1)*4)
    RotateObjectGlobalY(player,getrawjoystickrx(1)*4)
    RotateObjectLocalX(player,getrawjoystickry(1)*4)
endif
 `
 rem Gravity on camera
  cy# = GetObjectRayCastY(0)
 if cy#<25 then SetObjectPosition(player,getObjectx(player),getObjecty(player)-1,getObjectz(player))
 if cy#>25 then SetObjectPosition(player,getObjectx(player),getObjecty(player)-4,getObjectz(player))

 rem New position
 newx#=GetObjectX(player)
 newy#=GetObjectY(player)-4
 newz#=GetObjectZ(player)
 `
 rem Adjust Y to match floor
 if ObjectSphereSlide(0,oldx#,oldy#,oldz#,newx#,newy#,newz#,5.0)>0
  newx#=GetObjectRayCastSlideX(0)
  newy#=GetObjectRayCastSlideY(0)
  newz#=GetObjectRayCastSlideZ(0)
  SetObjectPosition(player,newx#,newy#+4.0,newz#)
 endif

            
   		  SetCameraPosition(1,GetObjectX(player),GetObjectY(player)+13,GetObjectZ(player))
		  SetCameraRotation(1,360-(GetObjectAngleX(player)),(GetObjectAngleY(player)),360-(GetObjectAngleZ(player)))
		 if moving =0 then MoveCameraLocalZ(1,-20)
	     if moving =1 then MoveCameraLocalZ(1,-15)
	     if moving =2 then MoveCameraLocalZ(1,-18)
	     if moving =3 then MoveCameraLocalZ(1,-18)