# Character gravity - Macromedia Director 3D

Hi, could someone plaese tell me where i am going wrong here.....I am trying to create some simple gravity effects...so that when the character walks off a surface, he falls to the ground...but my attempts so far have produced strange results: on alignToTerrain locationVector2 = bongoWorld.model("joint70-skeleton").worldPosition locationVector2.y = bongoWorld.model("joint70-skeleton").worldposition.y + 5 directionVector2 = vector(0,-1,0) col2 = bongoWorld.modelsUnderRay(locationVector2,directio nVector2,2,#detailed) if col2.count>0 then repeat with j=1 to col2.count if (col2[j].model <> bongoWorld.model("joint70-skeleton")) then bongopos = col2[j].isectPosition.y +3.7721 if (col2[j].distance > 3.7721) then bongoWorld.model("joint70-skeleton").translate(0,-2,0) else bongoWorld.model("joint70-skeleton").worldposition.y = bongopos end if end if end repeat end if end The alignToTerrain function is called in ...

1. ## Character gravity

Hi, could someone plaese tell me where i am going wrong here.....I am trying to
create some simple gravity effects...so that when the character walks off a
surface, he falls to the ground...but my attempts so far have produced strange
results:

on alignToTerrain
locationVector2 = bongoWorld.model("joint70-skeleton").worldPosition
locationVector2.y = bongoWorld.model("joint70-skeleton").worldposition.y + 5
directionVector2 = vector(0,-1,0)
col2 =
bongoWorld.modelsUnderRay(locationVector2,directio nVector2,2,#detailed)
if col2.count>0 then
repeat with j=1 to col2.count
if (col2[j].model <> bongoWorld.model("joint70-skeleton")) then
bongopos = col2[j].isectPosition.y +3.7721
if (col2[j].distance > 3.7721) then
bongoWorld.model("joint70-skeleton").translate(0,-2,0)
else
bongoWorld.model("joint70-skeleton").worldposition.y = bongopos
end if
end if
end repeat
end if
end
The alignToTerrain function is called in an enterframe loop.

thanks very much for your time

Richard

Richard.B Guest

2. ## Re: Character gravity

i think you should calculate a little tolerance into your script.

every frame you fall you go down about -2 worldunits and you are something
about 3.7721 over the ground. at the moment i have no idea what happens in your
scene or what you ment by strange results. at the moment i am thinking about
the size of the models. i use cinema4D for modelscreations and the standard box
if i use a downvector of (0,-2,0) it is quite slow, if your models are in
sizes of 5 or 2 wordunits you are pretty fast.

and your ray can detect two models at the same time and i would try to use
only the first one in the col2 list.
"col2 =
bongoWorld.modelsUnderRay(locationVector2,directio nVector2,1,#detailed)"

and what should happen if you have stairs?

you need something for falling and something for going up.

alist = member("3dwelt").modelsUnderRay(pEgoposition, vector( 0,-1, 0), 1,
#detailed)

repeat with i in alist
a=(i.isectposition)
c=i.model
d=integer(i.distance)
e=i.isectNormal
end repeat

h?he=150
fallh?he=250
uph?he=140
b=vector(0,-50,0)
bb=vector(0,1,0)
f=h?he+a.y+up

if string(c) contains "*" then
backvec=pEgo.transform.position
if d<=fallh?he then
pEgo.translate (transvec)
pEgo.transform.position.y=f
else if d>fallh?he then
pEgo.translate (transvec+b)
else if d<uph?he then
pEgo.translate (transvec+bb)
end if
else if string(c) contains "#" then
pEgo.transform.position=backvec
end if

hondo3000 Guest

3. ## Re: Character gravity

use removeFromWorld() to remove your character model before casting the ray
into the ground, then use addToWorld() to add the character back in again.
Do this every frame. Otherwise if the ray hits the character and not the
ground you will miss collision data here and there, and the model could fall
through the floor. This will also speed up the raycast massively.

Also you don't have to fire the ray from the postion of the character. Just
set the ray origin pos to the x and z of your character world position and
use a y value at a constant height well above the highest point of your
terrain. That way the ray will never "miss". Given the known height of the
ray origin you can work out the world position of the intersection.

A trick mentioned on this site a lot is to add every model that you don't
want to cast the ray against to a group object, then remove the group, cast
the ray, add back the group. That way all the unwanted objects will be
ignored.

Also the latest version of Director has a raycast where you can specify a
list of models to check against. This is even better, but I still use 8.5 as
it hasn't been worth upgrading for years.

Cheers

Richard

Richard Guest

4. ## Re: Character gravity

and take a look at the livedocs of modelsunderray, for a description or more
options you can use with the ray.

and if you want to make some kind of "gravity like" falling, with a little bit
of physic behavior, you can take a look at this littel example.
[url]http://kisd.de/~lutz/crap/jump/[/url]

the code is not very long and you can make it work in 3D. just press on the
space bar and see how it works, its very simple and you can see all the code.

to richard:
if the ray is not send from the position of the character, it is not possible
to make more than one level where you can move, if there are more levels above
each other and you climb stairs and these things it will become difficult to
manage this with a fixed high. i think about going under an object.
if there is only one level it should work fine.

regards!

hondo3000 Guest

5. ## Re: Character gravity

> to richard:
> if the ray is not send from the position of the character, it is not
> possible
> to make more than one level where you can move, if there are more levels
> above
> each other and you climb stairs and these things it will become difficult
> to
> manage this with a fixed high. i think about going under an object.
> if there is only one level it should work fine.
Very true! Was assuming (maybe wrongly) a single terrain object.

Cheers

Richard

Richard Guest

6. ## Re: Character gravity

Hey...thanks for all the replies guys......i will take a look at them now....

to: hondo3000
you are absolutely right about sending the ray from the position of the
character, the enviroment that the character walks around in has multi-levels,
with stairs etc...

thanks again guys

Richard.B Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•