3D Physics with ZgeBullet

Use of external libraries (DLLs) from ZGE.

Moderator: Moderators

User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: 3D Physics with ZgeBullet

Post by Rado1 »

Hi Smags.

I like your effect; cool idea. Even if I did not study it in details at first look there's a bug, you should set the current world before creating any shape, so the init script should look like this:

Code: Select all

World = zbtCreateWorld();
zbtSetCurrentWorld(World);
zbtSetWorldGravity(0,-9.8*4, 0);
sphere = zbtCreateSphereShape(1.5);
...
You can also remove (or comment)

Code: Select all

// zbtSetCurrentWorld(World);
in OnUpdate expression. It is useless since you are using just one world.

Another quite big problem leading to bad performance is that you remove all Keys models and spawn them again on each render pass. You should just spawn them on initialization time (in OnLoaded section) and do not touch them during lifetime. Just removing the RemoveAllModels component from OnBeginRenderPass and moving the

Code: Select all

for (int i=0; i<spc1.SizeDim1; i++){
  keys.Position.x=spc1[i].x;
  ...
expression to the OnLoaded section makes the job. But please check correctness of these changes, because I do not understand your code in details...

Rado1.
Smags
Posts: 2
Joined: Fri Apr 14, 2017 7:22 pm

Re: 3D Physics with ZgeBullet

Post by Smags »

Thanks Rado -

It was the init code. I was convinced that the init code was fine because it did work occasionally. I never would have guessed that was the problem.

My Code:

Code: Select all

World = zbtCreateWorld();
sphere = zbtCreateSphereShape(1.5);[/quote]
zbtSetCurrentWorld(World);
zbtSetWorldGravity(0,-9.8*4, 0);
Fix:

Code: Select all

World = zbtCreateWorld();
zbtSetCurrentWorld(World);
zbtSetWorldGravity(0,-9.8*4, 0);
sphere = zbtCreateSphereShape(1.5)
I also removed World zbtCreateWorld() from update and moved the keys to onLoaded.
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: 3D Physics with ZgeBullet

Post by Rado1 »

Smags wrote:I also removed World zbtCreateWorld() from update and moved the keys to onLoaded.
You probably meant zbtSetCurrentWorld(World). I hope your code works fine now.
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

I've started toying with ZgeBullet in order to improve the collisions and the overall gameplay of Omeganaut, and it's pretty neat.

But since we're adding the collisions zones with numbers in the code, I'm wondering if there's a technique to display those zones in order to verify if they are correctly placed. Or is the only answer is to manually create and display Meshes with transformations equals to those numbers?

Thanks for your help
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Re: 3D Physics with ZgeBullet

Post by Rado1 »

Ats wrote: Thu Jan 24, 2019 4:12 pm But since we're adding the collisions zones with numbers in the code, I'm wondering if there's a technique to display those zones in order to verify if they are correctly placed. Or is the only answer is to manually create and display Meshes with transformations equals to those numbers?
Ats, you are right. There's no way to display collision zones "automatically" out-of-the-box. ZgeBullet provides you just computation of 3D physics, no rendering. And displaying meshes representing collision zones and setting their transformations according to the numbers from code is exactly the way how I would do it.
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

All right. Glad to see you are still here :wink:

Edit:
I'm having a hard time validating if the shapes and the meshes are the same in my game, so I made this shape/mesh viewer.
Thanks to that, I discovered that a Cone Shape with a height of 1 is in fact a Cone Mesh with a height of 0.5 !? Maybe that's supposed to be like that, I don't know.

What I would like to know is if collision shapes can be scaled using zbtSetShapeLocalScaling?

Press left/right to change shape/model and move the mouse around to see if everything's ok. You'll see my problem with the ArrowShape example (it's a squished cone).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" MouseVisible="255" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary Comment="Bullet 3D physics" ModuleName="ZgeBullet" CallingConvention="1" BeforeInitExp="if(ANDROID) this.ModuleName = &quot;./libZgeBullet.so&quot;;">
      <Source>
<![CDATA[/*
  ZgeBullet Library, a wrapper for the Bullet Physics Library.
  http://bulletphysics.org

  Project home
  https://github.com/Rado-1/ZgeBullet

  Download Windows DLL and Android shared library from
  https://github.com/Rado-1/ZgeBullet/releases

  Copyright (c) 2012-2015 Radovan Cervenka

  Version: 2.1 (2015-06-27)
*/


// Constants

// Triangle mesh types
const int ZBT_TRIANGLE_CONVEX_HULL_MESH = 1;
const int ZBT_TRIANGLE_CONCAVE_STATIC_MESH = 2;
const int ZBT_TRIANGLE_CONCAVE_DEFORMABLE_MESH = 3;

// Activation states
const int ZBT_ACTIVE_TAG = 1;
const int ZBT_ISLAND_SLEEPING = 2;
const int ZBT_WANTS_DEACTIVATION = 3;
const int ZBT_DISABLE_DEACTIVATION = 4;
const int ZBT_DISABLE_SIMULATION = 5;

// Default values of constraint limits
const float ZBT_DEFAULT_HINGE_SOFTNESS = 0.9;
const float ZBT_DEFAULT_HINGE_BIAS_FACTOR = 0.3;
const float ZBT_DEFAULT_HINGE_RELAXATION_FACTOR = 1.0;
const float ZBT_DEFAULT_CONE_TWIST_SOFTNESS = 1.0;
const float ZBT_DEFAULT_CONE_TWIST_BIAS_FACTOR = 0.3;
const float ZBT_DEFAULT_CONE_TWIST_RELAXATION_FACTOR = 1.0;

// Vehicle tunning defaults
const float ZBT_DEFAULT_VEHICLE_SUSP_STIFFNESS = 5.88;
const float ZBT_DEFAULT_VEHICLE_SUSP_COMPRESSION = 0.83;
const float ZBT_DEFAULT_VEHICLE_SUSP_DAMPING = 0.88;
const float ZBT_DEFAULT_VEHICLE_SUSP_MAX_SUSP_TRAVEL_CM = 500.0;
const float ZBT_DEFAULT_VEHICLE_SUSP_FORCE = 6000.0;
const float ZBT_DEFAULT_VEHICLE_FRICTION_SLIP = 10.5;

// Axes
const int ZBT_AXIS_X_LINEAR = 0;
const int ZBT_AXIS_Y_LINEAR = 1;
const int ZBT_AXIS_Z_LINEAR = 2;
const int ZBT_AXIS_X_ANGULAR = 3;
const int ZBT_AXIS_Y_ANGULAR = 4;
const int ZBT_AXIS_Z_ANGULAR = 5;

// Collision flags
const int ZBT_CF_STATIC_OBJECT= 1;
const int ZBT_CF_KINEMATIC_OBJECT= 2;
const int ZBT_CF_NO_CONTACT_RESPONSE = 4;
const int ZBT_CF_CUSTOM_MATERIAL_CALLBACK = 8;
const int ZBT_CF_CHARACTER_OBJECT = 16;
const int ZBT_CF_DISABLE_SPU_COLLISION_PROCESSING = 64;


// Functions


// World

xptr zbtCreateWorld() {}

void zbtDestroyWorld(xptr world) {}

void zbtSetCurrentWorld(xptr world) {}

void zbtSetWorldGravity(float x, float y, float z) {}

void zbtStepSimulation(float timeStep, int maxSubSteps, float fixedTimeStep) {}


// Collision shapes
xptr zbtCreateStaticPlaneShape(
	float normalX, float normalY, float normalZ, float planeConstant) {}

xptr zbtCreateBoxShape(float x, float y, float z) {}

xptr zbtCreateSphereShape(float radius) {}

xptr zbtCreateScalableSphereShape(float radius) {}

xptr zbtCreateConeShape(float radius, float height) {}

xptr zbtCreateCylinderShape(float radius, float height) {}

xptr zbtCreateCapsuleShape(float radius, float height) {}

xptr zbtCreateCompoundShape() {}

xptr zbtAddChildShape(xptr compoundShape, xptr childShape,
	float x, float y, float z, float rx, float ry, float rz) {}

xptr zbtRemoveChildShape(xptr compoundShape, xptr childShape) {}

xptr zbtCreateHeightfieldTerrainShape(xptr heightfieldData,
	int width, int length, float minHeight, float maxHeight, int upAxis,
	int bFlipQuadEdges, int bDiamondSubdivision) {}

xptr zbtCreateConvexHullShape(xptr points, int numPoints) {}

xptr zbtCreateMultiSphereShape(xptr positions, xptr radii, int numSpheres) {}

xptr zbtCreateTriangleMeshShape(xptr triangles, int numTriangles, int meshType) {}

void zbtUpdateDeformableTriangleMesh(xptr triangleMeshShape) {}

void zbtSetShapeLocalScaling(xptr shape, float x, float y, float z) {}

void zbtSetShapeMargin(xptr shape, float margin) {}

void zbtDeleteShape(xptr shape) {}

void zbtDeleteAllShapes() {}


// Rigid bodies

xptr zbtCreateRigidBodyXYZ(float mass, xptr shape,
	float x, float y, float z, float rx, float ry, float rz) {}

xptr zbtCreateRigidBody(float mass, xptr shape, xptr position, xptr rotation) {}

void zbtDeleteRigidBody(xptr rigidBody) {}

void zbtSetMass(xptr rigidBody, float mass) {}

void zbtSetDamping(xptr rigidBody, float linearDamping, float angularDamping) {}

void zbtSetLinearFactor(xptr rigidBody, float x, float y, float z) {}

void zbtSetAngularFactor(xptr rigidBody, float x, float y, float z) {}

void zbtSetGravity(xptr rigidBody, float x, float y, float z) {}

void zbtSetLinearVelocity(xptr rigidBody, float x, float y, float z) {}

void zbtGetLinearVelocity(xptr rigidBody,
	ref float outX, ref float outY, ref float outZ) {}

void zbtSetAngularVelocity(xptr rigidBody, float x, float y, float z) {}

void zbtGetAngularVelocity(xptr rigidBody,
	ref float outX, ref float outY, ref float outZ) {}

void zbtApplyCentralImpulse(xptr rigidBody, float x, float y, float z) {}

void zbtApplyTorqueImpulse(xptr rigidBody, float x, float y, float z) {}

void zbtApplyImpulse(xptr rigidBody, float x, float y, float z,
	float relX, float relY, float relZ) {}

void zbtSetSleepingThresholds(xptr rigidBody, float linear, float angular) {}


// Constraints and limits

int zbtAreConnected(xptr rigidBodyA, xptr rigidBodyB) {}

xptr zbtAddFixedConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float rotAx, float rotAy, float rotAz,
	float rotBx, float rotBy, float rotBz, int bDisableCollision) {}

xptr zbtAddPoint2PointConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ) {}

xptr zbtAddPoint2PointConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz, int bDisableCollision) {}

xptr zbtAddHingeConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ,
	float axisX, float axisY, float axisZ) {}

