Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Newton Physics Code Snippet

Author
Message
Physics coder
20
Years of Service
User Offline
Joined: 25th May 2004
Location: United States
Posted: 7th Aug 2004 13:43
Made this for fun, and hopefully a basic starting point for whoever wants to try Newton Physics but wants to set the position/rotation/velocity/whatever like normal objects.

Some functions that might be useful that are in the code snippet:

make_box(object number,x size,y size z size,mass,surface type, move flag)
makes a DBP object and a body with one command. IF the move flah is 1, the box can move and will if it collides with other objects and will be affected by gravity, else the object will remain still and only move with the next function.

set_object(object number,x position,y position,z position,x angle,y angle,z angle)
this function sets the object's and body's position and rotation.

set_velocity(object number, x velocity, y velocity, z velocity)
this function sets the linear velocity of the object.

set_omega(object number, x velocity, y velocity, z velocity)
this function sets the rotational velocity of the object.

add_velocity(object number, x velocity, y velocity, z velocity)
this functions increases the velocity of the object.

add_omega(object number, x velocity, y velocity, z velocity)
this functions increases the rotational velocity of the object.

set_relative_velocity(object number, x velocity, y velocity, z velocity)
this functions sets the velocity relative to the object, so a positive z velocity will make the object go forward no matter what direction it's facing.

set_relative_omega(object number, x velocity, y velocity, z velocity)
this functions sets the rotational velocity relative to the object, so a non-zero z velocity will make the object roll no matter what direction it's facing.

add_relative_velocity(object number, x velocity, y velocity, z velocity)
this functions increases the velocity relative to the object, so a positive z velocity will accelerate the object forward no matter what direction it's facing.

add_relative_omega(object number, x velocity, y velocity, z velocity)
this functions increases the rotational velocity relative to the object, so a positive y velocity will accelerate the object to the right no matter what direction it's facing.

Using these functions you don't have to worry about the bodies or what object is the visual representation of what body.



Controls arrow keys for turn left/right and forward/backward during car controls, during rocket controls arrow keys are same but up/down control the up/down acceleration, and WASD control the x and z axis rotational acceleration.

-----------------------------------
To delete the bug, delete the code.
Specs: Sony VAIO Laptop, Windows XP, P4 2.8Ghz, 512MB RAM, ATI Radeon 64MB video memory, DBP Upgrade 5.3.
walaber
21
Years of Service
User Offline
Joined: 22nd Oct 2003
Location: Los Angeles, CA
Posted: 7th Aug 2004 15:16
interesting command set. very nice use of the matrix system to make relative (local space) movement.

one note about the methods used in this system: directly setting the velocity / omega effectively gives the rigid body an infinite amount of energy, so collisions between it and other bodies will not be realistic- the other body will be sent flying, and the moving body will hardly be effected, regardless of mass settings.

these functions are actually intended for setting initial velocities and omegas (and testing purposes), not for realtime control.

for physics-sound control, look into setting forces and torques on the bodies with the NDB_BodySetGravity / NDB_BodySetTorque commands.

if you want a specific velocity, you can use algebra to determine the force necessary to result in the desired velocity. it's a little more work and math, but it'll make for a more realistic simulation.

Go Go Gadget DBPRO!

Athlon XP 2400+ || DDR-SDRAM 1GB || Nvidia GeForce 4 Ti4200 AGP 8x 128MB
Physics coder
20
Years of Service
User Offline
Joined: 25th May 2004
Location: United States
Posted: 8th Aug 2004 05:23
Hey walaber, thanks for trying the snippet.

Quote: "one note about the methods used in this system: directly setting the velocity / omega effectively gives the rigid body an infinite amount of energy,"


I don't exactly set the velocity, I increase it, so when you colide with a box with the same mass, both of the body will be affected, but the car will have an acceleration to go forward. It looks perfectly realistic to me, when you set the mass of the boxes higher, they are harder to move, until eventually, with enough mass, they are impossible to move. In the example I increase the velocity of the "car" (not set the velocity), and that is what a gravity would do.

Quote: "so collisions between it and other bodies will not be realistic- the other body will be sent flying, and the moving body will hardly be effected, regardless of mass settings."


If it's not realistic, could you make a snippet that shows the difference between increasing the velocity and setting a gravity?

-----------------------------------
To delete the bug, delete the code.
Specs: Sony VAIO Laptop, Windows XP, P4 2.8Ghz, 512MB RAM, ATI Radeon 64MB video memory, DBP Upgrade 5.3.

Login to post a reply

Server time is: 2024-11-23 19:46:23
Your offset time is: 2024-11-23 19:46:23