Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Manipulate Gravity

All about the StoneScript

Manipulate Gravity

Postby TheDukeVIP » 20 Apr 2012, 11:02

Hello everyone,

Is it possible to make models fall to another object, not to the ground ?
Like the wall walking mechanism in Prey, where the player can walk on walls, upside down etc.

Thanks.
TheDukeVIP
Fresh Boarder
Fresh Boarder
 
Posts: 9

Re: Manipulate Gravity

Postby broozar » 20 Apr 2012, 12:21

it is easy to manipulate gravity using http://www.stonetrip.com/developer/doc/api/scene-setDynamicsGravity, this will not give you the result you want. if you want to enable the player to be able to walk on walls, you need to do a raycast to his feet, either to a sensor or a collider http://www.stonetrip.com/developer/doc/api/scene-getFirstHitColliderEx, then get the hit face normal and rotate your player accordingly.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3479
Location: Berlin - Germany

Re: Manipulate Gravity

Postby TheDukeVIP » 20 Apr 2012, 13:18

Raycast to a collider, get normal vertex then rotate... looks nice, must give it a try!
Thanks :)
TheDukeVIP
Fresh Boarder
Fresh Boarder
 
Posts: 9

Re: Manipulate Gravity

Postby TheDukeVIP » 24 Apr 2012, 19:41

I have a little problem with the following code:
Code: Select all
...
local Tx, Ty, Tz = object.getTranslation ( ShipObject, object.kGlobalSpace )
local Rx, Ry, Rz = object.getRotation ( ShipObject, object.kGlobalSpace )
local Dx, Dy, Dz = object.getDirection ( ShipObject, object.kGlobalSpace )
...
local hHitObject, nHitDist, nHitSurfaceID, ix, iy, iz, ni, nj, nk = scene.getFirstHitColliderEx ( MyScene, Tx, Ty, Tz, Dz, Dy,Dz , 1000 )

or this one
Code: Select all
local hHitObject, nHitDist, nHitSurfaceID, ix, iy, iz, ni, nj, nk = scene.getFirstHitColliderEx ( MyScene, Tx, Ty, Tz, 0, 0, Dz, 1000 )

(the same thing with only Y or X direction)
and also this one
Code: Select all
local hHitObject, nHitDist, nHitSurfaceID, ix, iy, iz, ni, nj, nk = scene.getFirstHitColliderEx ( MyScene, Tx, Ty, Tz, math.abs ( Dx ), math.abs ( Dy ), math.abs ( Dz ), 1000 )

Well, sometimes I get a collision detection, but with certain model rotations the collision is lost.
Is using getDirection results a good way or do I need something else ?

Thank in advance :)
TheDukeVIP
Fresh Boarder
Fresh Boarder
 
Posts: 9

Re: Manipulate Gravity

Postby broozar » 24 Apr 2012, 20:01

what exactly is it you try to achieve? i thought you wanted to trace down (-Y) to get what's unter the player feets. of course, if you use getDirection which traces forward (-Z), you might get something you did not have in mind.

so.

1. make a test level consisting of level geometry (colliders) and sensors (the walkways you want to walk on).
2. give those sensor an ID of 1.
3. build a half-pipe with colliders and superimposed sensors.
4. use scene.getFirstHitSensorEx and trace DOWN in LOCAL SPACE, and while doing so, check for the ID 1
5. if ID 1 is found, use the normal vector that comes out of getFirstHitSensorEx and rotate your character appropriately.
6. move your character appropriately to the position where the sensor ray hit, plus a character offset if your character's feet are not at 0,0,0. do not use dynamics on the player.

hope that helped, sorry that i cannot code you an example right now, i am swamped.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3479
Location: Berlin - Germany

Re: Manipulate Gravity

Postby TheDukeVIP » 25 Apr 2012, 16:38

Sorry again for any annoyance, this time I brought a picture which might better explain my problem !
Image
The big red circle (setup as a collider) is the track I need the car to stick to. the car positions can be upside/down (1), normal position (3) or any other angle (2 and 4)
For scene.getFirstHitColliderEx to work, I need a ray vector direction, which, if I'm right, is the car (-Y), and so, my problem is to get this vector direction coordinates and use them as nRayDirX, nRayDirY, nRayDirZ.
TheDukeVIP
Fresh Boarder
Fresh Boarder
 
Posts: 9

Re: Manipulate Gravity

Postby broozar » 25 Apr 2012, 23:07

maybe you should connect with berdal, he seems to have it nailed down pretty well.
http://www.stonetrip.com/developer/forum/viewtopic.php?f=25&t=24829

here are my ideas. the raycast and translation itself is not very hard:

Code: Select all
    local o = this.getObject ( )
    local ox, oy, oz = object.getTranslation ( o, object.kGlobalSpace )
    local dx, dy, dz = object.getDirection ( o, object.kGlobalSpace ) --for the forward movement
    local yx, yy, yz = object.getYAxis ( o, object.kGlobalSpace ) --for the down trace
   
    --desired move forward with framerate dependent scale factor
    local speed = 1 -- your speed
    local ndx, ndy, ndz = math.vectorScale ( dx, dy, dz, application.getLastFrameTime ( )*60*speed )
   
    local hObj, hDist, hSurf, rx, ry, rz, rnx, rny, rnz = scene.getFirstHitColliderEx ( application.getCurrentUserScene ( ), ndx, ndy, ndz, -yx, -yy, -yz, 2 )
   
    --normalize normal vector and scale by 0.85 for object height
    local rx_n, ry_n, rz_n = math.vectorNormalize ( rnx, rny, rnz )
    local rx_s, ry_s, rz_s = math.vectorScale ( rx_n, ry_n, rz_n, 1 )
   
    object.translateTo ( o, rx+rx_s, ry+ry_s, rz+rz_s, object.kGlobalSpace, 0.8 ) --smooth

--unfinished! rotation is missing! will not work without!


what is giving me headaches is the rotation, since i am no math genius. you need to find a way to transform the normal vector rnx, rny, rnz into useful angles that you can then apply to your object using object.setRotation.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3479
Location: Berlin - Germany

Re: Manipulate Gravity

Postby TheDukeVIP » 01 May 2012, 21:07

Hello !
It's me again !
Thank you for your answer, also, I asked Berdal and he told me to "use object.lookAtWithUp, with the normal of the surface as up vector"
So I'll give it a try asap !

Thanks again for your support :)
TheDukeVIP
Fresh Boarder
Fresh Boarder
 
Posts: 9

Re: Manipulate Gravity

Postby Fraser » 02 May 2012, 22:24

Duke, another thing you might consider doing is this:

Setup physics as normal, big ball as collider, trains as dynamic box bodies.

Set scene gravity to zero:
scene.setDynamicsGravity ( hScene,0,0,0 )

Then in your train onEnterFrame or StateLoop synthesize your own gravity:
dynamics.addForce ( hTrain, K*x,K*y,K*z, object.kGlobalSpace )

K is your new gravitational constant.

x,y,z is the vector that points to the centre of your mini planet.

This will give behaviour like mario galaxy.
Fraser Ashworth,
https://www.cortex7.net
User avatar
Fraser
Platinum Boarder
Platinum Boarder
 
Posts: 1056
Location: Europe


Return to Game programming