xptr zbtAddHingeConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float axisAx, float axisAy, float axisAz,
	float axisBx, float axisBy, float axisBz, int bDisableCollision) {}

void zbtSetHingeLimits(xptr hinge, float low, float high,
	float softness, float biasFactor, float relaxationFactor) {}

void zbtEnableHingeAngularMotor(xptr hinge, int bEnableMotor,
	float targetVelocity, float maxMotorImpulse) {}

xptr zbtAddConeTwistConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ,
	float rotX, float rotY, float rotZ) {}

xptr zbtAddConeTwistConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float rotAx, float rotAy, float rotAz,
	float rotBx, float rotBy, float rotBz, int bDisableCollision) {}

void zbtSetConeTwistLimits(xptr twist, float swingSpanA, float swingSpanB,
	float twistSpan, float damping, float softness, float biasFactor,
	float relaxationFactor) {}

void zbtEnableConeTwistMotor(xptr twist,
	int bEnableMotor, float maxMotorImpulse,
	float targetX, float targetY, float targetZ) {}

xptr zbtAddSliderConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ,
	float rotX, float rotY, float rotZ,
	int bUseLinearReferenceWorldFrame) {}

xptr zbtAddSliderConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float rotAx, float rotAy, float rotAz,
	float rotBx, float rotBy, float rotBz,
	int bUseLinearReferenceFrameA, int bDisableCollision) {}

