ATTENTION MODS: Could you please change the name to
"(GDK/DBpro + Physics) Easy jumping with dynamic objects (raycasts)"
Thank you
DarkBasic Pro version at bottom
Well I just got it working today
Ok well let's get to it, this is a pretty accurate way to make a dynamic object (your player for example if your not using character controllers) jump easily!
Object 1 is my dynamic object
coly is the variable for the raycasts
First of all you need to declare to objects position if you already haven't, must be inside your loop.
float x = dbObjectPositionX(1);
float y = dbObjectPositionY(1);
float z = dbObjectPositionZ(1);
Next, after that, inside your loop place this.
dbPhyRayCastAllShapes(x,y,z,0,-1,0);
float coly = dbPhyGetRayCastDistance();
Alright after that simply place this (This will make the spacebar the button for the player to press to make the object jump)
if (dbKeyState(57)) {
if ((coly > 10) && (coly < 25))
{
dbPhyAddRigidBodyForce(1,0,50,0,2);
}
}
You guys can edit the codes to your liking, some of the main things you will want to edit are:
and
dbPhyAddRigidBodyForce(1,0,50,0,2)
mainly the 50 in dbPhyAddRigidBodyForce because that chooses how high the player will jump, the coly's can also do that but it's simpler to just change the 50s.
This is a demo of it working, with this you can jump around, and move the sphere using the arrow keys.
#include "DarkGDK.h"
#include "DarkPhysics.h"
#include "DarkAI.h"
#include "DarkLights.h"
#include "ShaderData.h"
#pragma comment ( lib, "DarkPhysics.lib" )
#pragma comment ( lib, "DarkAI.lib" )
#pragma comment ( lib, "DarkLights.lib" )
#pragma comment ( lib, "ShaderData.lib" )
void DarkGDK ( void ) {
//Settings
dbSyncOn();
dbPhyStart();
dbPhySetGravity(0,-200,0);
dbSyncRate(60);
dbBackdropOn();
dbPositionCamera(0,0,-10,30);
//Player
dbMakeObjectSphere(1,40);
dbPositionObject(1,0,50,0);
dbColorObject(1, RGB(0,255,0));
//Other Objects
dbMakeObjectBox(2,200,1,200);
dbColorObject(2,RGB(126,235,21));
dbMakeObjectCube(3,80);
dbColorObject(3,RGB(255,0,0));
dbPositionObject(3,0,0,30);
dbMakeObjectSphere(4,20);
dbColorObject(4,RGB(255,0,0));
dbPositionObject(4,0,0,-50);
//Setting up physics
dbPhyMakeRigidBodyDynamicSphere(1);
dbPhyMakeRigidBodyStaticMesh(2);
dbPhyMakeRigidBodyStaticMesh(3);
dbPhyMakeRigidBodyStaticMesh(4);
//Camera
dbRotateCamera(0,0,0,0);
//Loop
while( LoopGDK ( ) ) {
//Declare player position as x,y,z
float x = dbObjectPositionX(1);
float y = dbObjectPositionY(1);
float z = dbObjectPositionZ(1);
//Raycast
dbPhyRayCastAllShapes(x,y,z,0,-1,0);
float coly = dbPhyGetRayCastDistance();
//Movement
if (dbKeyState(200) == 1) {
dbPhyAddRigidBodyForce(1,0,0,5,2);
}
if (dbKeyState(208) == 1) {
dbPhyAddRigidBodyForce(1,0,0,-5,2);
}
if (dbKeyState(205) == 1) {
dbPhyAddRigidBodyForce(1,5,0,0,2);
}
if (dbKeyState(203) == 1) {
dbPhyAddRigidBodyForce(1,-5,0,0,2);
}
//Jumping
if (dbKeyState(57)) {
if ((coly > 10) && (coly < 25))
{
dbPhyAddRigidBodyForce(1,0,50,0,2);
}
}
//Position camera to player
dbPositionCamera(0,x,y,z-100);
dbShowObject(2);
//Display x,y,z
dbInk(RGB(250,0,160),RGB(250,0,160));
dbText(0,0,dbStr((int)x));
dbText(0,10,dbStr((int)y));
dbText(0,20,dbStr((int)z));
dbText(0,30,dbStr((int)coly));
//Update Screen and Physics
dbPhyUpdate();
dbSync();
}
}
This is the Darkbasic Pro version of it (Thank HowDo for this):
//Settings
Sync On
Phy Start
Phy Set Gravity 0,-200,0
Sync Rate 60
Backdrop On
Position Camera 0,0,-10,30
//Player
Make Object Sphere 1,40
Position Object 1,0,50,0
Color Object 1, RGB(0,255,0)
//Other Objects
Make Object Box 2,200,1,200
Color Object 2,RGB(126,235,21)
Make Object Cube 3,80
Color Object 3,RGB(255,0,0)
Position Object 3,0,0,30
Make Object Sphere 4,20
Color Object 4,RGB(255,0,0)
Position Object 4,0,0,-50
//Setting up physics
Phy Make Rigid Body Dynamic Sphere 1
Phy Make Rigid Body Static Mesh 2
Phy Make Rigid Body Static Mesh 3
Phy Make Rigid Body Static Mesh 4
//Camera
Rotate Camera 0,0,0,0
//Loop
` while( LoopGDK ( ) ) {
do
//Declare player position as x,y,z
x as float
y as float
z as float
coly as float
x = Object Position X(1)
y = Object Position Y(1)
z = Object Position Z(1)
//Raycast
if Phy Ray Cast All Shapes (x,y,z,0,-1,0)=1
coly = Phy Get Ray Cast Distance()
endif
//Movement
if KeyState(200) = 1
Phy Add Rigid Body Force 1,0,0,5,2
endif
if KeyState(208) = 1
Phy Add Rigid Body Force 1,0,0,-5,2
endif
if KeyState(205) = 1
Phy Add Rigid Body Force 1,5,0,0,2
endif
if KeyState(203)= 1
Phy Add Rigid Body Force 1,-5,0,0,2
endif
//Jumping
if KeyState(57)
if coly > 10 and coly < 25
Phy Add Rigid Body Force 1,0,50,0,2
endif
endif
//Position camera to player
Position Camera 0,x,y,z-100
Show Object 2
//Display x,y,z
Ink RGB(250,0,160),RGB(250,0,160)
Text 0,0,Str$(x)
Text 0,10,Str$(y)
Text 0,20,Str$(z)
Text 0,30,Str$(coly)
//Update Screen and Physics
Phy Update
Sync
Thanks to (for helping me with my errors on the forum with this particular thing):
HowDo
Fatal Berseker
I want coke, not Pepsi!