Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Getting Started

Best choice for a 3D platform game

You feel lost? You don't understand how to use ShiVa?

Best choice for a 3D platform game

Postby TailsetSonic » 14 May 2012, 13:07

Hello all !

I'm currently working on my 3D platform game projet and I just started with Shiva. I understand more and more mechanisms but I still don't know "how" to make exactly what I wish. I slowly find some solution but I prefer not make mistake now.

I don't really how to represent my character in the game to exploit the dynamics as well as i can. For example, my game will look like a 3D Sonic game. To begin basically, I grouped a cube that represent the mesh I will have (because I don't already have) and a dynamic ball that will rotate to move the character. When I push on a key I add an angular velocity on the ball.

The problem is that I don't think it's the more adviced way to make it. Moreover, Sonic is always sticked on the platform for any slope variation while he doesn't jump and he dosn't bounce on the floor. That's why I think I make a terrible mistake but I don't really see how to make it.

I look on a lot of documentation but nothing that really helped me :(

Thanks

Edit : Do you think that begining from the "First person view" sample is a good idea ?
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby broozar » 14 May 2012, 13:23

hi TailsetSonic,

i think you are on the right track.

TailsetSonic wrote:When I push on a key I add an angular velocity on the ball.

you can do that, however i recommend dynamics.addForce.

if i understand you correctly, you are having troubles making your character stand still while he is standing on a slope? here's an excerpt form my 3rd person camera rig for detecting slopes:

Code: Select all
    --trace down for slope
    local hHit2, hDist2, hSurf2, hx2, hy2, hz2, i2, j2, k2 = scene.getFirstHitColliderEx ( application.getCurrentUserScene ( ), rx, ry, rz, 0, -1, 0, this.CharacterWidth ( ) )
    local angletoground = math.acos ( math.vectorDotProduct ( i2, j2, k2, 0, 1, 0 ) )


now, with this angle, you can determine wheter you want the character to slide or not,

Code: Select all
if ( angletoground < this.MinSlope ( ) and angletoground > 0 ) then --this.MinSlope is a member variable , in my case a number with the value 20

either by disabling dynamics completely

Code: Select all
dynamics.enableDynamics ( this.playersphere ( ), false )


and switching it on again as soon as you press a movement key, or set the object in a kinematic state to make it immune to any force,

Code: Select all
dynamics.setKinematic ( this.playersphere ( ), true )


or set the velocity to 0.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: Best choice for a 3D platform game

Postby TailsetSonic » 14 May 2012, 13:40

Thank you for your precious answer :)

This resolve a part of the problem effectively. But the bigest problem I will have is when the character is moving. The character will run fast and stay "sticked" on the floor when the slope angle change progressively.

Image

For example in this case, Sonic will stay on the floor even if he runs fast.

But anyway thank you for your advice.
Just, why addForce is more recommanded ?
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby broozar » 14 May 2012, 13:47

well the physics engine is there to simulate "realistic" physics. if you want a character running at 60 miles per hour and still glue him to the ground, i guess physics is not what you need ;) old games have no dedicated physics engine. if you want to glue your character to the ground no matter what, do a raycast every frame down the feet of the character (first code from the post above) and move your character there, plus his height, and don't use physics at all.

as you have to move your character with object.translate or object.setTranslation now, you will have no collision detection, so you will need another raycast just in front of the character to detect if you got a wall in front of you or not.

PS: addForce is just a personal preference that i found to feel more natural for character movement, that's all.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: Best choice for a 3D platform game

Postby TailsetSonic » 14 May 2012, 14:03

Ok, thank you for this information.

I have a more precise idea about what to do. I will simply unable the gravity when my character is running and use the method you advice me. That will allow me to keep the collision detection with the ennemies for example without doing it manually.

Thank you again :D .
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby Fraser » 14 May 2012, 14:21

If you want to retain shiva physics for main character then I recommend using a sphere body for collider. Then lock using dynamics.setAngularDampingEx ( )

Then you can control rotation in characters local space depending on normal of floor contact point.

It will also have benefit of being able to unlock and roll around like a ball.

I use a sphere collider for main character on my 2.5d platformer (click on my siggy and check out bob orange). He doesn't do the quickroll technique at the moment, but will before release.
Fraser Ashworth,
https://www.cortex7.net
User avatar
Fraser
Platinum Boarder
Platinum Boarder
 
Posts: 1057
Location: Europe

Re: Best choice for a 3D platform game

Postby TailsetSonic » 18 May 2012, 12:52

Hello. Thank you Frazer for answering.

I have now an other problem about the rotation of my character.
I get the colide point this way to hit under my character feet depending on its rotation

Code: Select all
local x, y, z = object.getTranslation       ( o, object.kGlobalSpace )
local rX, rY, rZ = object.getYAxis ( o, object.kGlobalSpace )
           