void zbtSetSliderLimits(xptr slider,
	float linLower, float linUpper, float angLower, float angUpper) {}

void zbtSetSliderSoftness(xptr slider,
	float dirLin, float dirAng, float limLin, float limAng,
	float orthoLin, float orthoAng) {}

void zbtSetSliderRestitution(xptr slider,
	float dirLin, float dirAng, float limLin, float limAng,
	float orthoLin, float orthoAng) {}

void zbtSetSliderDamping(xptr slider,
	float dirLin, float dirAng, float limLin, float limAng,
	float orthoLin, float orthoAng) {}

void zbtEnableSliderLinearMotor(xptr slider, int bEnableMotor,
	float targetVelocity, float maxForce) {}

void zbtEnableSliderAngularMotor(xptr slider, int bEnableMotor,
	float targetVelocity, float maxForce) {}

xptr zbtAddGearConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float axisAx, float axisAy, float axisAz,
	float axisBx, float axisBy, float axisBz,
	float ratio) {}

void zbtSetGearConstraint(xptr gear,
	float axisAx, float axisAy, float axisAz,
	float axisBx, float axisBy, float axisBz,
	float ratio) {}

xptr zbtAddGeneric6DofConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ,
	float rotX, float rotY, float rotZ,
	int bUseLinearReferenceWorldFrame) {}

xptr zbtAddGeneric6DofConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float rotAx, float rotAy, float rotAz,
	float rotBx, float rotBy, float rotBz,
	int bUseLinearReferenceFrameA, int bDisableCollision) {}

void zbtSetGeneric6DofLimits(xptr dof, int axis, float lower, float upper) {}

void zbtSetGeneric6DofLinearLimits(xptr dof,
	float lowerX, float lowerY, float lowerZ,
	float upperX, float upperY, float upperZ) {}

void zbtSetGeneric6DofAngularLimits(xptr dof,
	float lowerX, float lowerY, float lowerZ,
	float upperX, float upperY, float upperZ) {}

xptr zbtAddGeneric6DofSpringConstraint1(xptr rigidBody,
	float pivotX, float pivotY, float pivotZ,
	float rotX, float rotY, float rotZ,
	int bUseLinearReferenceWorldFrame) {}

xptr zbtAddGeneric6DofSpringConstraint(xptr rigidBodyA, xptr rigidBodyB,
	float pivotAx, float pivotAy, float pivotAz,
	float pivotBx, float pivotBy, float pivotBz,
	float rotAx, float rotAy, float rotAz,
	float rotBx, float rotBy, float rotBz,
	int bUseLinearReferenceFrameA, int bDisableCollision) {}

void zbtSetGeneric6DofSpring(xptr spring,
	int axis, int bEnableSpring, float stiffness, float damping,
	float equilibriumPoint) {}

void zbtSetEnabled(xptr constraint, int bEnabled) {}

void zbtDeleteConstraint(xptr constraint) {}


// Raycast vehicle

void zbtSetVehicleTunning(float suspStiffness, float suspCompression,
	float suspDamping, float maxSuspTravelCm, float maxSuspForce,
	float frictionSlip) {}

xptr zbtCreateRaycastVehicle(xptr carChassis,
	int rightAxis, int upAxis, int forwardAxis) {}

int zbtAddWheel(xptr vehicle,
	float connectionPointX, float connectionPointY, float connectionPointZ,
	float directionX, float directionY, float directionZ,
	float wheelAxleX, float wheelAxleY, float wheelAxleZ,
	float wheelRadius, float suspRestLength, int bIsFrontWheel) {}

void zbtSetWheelIsFront(xptr vehicle, int wheelId, int bIsFront) {}

void zbtSetWheelRadius(xptr vehicle, int wheelId, float radius) {}

void zbtSetWheelRollInfluence(xptr vehicle, int wheelId, float rollInfluence) {}

void zbtSetWheelFrictionSlip(xptr vehicle, int wheelId, float frictionSlip) {}

void zbtSetWheelSuspRestLength(xptr vehicle, int wheelId, float suspRestLength) {}

