Help with Spherical Gravity and rotation

Blitz3D Forums/Blitz3D Beginners Area/Help with Spherical Gravity and rotation

VladHQ(Posted 2015) [#1]
Hello,
First I would like to say that I find this engine super easy to use and I am using Linux and emulating it in Wine, and so far everything looks fantastic. I've been making this space/moon type FPS game and I can't seem to get gravity working perfectly. Right now, it works, however when I go farther on the moon, I see that my character is not actually rotating and it's causing issues.

Here's my code:
AppTitle "FPS"
Graphics3D 800,600,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
HidePointer

;Objects
Global player = CreatePivot()
Global camera = CreateCamera(player)
Global cube = CreateCube()
Global light = CreateLight()
Global cube2 = CreateCube(player)
Global level1 = CreateSphere(64)
col_level1 = 1
col_player = 2
col_cube = 3
jump_flag = 0

;Textures 
CameraClsColor camera,0,0,0
box = LoadTexture("box.jpg") 
moon = LoadTexture("moon.jpg") 

;Object Details 
EntityType cube,col_cube 
EntityType player,col_player 
EntityType level1,col_level1
EntityRadius player,1
EntityRadius level1,1
PositionEntity level1, 0, -100, 0
PositionEntity player,0,25,0
PositionEntity light,0,2,0 
PositionEntity camera,0,1,0
EntityTexture cube,box 
EntityTexture level1, moon
CameraRange camera,0.25,200
ScaleEntity level1, 100, 100, 100
ScaleTexture moon,.5,.5

;Cubes
PositionEntity cube,0,1,5
cube2 = CopyEntity(cube)

PositionEntity cube2,2,3,5
cube2 = CopyEntity(cube)
PositionEntity cube2,4,5,5
cube2 = CopyEntity(cube)
PositionEntity cube2,6,7,3
cube2 = CopyEntity(cube)
PositionEntity cube2,8,9,5
cube2 = CopyEntity(cube)
PositionEntity cube2,10,11,5
cube2 = CopyEntity(cube)
PositionEntity cube2,12,13,5

;Collisions 
Collisions col_player,col_level1,2,2 
Collisions col_player,col_cube,2,2  

;Called Functions 
While Not KeyHit(1)
	Cls

	;Spherical gravity Fail
	a_p = EntityPitch(player, 1)
	a_y = EntityYaw(player, 1)
	a_r = EntityRoll(player, 1)

	c_p = EntityPitch(camera, 1)
	c_y = EntityYaw(camera, 1)
	c_r = EntityRoll(camera, 1)
	PointEntity player, level1
	PointEntity camera, player
	MoveEntity player, 0, 0, 0.2
	RotateEntity player, a_p, a_y, a_r, 1
	RotateEntity camera, c_p, c_y, c_r, 1

	;Jumping
	If KeyHit(57) And jump_flag = 0 Then
		jump_flag = 1
	End If
	If jump_flag = 1 Then
		MoveEntity player,0,Cos(jump_value)/10,0
		jump_value = jump_value + 2
		If jump_value >60 Then
			jump_flag = 2
		End If
	ElseIf jump_flag = 2 Then
		MoveEntity player,0,-Cos(jump_value)/10,0
		jump_value = jump_value - 2

		For k = 1 To CountCollisions(player)
			If CollisionEntity(player,k) = level1 Then
				If KeyHit(18)
					;
				End If
			End If
  			If CollisionEntity(player,k) = plane Then 
				;
			End If
		Next

		If jump_value < 0 Then
			jump_flag = 0
			jump_value = 0
		End If
	End If
	If jump_flag <> 1 Then
		; If not jumping turn on gravity
		;
TranslateEntity player, 0, 0.2, 0	End If

	control()
	UpdateWorld()
	RenderWorld()
	MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
	Flip
Wend
End
;Functions 
Function control() 
	If KeyDown(42) ;Shift (for running)
        If KeyDown(17) And KeyDown(30) ;W and A
            MoveEntity player,-0.15,0,0.15
        ElseIf KeyDown(17) And KeyDown(32) ;W and D
            MoveEntity player,0.15,0,0.15
        ElseIf KeyDown(17) And KeyDown(30) ;S and A
            MoveEntity player,-0.15,0,-0.15
        ElseIf KeyDown(17) And KeyDown(32) ;S and D
            MoveEntity player,0.15,0,-0.15
        ElseIf KeyDown(17) ;W
            MoveEntity player,0,0,0.15
        ElseIf KeyDown(31) ;S
            MoveEntity player,0,0,-0.15
        ElseIf KeyDown(30) ;A
            MoveEntity player,-0.15,0,0
        ElseIf KeyDown(32) ;D
            MoveEntity player,0.15,0,0
        End If
    Else ;Walking
        If KeyDown(17) And KeyDown(30) ;W and A
            MoveEntity player,-0.1,0,0.1
        ElseIf KeyDown(17) And KeyDown(32) ;W and D
            MoveEntity player,0.1,0,0.1
        ElseIf KeyDown(17) And KeyDown(30) ;S and A
            MoveEntity player,-0.1,0,-0.1
        ElseIf KeyDown(17) And KeyDown(32) ;S and D
            MoveEntity player,0.1,0,-0.1
        ElseIf KeyDown(17) ;W
            MoveEntity player,0,0,0.1
        ElseIf KeyDown(31) ;S
            MoveEntity player,0,0,-0.1
        ElseIf KeyDown(30) ;A
            MoveEntity player,-0.1,0,0
        ElseIf KeyDown(32) ;D
            MoveEntity player,0.1,0,0
        End If
    End If

    TurnEntity player, 0, -MouseXSpeed()/6.0, 0
    TurnEntity camera, MouseYSpeed()/6.0, 0, 0
    If EntityPitch(camera) < -70
        RotateEntity camera, -70, EntityYaw(camera), EntityRoll(camera)
    ElseIf EntityPitch(camera) > 70
        RotateEntity camera, 70, EntityYaw(camera), EntityRoll(camera)
    EndIf
End Function


If you run that, you will see that gravity works okay at first, but once you go away from the boxes and try to explore it will become harder to walk and the character won't rotate as the slope changes. Please help me fix this, I kinda want to make a simple space shooter and I have some sick ideas but I am stuck on this.. :/


videz(Posted 2015) [#2]
hey welcome dude. yeah it's pretty easy to make a 3d game with b3d.

Maybe you can check out some samples and snippets here. A lot of them already has some sort of gravity added to their demo code.


VladHQ(Posted 2015) [#3]
@videz,
I already had a pretty good script with gravity, it worked perfectly, but I am making an FPS that takes place on the actual moon, and it's round. And the gravity does not work around it as the slope changes.

There is this thread that I looked at but it didn't help me:
http://www.blitzbasic.com/Community/posts.php?topic=31079


Stevie G(Posted 2015) [#4]
I think the problem is that Gravity isn't straight down on the sphere ... you need to transform it to be directly beneath the player, in it's local space, like so ...

tformvector 0, -.02 ,0 ,Player , 0
translateentity tformedx(), tformedy(), tfromedz()

Stevie


Rick Nasher(Posted 2015) [#5]
Perhaps this topic/source code might be of interest to you:

http://www.gamasutra.com/view/feature/131997/games_demystified_super_mario_.php?page=2

And the download link to the source from there:
http://gamasutra.com/images/superMarioGalaxyDemystifiedSource.zip

Also see:
http://www.blitzbasic.com/Community/posts.php?topic=65425


Yue(Posted 2015) [#6]
@Rick Nasher

Incredible is code ever surprises me more Blitz3D and me confirms that I will never be a programmer. : D


Rick Nasher(Posted 2015) [#7]
@Yue: I know how you feel. Perhaps not the most brilliant one, because some people are just born math geniuses, but with endurance and original thinking, analyzing code examples you can get far too. The mind is very flexible and capable of learning new tricks. It might take a while but you will get there I am sure. Í have seen you making some very promising things. Still find it a pity you abandoned your Mars Rover project: was looking good.

To make a good game it takes good skills, but also creativity and good ideas. You have creativity and ideas, skill is something that might take a while but you will get there.