## Formula for a crate dropping into the water

Discuss all aspects of editing for ZDoom.
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

### Formula for a crate dropping into the water

Okay guys, now I need someone who is quite skilled in physics and maths. What I actually need is an enhanced version of this script:
Code: Select all
`//Dropping cratescript 69 (void){if (GameSkill () == 1)    {    floor_lowerbyvalue(309, 200, 288);    ceiling_lowerbyvalue(309, 200, 288);    }}`

As of now, this script gets activated as soon as the player enters a room. If that happens, a crate drops from the ceiling to the ground, directly into a nukage pool. As of now, the whole thing is too linear and obviously not very realistic. What I would like is to have an accelerated drop-movement, the crate drops to the ground, and as soon as it hits the water, there will be splashes and the crate will just waggle for 5-6 times (the longer the slower) after it dropped into the nukage - actually some kind of movement that's realistic.

I have ideas how to do this mathematically but I am missing the knowledge how to combine this with ACS. So if there is someone who is skilled enough and knows what I want to achieve, I'd be very happy

cheers,
Dan

Tormentor667

Joined: 16 Jul 2003
Location: Germany

### Re: Formula for a crate dropping into the water

Maybe you can work something out from this...
Spoiler:
The not-used values are based on wikipedia (gravitational constant, or approximate acceleration per second). I should probably have used proper (what's that in english; integration/derivation), which would probably have resulted in a lower acceleration per tick. I also didn't closely represent a meter. (I think I used 30.0 as a little more than half of the very common height 56.0)

The "terminal_velocity" constant is not actively used, because I prefer to precalculate my drag-factor on the side if I'm going to have one.

But the concept is there. Even if it was really frustrating to work in increments of 8.0 what I can calculate at zdoom fixed point precision. So you might need to tweak a bit, unless you know a sector-adjusting function that lets you work with the full precision of the engine.

You can experiment with values (calculate on the side, input as fixed point):
#define grav_accel -- acceleration per tick --
#define terminal_velocity -- not used, but good to have noted --
#define linear_drag -- grav_accel / terminal_velocity --

FDARI
Bronies eunt domus

Joined: 03 Nov 2009

### Re: Formula for a crate dropping into the water

I would just put a zero speed, add a loop and up the speed one by one and he just get out the loop when the floor/ceilling be where it should be ^^

ibm5155

Joined: 20 Jul 2011

### Re: Formula for a crate dropping into the water

@FDARI - Thanks for the script, this is the way I implemented it
Code: Select all
`//Dropping crate#define sec_crate 308 // sector containing 3d-floor#define sec_3d_crate 309 // sector defining 3d-floor (has line-special)#define grav_accel 1.85#define terminal_velocity 500.0#define linear_drag 0.0037script 69 (void){if (GameSkill () == 0)    {        delay(1);    }    else if (GameSkill () == 1)    {        int velocity = 0, v;        delay(90);            v = GetSectorFloorZ(sec_crate,0,0) - GetSectorFloorZ(sec_3d_crate,0,0);        while (v > 0)        {            velocity = velocity + grav_accel - FixedMul(linear_drag, velocity);                if (v > 8 * velocity) v = (velocity + 0.5)>>16;            else v = (v >>16)/8;                    printbold(s:"C = ",f:getsectorfloorz(sec_crate,0,0),s:" F = ",f:getsectorfloorz(sec_3d_crate,0,0),s:"\nLower: ",i:v);                        Floor_LowerInstant(sec_crate, 0, v);            Ceiling_LowerInstant(sec_crate, 0, v);            delay(1);                   v = GetSectorFloorZ(sec_crate,0,0) - GetSectorFloorZ(sec_3d_crate,0,0);        }        }}`

...but unfortunately nothing happens at all, the 3D crate doesn't drop even though the sector ids are correct and I didn't change a single value. Is there something else I forgot to take care of?

Tormentor667

Joined: 16 Jul 2003
Location: Germany

### Re: Formula for a crate dropping into the water

Is the printbold-statement happening? If it isn't, then then v is not greater than 0 (the bottom of the crate is already at or below the true line).
However... did I really do that? I seem to have mixed up sec_crate and sec_3d_crate semantically and "corrected" it in my local testmap by assigning the wrong numbers.

The best correction would probably to take every use of sec_crate/sec_3d_crate and swap them. I'd have to load my map to verify, but a quick correction looks like this
Code: Select all
`        int velocity = 0, v;        delay(90);            v = GetSectorFloorZ(sec_3d_crate,0,0) - GetSectorFloorZ(sec_crate,0,0);        while (v > 0)        {            velocity = velocity + grav_accel - FixedMul(linear_drag, velocity);                if (v > 8 * velocity) v = (velocity + 0.5)>>16;            else v = (v >>16)/8;                    printbold(s:"C = ",f:getsectorfloorz(sec_3d_crate,0,0),s:" F = ",f:getsectorfloorz(sec_crate,0,0),s:"\nLower: ",i:v);                        Floor_LowerInstant(sec_3d_crate, 0, v);            Ceiling_LowerInstant(sec_3d_crate, 0, v);            delay(1);                   v = GetSectorFloorZ(sec_3d_crate,0,0) - GetSectorFloorZ(sec_crate,0,0);        } `

FDARI
Bronies eunt domus

Joined: 03 Nov 2009

### Re: Formula for a crate dropping into the water

shouldn´t have a delay at the end of the while for it works? or the v = GetSectorFloorZ(sec_3d_crate,0,0) - GetSectorFloorZ(sec_crate,0,0); works there?
and getactor only works with enter scripts, I don´t know about this one so i don´t know if it´s that

ibm5155

Joined: 20 Jul 2011

### Re: Formula for a crate dropping into the water

@FDARI - The "Printbold" statement isn't happening, so that might mean something. Though what do you mean with "If it isn't, then then v is not greater than 0 (the bottom of the crate is already at or below the true line)."?

Do I need to define the "goal height" of the crate somewhere? I absolutely do not understand how things are working here that's why I ask

Tormentor667

Joined: 16 Jul 2003
Location: Germany

### Re: Formula for a crate dropping into the water

You may not even have realized that the reposted code contains a probable fix.
sec_3d_crate should be the sector that defines the 3d-floor (heights, flats and textures)
sec_crate should be the sector that puts the 3d-floor into the map

In my original code, the two were reversed. The original code used sec_3d_crate where it needed sec_crate, and used sec_crate where it needed sec_3d_crate. (I had not noticed during testing, because I had mistakenly assigned the value of sec_3d_crate to sec_crate, and vice versa.)

Spoiler: Understand?
Goal height in my script: The floor of the sector the crate drops into. If you want a goal height that is not the floor of the actual sector, you will need to change something.

FDARI
Bronies eunt domus

Joined: 03 Nov 2009

### Re: Formula for a crate dropping into the water

Okay, got it so far. It avtually works, though it doesn't seem to work correctly at the moment.

The crate drops too fast at the moment, it accelerates too fast. How can I decrease the acceleration and speed that the crate drops? Which variable is the right one to edit here?

Tormentor667

Joined: 16 Jul 2003
Location: Germany

### Re: Formula for a crate dropping into the water

#define grav_accel 1.85
- This is the acceleration, set it to a lower value. (Try 1.0, try 0.5; and write them like that, do NOT write "1", write "1.0".)
- 0.5 would probably result in 0 units drop at first, 1.0 will probably be a bit more even, and still slower than what you have right now.

#define linear_drag 0.0037
- This is the "drag"; it reduces acceleration when speed gets higher. ( new speed = old speed + acceleration - old speed * drag );
- Most likely you won't note much the effect of this unless you have things falling slowly or set it to a very high value
- Higher drag causes acceleration to drop off sooner, stabilizing the fall at a lower top speed.
- Drag around 1.0 will just about negate gravity. (Probably not a good idea with my script)
- Drag around 0.5 is high.
- You can always try 0.005 or 0.01

I found this kinda decent:

#define grav_accel 1.0
#define linear_drag 0.005

It's still quite fast, but any slower doesn't feel much like a gravity-driven drop.

FDARI
Bronies eunt domus

Joined: 03 Nov 2009

### Re: Formula for a crate dropping into the water

Thanks again for the explanation, that does the job

Tormentor667

Joined: 16 Jul 2003
Location: Germany