void zbtSetWheelMaxSuspTravel(xptr vehicle, int wheelId, float maxSuspTravel) {}

void zbtSetWheelSuspStiffness(xptr vehicle, int wheelId, float suspStiffness) {}

void zbtSetWheelDampingCompression(xptr vehicle, int wheelId,
	float dampingCompression) {}

void zbtSetWheelDampingRelaxation(xptr vehicle, int wheelId,
	float dampingRelaxation) {}

void zbtSetWheelSteering(xptr vehicle, int wheelId, float steering) {}

void zbtSetWheelEngineForce(xptr vehicle, int wheelId, float force) {}

void zbtSetWheelBrake(xptr vehicle, int wheelId, float brake) {}

void zbtResetVehicleSusp(xptr vehicle) {}

float zbtGetVehicleCurrentSpeed(xptr vehicle) {}

void zbtGetWheelPositionXYZ(xptr vehicle, int wheelId,
	ref float outX, ref float outY, ref float outZ) {}

void zbtGetWheelPosition(xptr vehicle, int wheelId, xptr outPosition) {}

void zbtGetWheelRotationXYZ(xptr vehicle, int wheelId,
	ref float outRx, ref float outRy, ref float outRz) {}

void zbtGetWheelRotation(xptr vehicle, int wheelId, xptr outRotation) {}

void zbtGetWheelPosRotXYZ(xptr vehicle, int wheelId,
	ref float outX, ref float outY, ref float outZ,
	ref float outRx, ref float outRy, ref float outRz) {}

void zbtGetWheelPosRot(xptr vehicle, int wheelId,
	xptr outPosition, xptr outRotation) {}

void zbtDeleteRaycastVehicle(xptr vehicle) {}


// Ghost object

xptr zbtCreateGhostObject(xptr shape,
	float x, float y, float z, float rx, float ry, float rz) {}

void zbtDeleteGhostObject(xptr ghostObject) {}

int zbtGetNumOverlappingObjects(xptr ghostObject) {}

xptr zbtGetOverlappingObject(xptr ghostObject, int index) {}


// Kinematic character controller

xptr zbtCreateKinematicCharacterController(
	xptr ghostObject, float stepHeight) {}

void zbtDeleteKinematicCharacterController(xptr controller) {}

void zbtSetCharacterUpAxis(xptr controller, int axis) {}

void zbtSetCharacterWalkDirection(xptr controller,
	float x, float y, float z) {}

void zbtSetCharacterVelocityForTimeInterval(xptr controller,
	float x, float y, float z, float timeInterval) {}

void zbtCharacterWarp(xptr controller, float x, float y, float z) {}

void zbtSetCharacterFallSpeed(xptr controller, float fallSpeed) {}

void zbtSetCharacterJumpSpeed(xptr controller, float jumpSpeed) {}

void zbtSetCharacterMaxJumpHeight(xptr controller, float maxJumpHeight) {}

int zbtCharacterCanJump(xptr controller) {}

void zbtCharacterJump(xptr controller) {}

void zbtSetCharacterGravity(xptr controller, float gravity) {}

void zbtSetCharacterMaxSlope(xptr controller, float slope) {}

void zbtSetCharacterUseGhostSweepTest(xptr controller,
	int bUseGhostObjectSweepTest) {}

int zbtCharacterOnGround(xptr controller) {}

void zbtCharacterReset(xptr controller) {}

void zbtSetCharacterUpInterpolate(xptr controller, int bInterpolate) {}


// Collision objects (in general)

void zbtSetFriction(xptr obj, float friction) {}

void zbtSetRestitution(xptr obj, float restitution) {}

void zbtSetHitFraction(xptr obj, float hitFraction) {}

void zbtGetPositionXYZ(xptr obj,
	ref float outX, ref float outY, ref float outZ) {}

void zbtGetPosition(xptr obj, xptr outPosition) {}

void zbtSetPositionXYZ(xptr obj, float x, float y, float z) {}

void zbtSetPosition(xptr obj, xptr position) {}

void zbtGetRotationXYZ(xptr obj,
	ref float outRx, ref float outRy, ref float outRz) {}

void zbtGetRotation(xptr obj, xptr outRotation) {}

void zbtSetRotationXYZ(xptr obj, float rx, float ry, float rz) {}

void zbtSetRotation(xptr obj, xptr rotation) {}

void zbtGetPosRotXYZ(xptr obj,
	ref float outX, ref float outY, ref float outZ,
	ref float outRx, ref float outRy, ref float outRz) {}

void zbtGetPosRot(xptr obj, xptr outPosition, xptr outRotation) {}

void zbtSetPosRotXYZ(xptr obj,
	float x, float y, float z,
	float rx, float ry, float rz) {}

void zbtSetPosRot(xptr obj, xptr position, xptr rotation) {}

void zbtSetCollisionFlags(xptr obj, int flags) {}

int zbtIsActive(xptr obj) {}

void zbtActivate(xptr obj, int bForceActivation) {}

void zbtSetActivationState(xptr obj, int newState) {}

void zbtForceActivationState(xptr obj, int newState) {}

void zbtSetDeactivationTime(xptr obj, float time) {}

void zbtSetUserIndex(xptr obj, int index) {}

int zbtGetUserIndex(xptr obj) {}

void zbtSetUserModel(xptr obj, model userModel) {}

model zbtGetUserModel(xptr obj) {}


// Collision detection

void zbtSetIgnoreCollisionCheck(xptr objA, xptr objB,
	int bIgnoreCollisionCheck) {}

int zbtStartCollisionDetection() {}