local hHit, hDist, hSurf, hx, hy, hz, i, j, k = scene.getFirstHitColliderEx ( s, x, y, z, rX * -1, rY * -1, rZ * -1, 0.75 )


then I don't really succeed to use this i, j and k to rotate my character.
These i, j and k represent the coordinates of the collider's normal vector of length 1, but I don't see how to use it to make my character being sticked on the ground.

Maybe I forgot some mathematics about that ...
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby shivachristy » 18 May 2012, 13:46

you probably want to add a downward force to emulate gravity better. do this onenterframe.
http://cur.lv/1x0wx via an ad! Thanks
User avatar
shivachristy
Platinum Boarder
Platinum Boarder
 
Posts: 1035
Location: UK

Re: Best choice for a 3D platform game

Postby broozar » 18 May 2012, 14:10

the normal vector is uninteresting in your case, use the hit coordinates (hx, hy, hz) and setTranslation() your character there, plus the character height.

as far as rotation goes, look at your sonic screen, he's upright, even if the ground has like a 45 degree angle.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: Best choice for a 3D platform game

Postby TailsetSonic » 19 May 2012, 07:05

Yes but when he runs it's not the case :?
And I don't want to mak exactly a Sonic but it's a good way to show you.

Just to give an illustration of what behaviour I can expect :

Image

@shivachristy : I didn't understand. The gravity of dynamics is not sufficient ?
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby broozar » 19 May 2012, 17:12

but sonic only changes his orientation in loopings. that means, the loopings are a special case that has been designed by the developers separately from the rest of the movement syste.

in shiva, you can realize this with states defined in your AI model. you can use a this.sonicStandard() state to define the always-upright behaviour, and a this.sonicLooping() state that handles loopings. you can switch between those states elegantly using the sensor collision events.

shivachristy suggested that you can apply an additional force to sonic when he walks to make him stick to the floor more, but that won't help you with your loopings.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: Best choice for a 3D platform game

Postby TailsetSonic » 22 May 2012, 12:51

Thank you for your advice.

I increased the force for gravity for an other reason but it helps for my problem here.
Finally I found a solution to my problem. I just have to adjust my code but it looks fine.

Each frames I try to parallelize the normal vector with the Y axis coordinates. :D


I have an other question, is it possible to make an object translate depending on the referential of an other object (like the camera) ?
It's easy to use object.kGlobalSpace or kLocalSpace but I would like to use the local space of an other.
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby broozar » 22 May 2012, 18:54

you can transform a point of an object in its local space into global space, using http://www.stonetrip.com/developer/doc/api/object-transformPoint

alternatively, you could use kParentSpace. parenting does not mean for all children to inherit translation or ratation automatically, you can switch it off using http://www.stonetrip.com/developer/doc/api/object-setTransformOption, so kParentSpace would be "just" another localspace that the children could use.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: Best choice for a 3D platform game

Postby TailsetSonic » 31 May 2012, 11:28

Thank you again for your help, I forgot to answer.

I think I don't make it in the good way ... Compared to what I already can do with Shiva's physics, I add a kind of "glue" effect when my character runs. I'm sure there is a simplier way that what I do ... For the moment I adapt the behaviour of my dynamic ball depending on if I am on the ground or not. If I am close to the ground, I try to stick my cube with a rotation and a translation. The problem is that at each frames I make a matchTranslation with the dynamic ball. And here the problems appear because if I translate the ball in the same time than the cube the movement become unstable, crazy ...

Anyway I'm sure I make a ugly big mistake somewhere. Like the sonic example is not that good, I try to make mine.

First, the behaviour I try to make, the example is in 2D for a better understanding. I begin from the black ball, in green is what I need to get and in red what happens with the normal physics.

Image


Then, if I chose to use a "box" (a mesh later) and a dynamic ball I have this problem. (In red is the usual behaviour and in green what I want)

Image

For the moment the solution I experiment add a lot of but because my "physics patch" have some conflicts with the Shiva's dynamics ... I don't see how to do
TailsetSonic
Senior Boarder
Senior Boarder
 
Posts: 41

Re: Best choice for a 3D platform game

Postby broozar » 31 May 2012, 14:35

yes, then you can't use dynamics. dynamics is a simulation of real world physics, what you try to do has nothing in common with real world physics.

the way you can do it is similar to everything we have discussed above,
1. launch a ray to the ground, get hit coordinate, set character there
2. get the hit normal vector, rotate your box accordingly. (i have actually no formula for that, it's harder than it sounds. you need some math skills...)
3. launch a ray to the ground with a little offset depending on the direction of the character (scan the front if you want to move forward, scan left if you want to strafe left, etc)
4. get those new hit coordinates, move the character there and repeat from 2

the most difficult part is the rotation from the normal vector. it's easy as long as you only work with 2 dimensions, but i have never been able to figure it out for 3. any updates are appreciated.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Next

Return to Getting Started