int zbtGetNextContact(xptr outObjA, xptr outObjB,
	xptr outPosA, xptr outPosB, xptr outNormal) {}

void zbtGetCollidedObjects(int contactIndex, xptr outObjA, xptr outObjB) {}

int zbtIsColliding(xptr obj) {}

int zbtGetNumberOfCollisions(xptr obj) {}

int zbtIsCollidedWith(xptr objA, xptr objB) {}


// Raycasting

xptr zbtRayTest(float fromX, float fromY, float fromZ, float toX, float toY, float toZ) {}

void zbtGetRayTestHitPointXYZ(ref float outX, ref float outY, ref float outZ) {}

void zbtGetRayTestHitPoint(xptr outPosition) {}

void zbtGetRayTestHitNormalXYZ(ref float outX, ref float outY, ref float outZ) {}

void zbtGetRayTestHitNormal(xptr outNormal) {}]]>
      </Source>
    </ZExternalLibrary>
    <ZLibrary>
      <Source>
<![CDATA[const int
  CUBE=0,
  SPHERE=1,
  CONE=2,
  CYLINDER=3,
  CAPSULE=4,
  ARROW=5,
  STONE=6,
  PLANE=7,
  IDMAX=7;

xptr
  World,
  CubeShape,SphereShape,ConeShape,CylinderShape,CapsuleShape,
  ArrowShape,StoneShape,
  PlaneShape;]]>
      </Source>
    </ZLibrary>
    <ZExpression>
      <Expression>
<![CDATA[// Init physical world

World = zbtCreateWorld();
zbtSetCurrentWorld(World);
zbtSetWorldGravity(0, 0, 0);


// Basic collision shapes

SphereShape = zbtCreateSphereShape(1);
CubeShape = zbtCreateBoxShape(1,1,1);
ConeShape = zbtCreateConeShape(1,1);
CapsuleShape = zbtCreateCapsuleShape(1,1);
CylinderShape = zbtCreateCylinderShape(1,1);

// Squished shapes

ArrowShape = zbtCreateConeShape(1, 1);
zbtSetShapeLocalScaling(ArrowShape, 1.2, 1.5, 0.4);

StoneShape = zbtCreateScalableSphereShape(1.0);
zbtSetShapeLocalScaling(StoneShape, 1.4, 0.4, 1.4);

// Compound shapes

PlaneShape = zbtCreateCompoundShape();
zbtAddChildShape(PlaneShape, zbtCreateBoxShape(0.5, 0.2, 2.6), 0,0,-0.8, 0,0,0); // Hull
zbtAddChildShape(PlaneShape, zbtCreateBoxShape(0.9, 0.1, 0.3), 1.4,-0.1,1.05, -0.1,0.6,-0.53); // Wing right
zbtAddChildShape(PlaneShape, zbtCreateBoxShape(0.9, 0.1, 0.3), -1.4,-0.1,1.05, 0.1,0.6,0.03); // Wing left

createModel(TestModel);]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnUpdate>
    <ZExpression Comment="Update ZgeBullet">
      <Expression>
<![CDATA[Timer += App.DeltaTime;
zbtStepSimulation(App.DeltaTime, 0, 0);]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <OnClose>
    <ZExpression Comment="Destroy physics">
      <Expression>
<![CDATA[// ZgeBullet Destroy physical world
zbtDestroyWorld(World);
zbtDeleteAllShapes();]]>
      </Expression>
    </ZExpression>
  </OnClose>
  <Content>
    <Group Comment="Meshes">
      <Children>
        <Mesh Name="CubeMesh">
          <Producers>
            <MeshBox/>
          </Producers>
        </Mesh>
        <Mesh Name="SphereMesh">
          <Producers>
            <MeshBox Scale="1 0.5 1" XCount="18" YCount="12" Grid2DOnly="255"/>
            <MeshExpression AutoNormals="0">
              <Expression>
<![CDATA[//

        float E, A, K, X, Y, Z;

        // Convert range to radians

        E = v.Y*PI; // Elevation
        A = v.X*PI; // Azimuth

        // Convert spherical coordinates into cartesian

        K = cos(E);

        X = sin(A)*K;
        Y = sin(E);
        Z = cos(A)*K;

        // Assign coordinates

        v.X = X;
        v.Y = Y;
        v.Z = Z;

        n.X = X;
        n.Y = Y;
        n.Z = Z;]]>
              </Expression>
            </MeshExpression>
          </Producers>
        </Mesh>
        <Mesh Name="ConeMesh">
          <Producers>
            <MeshSphere ZSamples="3" RadialSamples="18"/>
            <MeshTransform Rotation="0.25 0 0"/>
            <MeshExpression Expression="v.Y = v.Y &lt; 0.25 ? -0.5 : 0.5;"/>
          </Producers>
        </Mesh>
        <Mesh Name="CylinderMesh">
          <Producers>
            <MeshBox Scale="1 0.5 1" XCount="18" YCount="2" Grid2DOnly="255"/>
            <MeshExpression AutoNormals="0">
              <Expression>
<![CDATA[float k;
if(abs(v.Y) < 0.5){
  k = v.X * PI;
  v.X = sin(k);
  v.Z = cos(k);
} else {
  v.X = 0;
  v.Z = 0;
}

v.Y = v.Y < 0 ? -1 : 1;

n.X = v.X;
n.Y = v.Y;
n.Z = v.Z;]]>
              </Expression>
            </MeshExpression>
          </Producers>
        </Mesh>
        <Mesh Name="CapsuleMesh">
          <Producers>
            <MeshBox Scale="0.5 0.5 1" XCount="18" Grid2DOnly="255"/>
            <MeshExpression AutoNormals="0">
              <Expression>
<![CDATA[float RX, SX, CX;

RX = v.X*PI*2;

SX = sin(RX); CX = cos(RX);

v.X = SX;
v.Z = CX;;

n.X = v.X;
n.Y = 0;
n.Z = v.Z;]]>
              </Expression>
            </MeshExpression>
            <MeshBox Comment="bottom cap" Scale="0.5 0.5 1" XCount="18" YCount="6" Grid2DOnly="255"/>
            <MeshExpression AutoNormals="0">
              <Expression>
<![CDATA[float RX, SX, CX,
      RY, SY, CY;

RX = v.X*PI*2;
RY = v.Y*PI/2-PI/4;

SX = sin(RX); CX = cos(RX);
SY = sin(RY); CY = cos(RY);

v.X = SX*CY;///2;
v.Y = SY;///2;
v.Z = CX*CY;///2;

n.X = v.X;
n.Y = v.Y;
n.Z = v.Z;

v.Y -= 0.5;]]>
              </Expression>
            </MeshExpression>
            <MeshCombine/>
            <MeshBox Comment="top cap" Scale="0.5 0.5 1" XCount="18" YCount="6" Grid2DOnly="255"/>
            <MeshExpression AutoNormals="0">
              <Expression>
<![CDATA[float RX, SX, CX,
      RY, SY, CY;

RX = v.X*PI*2;
RY = v.Y*PI/2+PI/4;

SX = sin(RX); CX = cos(RX);
SY = sin(RY); CY = cos(RY);

v.X = SX*CY;///2;
v.Y = SY;///2;
v.Z = CX*CY;///2;

n.X = v.X;
n.Y = v.Y;
n.Z = v.Z;

v.Y += 0.5;]]>
              </Expression>
            </MeshExpression>
            <MeshCombine/>
          </Producers>
        </Mesh>
        <Mesh Name="PlaneMesh">
          <Producers>
            <MeshBox Scale="0.5 0.2 2.6"/>
            <MeshTransform Position="0 0 -0.8"/>
            <MeshBox Comment="right" Scale="0.9 0.1 0.3"/>
            <MeshTransform Position="1.4 -0.1 1.05" Rotation="-0.1 0.6 -0.53"/>
            <MeshCombine/>
            <MeshBox Comment="left" Scale="0.9 0.1 0.3"/>
            <MeshTransform Position="-1.4 -0.1 1.05" Rotation="0.1 0.6 0.03"/>
            <MeshCombine/>
          </Producers>
        </Mesh>
      </Children>
    </Group>
    <Model Name="TestModel">
      <Definitions>
        <Variable Name="ModelID" Type="1"/>
        <Variable Name="ModelBody" Type="9"/>
        <ZLibrary>
          <Source>
<![CDATA[void CreateObjectBody()
{
  if (ModelBody != null) zbtDeleteRigidBody(ModelBody);

  TransformCollision.Scale = vector3(1,1,1);
  TransformCollision.Translate = vector3(0,0,0);
  TransformCollision.Rotate = vector3(0,0,0);

  switch (ModelID)
  {
    case CUBE:
      CollisionMesh.Mesh = CubeMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, CubeShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case SPHERE:
      CollisionMesh.Mesh = SphereMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, SphereShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, 0, 0, 0);
      break;

    case CONE:
      CollisionMesh.Mesh = ConeMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, ConeShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case CYLINDER:
      CollisionMesh.Mesh = CylinderMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, CylinderShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case CAPSULE:
      CollisionMesh.Mesh = CapsuleMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, CapsuleShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case ARROW:
      CollisionMesh.Mesh = ConeMesh;
      TransformCollision.Scale = vector3(1.2,1.5,0.4);
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, ArrowShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case STONE:
      CollisionMesh.Mesh = SphereMesh;
      TransformCollision.Scale = vector3(1.4, 0.4, 1.4);
      /*
      xptr sh = zbtCreateScalableSphereShape(1.0);
      zbtSetShapeLocalScaling(sh, 2, 0.4, 2);
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(1, sh, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      */
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(1, StoneShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;

    case PLANE:
      CollisionMesh.Mesh = PlaneMesh;
      CurrentModel.ModelBody = zbtCreateRigidBodyXYZ(0, PlaneShape, CurrentModel.Position.X, CurrentModel.Position.Y, CurrentModel.Position.Z, CurrentModel.Rotation.X, CurrentModel.Rotation.Y, CurrentModel.Rotation.Z);
      break;
  }
}]]>
          </Source>
        </ZLibrary>
      </Definitions>
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[Timer = 0;
CurrentModel.RotationVelocity = rnd()*0.1;
CreateObjectBody();]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[CurrentModel.Position.X += cos(Timer)/80;
CurrentModel.Position.Y += sin(Timer)/80;

zbtSetPosRot(ModelBody, CurrentModel.Position, CurrentModel.Rotation);

float r,x,y,z;

r = tan(App.FOV/360*PI);
//r = 0.4142; // If you're using a constant FOV, you can swap out this calculation with the resulting value.

z = App.CameraPosition.Z - CurrentModel.Position.Z;
x = z * App.MousePosition.X * r * App.ViewportWidth/App.ViewportHeight; // If you're using a constant aspectRatio, you can swap out "App.ViewportWidth/App.ViewportHeight" with a specific value.
y = z * App.MousePosition.Y * r;


if (zbtRayTest(x, y, 100, x, y, -100) != null)
  BoxColor.Color = vector3(1,0,0);
else
  BoxColor.Color = vector3(1,1,1);]]>
          </Expression>
        </ZExpression>
        <KeyPress Comment="LEFT" Keys="&lt;" RepeatDelay="0.1">
          <OnPressed>
            <ZExpression>
              <Expression>
<![CDATA[if (--ModelID<0) ModelID = IDMAX;
CreateObjectBody();]]>
              </Expression>
            </ZExpression>
          </OnPressed>
        </KeyPress>
        <KeyPress Comment="RIGHT" Keys="&gt;" RepeatDelay="0.1">
          <OnPressed>
            <ZExpression>
              <Expression>
<![CDATA[if (++ModelID>IDMAX) ModelID = 0;
CreateObjectBody();]]>
              </Expression>
            </ZExpression>
          </OnPressed>
        </KeyPress>
      </OnUpdate>
      <OnRender>
        <RenderTransformGroup Name="TransformCollision">
          <Children>
            <UseMaterial Material="ObjectMaterial"/>
            <RenderSetColor Name="BoxColor" Color="1 1 1 1"/>
            <RenderMesh Name="CollisionMesh" Mesh="CubeMesh"/>
          </Children>
        </RenderTransformGroup>
      </OnRender>
      <OnRemove>
        <ZExpression Comment="Delete rigid body and shape" Expression="zbtDeleteRigidBody(ModelBody);"/>
      </OnRemove>
    </Model>
    <Material Name="ObjectMaterial" WireframeWidth="0" SpecularColor="0 0 0 1" EmissionColor="0 0 0 1"/>
    <Variable Name="Timer"/>
  </Content>
</ZApplication>
Thanks for your help !

EDIT:
I've edited my example by adding a skipping stone after the arrow shape. Collisions works on the stone but not on the arrow, so it seems that we can only use zbtSetShapeLocalScaling on zbtCreateScalableSphereShape. Hence the name?

Anyway, I'm going to avoid pixel perfect collision shapes in Omeganaut, because it is nearly impossible to hit them and it's not funny to play. I'll stay with simple boxes and spheres!

BTW, is it better to use boxes or scalable spheres in term of speed performance?
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

The functions zbtSetCollisionFilterGroupAndMask and zbtRayTestFiltered are missing in the ZExternalLibrary from your examples. Yet they are in the source code and the documentation. And I really need to know who's touching who otherwise enemies destroy themselves while firing their own lasers...

So I added them to the ZExternalLibrary but it crashes when I call zbtSetCollisionFilterGroupAndMask:

Code: Select all

GroundShape = zbtCreateBoxShape(500, 1, 500);
zbtSetCollisionFilterGroupAndMask(GroundShape, 0, 1);
Gives:

Code: Select all

Access violation at adress 521FB860 in module ZgeBullet.dll. Write of adress 01F6E50D.
Runtime Error 216 at 521FB860
I'm using the latest ZgeBullet.dll (2.4)

Also, I'm not sure I get the difference between group and mask in those functions. Rado1, can you provide an example with those functions?

Edit:
So I managed to make zbtSetCollisionFilterGroupAndMask to work. The first object param have to be a collision body, not a collision shape :lol:
But I still don't get the explanation "Matching is computed as bitwise or operator of mask with group"...
I'll check on Bullet Physics website if I can find examples.
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

If anyone is wondering how to set groups and masks with ZgeBullet, I managed to make it work. It's simple but the explanations across the web are so obscure... Here's what I did for Omeganaut:

First you need to initialize the groups and masks:

Code: Select all

// Collision groups

int COL_PLAYER = 0x1;
int COL_OBJECT = 0x2;
int COL_ENEMY = 0x4;
int COL_POWERUP = 0x8;  // Collides only with COL_PLAYER
int COL_VISOR = 0x16; // Collides only with COL_ENEMY

// Collision masks = things the group is supposed to collide with

int MASK_PLAYER = COL_OBJECT | COL_ENEMY | COL_POWERUP;
int MASK_OBJECT = COL_OBJECT | COL_ENEMY | COL_PLAYER;
Then, when you create the collision body of your model:
Here, it's an enemy that can collide with other enemies, objects and the player:

Code: Select all

CurrentModel.EnemyBody = zbtCreateRigidBody(1, EnemyShape, CurrentModel.Position, CurrentModel.Rotation); 
zbtSetCollisionFilterGroupAndMask(CurrentModel.EnemyBody, COL_ENEMY, MASK_OBJECT);
zbtSetSleepingThresholds(CurrentModel.EnemyBody, 0, 0); // Don't forget that, otherwise collision are a bit wild
User avatar
Kjell
Posts: 1910
Joined: Sat Feb 23, 2008 11:15 pm

Re: 3D Physics with ZgeBullet

Post by Kjell »

Hi Ats,

Probably a honest mistake .. but if you're going to define bit-mask constants for your collision groups, make sure you only use values that set a single bit ( unless you want to have constants that contain multiple groups ). So you probably want CO_VISOR to be 0x10 instead of 0x16.

Code: Select all

HEX   DEC   BIN(LSB)
----- ----- --------
0x10  16    00010000
0x16  22    00010110
Can't imagine you intended "CO_VISOR = 0x16" to be identical to "CO_VISOR = 0x10 | COL_ENEMY | COL_OBJECT".

K
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

Woops... But COL_VISOR = 0x10 isn't working. Maybe because the MASK_OBJECT don't include COL_VISOR? I honestly don't know what I'm doing :lol:
But I removed COL_VISOR and set the raycast visor as a COL_OBJECT than only react to COL_ENEMY, and it works too.

Thanks for the explanations with the bits :)

Edit:
And the power up can also be a simple COL_OBJECT that only react to COL_PLAYER. Let's simplify everything !
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

Ok, I don't think I understand the documentation for zbtStepSimulation :
void zbtStepSimulation ( float timeStep, int maxSubSteps, float fixedTimeStep )
Proceed the simulation over time step.
@details By default, the timeStep is subdivided in constant sub-steps of each fixedTimeStep. In order to keep the simulation real-time, the maximum number of sub-steps can be clamped to maxSubSteps. You can disable subdividing the time step/sub-stepping by passing maxSubSteps=0, but in that case you have to keep the timeStep constant.
@param timeStep duration of simulation step in seconds
@param maxSubSteps if > 0, it will interpolate motion between fixedTimeStep's
@param fixedTimeStep fixed time step

So what should I do for a better precision?

Code: Select all

float t = App.DeltaTime;
zbtStepSimulation(t, 0, 0);
or

Code: Select all

zbtStepSimulation(t, 1, t);
Or is it the same?
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

All right, I'm moving to the last big part of porting Omeganaut to linux: the compilation of ZgeBullet.so library!

So I downloaded Bullet3 and ZgeBullet sourcecode.

Now I'm kind of following Rado1 compilation explanations, but there is no more build3\vs2010\0_Bullet3Solution.sln in Bullet3. So I tried building bullet3-master/build_cmake/ following those instructions but I'm not sure if it is needed.

Then I'm a bit lost when it comes to adding arguments to compilation programs...

Code: Select all

gcc -I ../../bullet3-master/src/ ZgeBullet.cpp
I included Bullet3 to compile ZgeBullet but all I get tons is errors such as...

Code: Select all

/usr/bin/ld: /tmp/ccbJGcS6.o: warning: relocation against `_ZTV13btSphereShape' in read-only section `.text._ZN13btSphereShapeC2Ef[_ZN13btSphereShapeC5Ef]'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
/usr/bin/ld: /tmp/ccbJGcS6.o: in function `zbtCreateStaticPlaneShape':
ZgeBullet.cpp:(.text+0x1a2): undefined reference to `btStaticPlaneShape::btStaticPlaneShape(btVector3 const&, float)'
Can someone more experienced than me hint me in the right direction? :oops:
User avatar
VilleK
Site Admin
Posts: 2317
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: 3D Physics with ZgeBullet

Post by VilleK »

I think the best chance of succeeding is if you can find the "bullet3-2.83.7" version of the bullet sources that Rado1 refers to in the build instructions. If there is no such zip file available then you could checkout the bullet sources from a revision from that year instead.
User avatar
Ats
Posts: 704
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: 3D Physics with ZgeBullet

Post by Ats »

I managed to compile bullet3. I just had to install cmake and python3-dev. By the way, it's better to download it from https://github.com/bulletphysics/bullet3/releases (where every version are stored), instead of making a full checkout, because the full thing is HUGE. +/-7go of little files...

But unfortunately, there are no script to compile bullet3-2.83.7 for linux. So I just went with the last one, since the needed functions hasn't changed.

So now I'm able to build rado1 ZgeBullet using:
gcc -c -I ~/bullet3-3.09/src/ -o ZgeBullet.o ZgeBullet.cpp

But I can't get it to link the object file into a shared library using:

gcc -shared -o Zgebullet.so ZgeBullet.o

Code: Select all

/usr/bin/ld: ZgeBullet.o: warning: relocation against `_ZTV18btVehicleRaycaster' in read-only section `.text._ZN18btVehicleRaycasterC2Ev[_ZN18btVehicleRaycasterC5Ev]'
/usr/bin/ld: ZgeBullet.o: relocation R_X86_64_PC32 against symbol `gCurrentWorld' can not be used when making a shared object; recompile with -fPIC
nor

gcc -fPIC -o Zgebullet.so ZgeBullet.o

which gives lots of undefined references such as:

Code: Select all

/usr/bin/ld: ZgeBullet.o: warning: relocation against `_ZTV13btSphereShape' in read-only section `.text._ZN13btSphereShapeC2Ef[_ZN13btSphereShapeC5Ef]'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
/usr/bin/ld: ZgeBullet.o: in function `zbtCreateStaticPlaneShape':
ZgeBullet.cpp:(.text+0x1f1): undefined reference to `btStaticPlaneShape::btStaticPlaneShape(btVector3 const&, float)'
/usr/bin/ld: ZgeBullet.cpp:(.text+0x23f): undefined reference to `btStaticPlaneShape::btStaticPlaneShape(btVector3 const&, float)'
/usr/bin/ld: ZgeBullet.o: in function `zbtCreateBoxShape':
ZgeBullet.cpp:(.text+0x308): undefined reference to `btBoxShape::btBoxShape(btVector3 const&)'
/usr/bin/ld: ZgeBullet.cpp:(.text+0x34f): undefined reference to `btBoxShape::btBoxShape(btVector3 const&)'
(it's way longer than that)

If anyone has an idea. I've already read so much pages about that...

Edit:
I tested making a so file out of a simple hello word cpp code, it's working.
User avatar
VilleK
Site Admin
Posts: 2317
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: 3D Physics with ZgeBullet

Post by VilleK »

I think you also need to provide all the .o files for bullet on that command line (or provide a folder where they are located).
Post Reply