MiniB3d for FreeBasic

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

I got it. make groups. earth is the static group.
Thanks have nice christmas days
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

Oke i'am trying to free entity collions.
Because it's slow i like to only enable collisions that are in range of the player.

I use the openB3D pdf you made, under ENTITY COLLISION i found the function RESETENTITY.
Amusing it supposes to reset back to no collisions. not seem to work.
There is also a FreeEntity that unload the model.
And FreeAction that for actions, seems also missing in pdf.

At line 78 in code below: i give the stars a 50% chance to reset the entity or make it blue. (not working)

Code: Select all

'#include "openb2d.bi"
#include "openb3d.bi"
#INCLUDE "crt/STDIO.BI"
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif
randomize 1234


screenres 1920, 1080, 32, , &h10002
dim as ulong scr_h, scr_w
screeninfo(scr_h, scr_w)
Graphics3d scr_h,scr_w,32,1,1
var mousezoom =150
var camera=createcamera(0)
	CAMERARANGE (camera,.1,1000)
	CAMERAPROJMODE(camera, 2)
	CAMERAZOOM(camera, 1/mousezoom)
	positionentity camera, 0, 0, -10

var player1 = createpivot
var p1 = createcylinder(16, 1, player1)
	'entityfx player1, 4
	entityalpha p1, 0.5
	rotateentity p1, -90, 0, 0
	scaleentity p1, 0.5, 0.05, 0.5
'dim as any ptr tex = LOADANIMTEXTURE("0011_Hplayer1 animation.png", 4, 120, 120, 0, 6)
'var spr = CREATESPRITE(p1)
	'positionentity(spr, 0, -1, -.5)
	'SCALESPRITE spr, 1.5, 1.5
	'SPRITEVIEWMODE spr, 2
	'rotateentity spr, 90, 0, 0
	'entitytexture(spr, tex, 0, 0)
	'entityfx (spr, 1+4+16+32)
	'entityorder spr, 0
	
var light=createlight()
	turnentity light, -45, -45, 0
	lightcolor light, 255, 127, 255
	
'var earth = createmesh
var earth = createsphere(16)
	positionentity earth, -5, -1, 0
	scaleentity earth, 4, 4, 4
	ENTITYRADIUS earth, 4
	entitycolor earth, 0, 255, 0
	EntityAlpha earth, 0.5
'var earthShadow = CREATESHADOW(earth, false)
	
var collision_player1 = 99
var collision_world = 98
ENTITYTYPE player1, collision_player1
entityRadius player1, 0.5
ENTITYTYPE earth, collision_world
collisions collision_player1, collision_world, 2, 2
	
dim as double xx,yy
var addStars = true                                      'add the stars
if addStars then
	var numberOfStars = 95 'max = 97
	dim as any ptr star(numberOfStars)
	dim as any ptr shadow(numberOfStars)
	for i as ulong = 1 to numberOfStars
		star(i) = createsphere(6,earth)
		'star(i) = createcube(earth)
		scaleentity star(i), .05, .05, .05
		do
			xx = (rnd-.5)*4
			yy = (rnd-.5)*4
		loop until sqr(xx^2+yy^2) > 1.2
		positionentity star(i), xx, yy, 0
		
		'shadow(i) = CREATESHADOW(star(i),false)
		ENTITYRADIUS star(i), i
		ENTITYTYPE star(i), collision_world
		collisions collision_player1, i, 2, 2
		if rnd > 0.5 then
			entitycolor star(i), 0,127,255
		else
			resetentity player1
			RESETENTITY star(i)
		end if
	next i
end if

var gravity = ACTTRACKBYPOINT (player1, earth, 0, 0, 0, 0.01)
var moment = ACTNEWTONIAN(player1, .95)
var rotPlayerToGrav = ACTTURNto(player1, -5, -1, 0, 0.1)


dim key as string
dim as long mx, my, mousewheel
wireframe 0
var ani_speed = 0
var jumpActive = 0

do
	dim As Long res = getmouse(mx, my, mousewheel)
	mousezoom = mousewheel*10 +150
	'mousewheel = 0
	CAMERAZOOM(camera, 1/mousezoom)
	
	PRINTf (chr(13))
	printf (str(countCollisions(player1))+"   "+str(ENTITYCOLLIDED(player1, collision_world))+"             ")
	'printf str(EntityRoll(player1))
	
	
	moveentity player1, 0, 0, -entityZ(player1) 'keep the player1 z at 0
	'entitytexture(spr, tex, TIMER*ani_speed MOD 6)
	key=inkey
		if key=chr(255)+"H" then moveentity camera,0, 0, 0.5
		if key=chr(255)+"P" then moveentity camera,0, 0,-0.5
		if key=chr(255)+"M" then turnentity camera,0,-5, 0, 0
		if key=chr(255)+"K" then turnentity camera,0, 5, 0, 0
		
		
		if multikey(SC_A) or multikey(SC_D) or multikey(SC_S) or multikey(SC_W) then
			collisions collision_player1, collision_world, 2, 2 ' set sliding on
			ani_speed = 10
			if multikey(SC_A) then moveentity player1, -0.02, 0, 0':rotateentity spr, 270, 0, 180
			if multikey(SC_D) then moveentity player1,  0.02, 0, 0':rotateentity spr, 90,  0, 0
			if multikey(SC_S) then moveentity player1, 0, -0.02, 0
			if multikey(SC_W) then moveentity player1, 0, 0.02, 0
		else
			collisions collision_player1, collision_world, 2, 1 ' set to no slide
			ani_speed = 0
		end if
		
		if multikey(SC_SPACE) then 'jump test
			if jumpActive = 0 then moveentity player1, 0, 0.4, 0
			jumpActive = 1
		else
			jumpActive = 0 ' work around fix becouse countCollisions(player1) not working with stars
			if countCollisions(player1) <> 0 then JumpActive = 0
		end if

		
	updateworld 1
	renderworld
	sleep 10
	'screensync
	flip
loop until key=chr(27)
Same for function CLEARCOLLISIONS() not doing any thing.
Also tested LibOpenB3D, same story
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

ResetEntity is supposed to be used after moving an entity, and before updating the world, to avoid collisions to be checked. It's useful, for example, when you want to place the player in a specific place of the world, and you have already enabled collision checking (otherwise it will be considered like a long movement, and obstacles on the trajectory would affect it). So, it makes no sense using it on entities like the stars, that are not supposed to move.

If you want to disable collision detection got the stars, just set their collision type back to zero with EntityType
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

angros47 wrote: Dec 24, 2023 16:27 ResetEntity is supposed to be used after moving an entity, and before updating the world, to avoid collisions to be checked. It's useful, for example, when you want to place the player in a specific place of the world, and you have already enabled collision checking (otherwise it will be considered like a long movement, and obstacles on the trajectory would affect it). So, it makes no sense using it on entities like the stars, that are not supposed to move.

If you want to disable collision detection got the stars, just set their collision type back to zero with EntityType
Yes i have tried setting EntityType back to 0.
it is not doing any thing, collision keeps been enabled.
Even worse then i use freeEntity on it with entitytype set will crash with a "segmentation violation"
what i just found out today.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

Whoops! Right, in the file "entity.cpp", the line

Code: Select all

	if(collision_type!=0 && type_no==0){
		CollisionPair::ent_lists[type_no].remove(this);
	}
should be

Code: Select all

	if(collision_type!=0 && type_no==0){
		CollisionPair::ent_lists[collision_type].remove(this);
	}
Looks like you found a bug
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

Uploaded an updated version with the fix, let me know if it works
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

angros47 wrote: Dec 26, 2023 0:16 Uploaded an updated version with the fix, let me know if it works
yeah, thanks now i can make my game.
Setting EntityType to 0 works fine and FreeEntity works correct.

I have one thing that is annoying about the DLLs
That is the name for openB3D.dll is the same for 32 and 64 bit. this way i cant not put hem in the same directory. I like a easy way to test 32 and 64 bit with different compile commands.
Rename the dlls to openB3D_64.dll and set openB3D.bi to that file i stile get a error openB3D.dll not find when trying to run.

For now i only tested the 64bit version because of that.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

You can modify the makefile and recompile it, in case you want to change its name
Otherwise, you can put the two versions of the library in two different directories, and when you compile and launch your program you set the PATH variable to one directory or another (the library doesn't have to be in the same directory of the executable)
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

angros47 wrote: Dec 26, 2023 18:10 You can modify the makefile and recompile it, in case you want to change its name
Otherwise, you can put the two versions of the library in two different directories, and when you compile and launch your program you set the PATH variable to one directory or another (the library doesn't have to be in the same directory of the executable)
Yeah thanks, i just think about making it easier for other users that like to use this openB3D version in the future. Because i like it.

Can you please add this line to the sourceforge version "openb3d.bi" file next update.

Code: Select all

declare sub FreeAction alias "FreeAction" (byval act1 as any ptr)
And i really like to know more about the openb3d physics nowton? or where it's build of?
Any refrend documents? it's behavior is not like others physics i used but still good.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

OpenB3D is not based on Newton, or any external physics engine. An external physics engine can be used, but OpenB3D has its own features: one is the collision detection, that comes from the original library MiniB3D, which OpenB3D is based on. The author, Simon Harrison, stated that he was helped by Mark Sibly, author of Blitz3D, who shared with him some of the algorithms.

The "actions" were a feature I copied from another open source 3d engine, called bOGL (https://github.com/AlexCeleste/bOGL-2): the specific action ActNewtonian is my idea

The commands CreateConstraint and CreateRigidBody are mine, too, and based on a simplified version of Verlet integration. Over it, it's possible to implement a lot of physics features.
Still, OpenB3D is not a physics library: there is nothing to implement, for example, different grades of freedom in joints, or wheels, or fluid dynamics. If you need these features you should use a specialized library. But for entry level physics, the provided features should be enough
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

ActNewtonian is a nice function to make beter motion.
I wish i can get to current velocity of current movement speed.

Also like to fix the z position at 0 because i make a 2d game in 3d space.

In my code i made a sample space ship from a cone shape with 2 very small cube as landing feeds. The feet are child object with entitytype set. Zo the feeds make contact with planet. I did expect the child cubes be fixed with the parent cone. Zo if the feeds hit the planet the cone stops moving.
This was not the case. The feet move withing the cone.

The best thing to add is fix body within bodies. Hope i make sense. Adding futures requers c programming i m not very good at it.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

You can use EntityX, EntityY and EntityZ to get an entity position. Save those values in some variables, at the next frame read them again, make the difference with old ones, and the three results would be the vector of your speed. In case you just want the speed, square those three values, add them and calculate the square root, that would be the speed of your object.

About fixing the z position to 0... if you set all the entities (minus the camera, of course) with z to 0, you don't need to lock it, because there is no reason for z to ever change, as long as you don't rotate anything on the wrong axis (anyone but the roll axis)

About your spaceship issue: collisions affect the moving entity (the feet of the spaceship, that are indeed stopped by the obstacle), but not its parent. If you want to stop the whole spaceship, set the appropriate entitytype to the cone, not to its children. You can move it using TranslateMesh, if you want to make it off centered, and you can use EntityRadius to ensure a radius large enough to include the feet too.

What do you mean as "fix body"? An invisible entity to use for collisions, and to attach sprites or meshes to? Such as entity exists already, it's called pivot
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

Yes i think i already got all of this.

About the ship with lading feets.
I am tryng to make a more complex collision chape than a single raduis.
The ship is a cone and from side its a triangel in 2d.
Putting a cirkel or elipse collision on triangel chape is a bad fit.
Zo i put 2 small cube feet on bottom with small collision raduis.
I wish to fix the feet on bottom corners and let the collisions be solved by ship position.
Not the feet position. Without luck.
I did use positionentity and not translateEntity not sure how this make a diffrens.

Also tried createconstrain the distance and make a cube chape out of small spheres.
I have a small demo that makes use of this and the rigidebody.
It is hard to setup or to have control over.
Using only createconstrain seem to trip out on me.

Let me make a small code example of the spaceship.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

In your example, then, you might just want to have the spaceship, and the feet as separate entities, both able to collide, and you can put a constraint between them to ensure they would keep always the same distance. You could ensure they point at the same direction either suing some action, or manually
Gunslinger
Posts: 105
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: MiniB3d for FreeBasic

Post by Gunslinger »

i give up, 100 ways to make is crash.
entitybox seems not implemented.
This code below try to fix feets with 3 constraints for each feet.

Code: Select all

#include "openb3d.bi"
#INCLUDE "crt/STDIO.BI"
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif
randomize 1234

function getEntityRoll(x as double, y as double) as double
	if x = 0 then
		if y = 0 then return -0
		if y < 0 then return 180
		return 0
	end if
	if x > 0 then return atan2(x, y) * 57.29577951307855
	return -(atan2(abs(x), y) *  57.29577951307855)
end function

screenres 1920, 1080, 32, , &h10002
dim as ulong scr_h, scr_w
screeninfo(scr_h, scr_w)
Graphics3d scr_h,scr_w, 32, 1, 1

var mousezoom =200
var camera=createcamera(0)
	CAMERARANGE (camera,.1,1000)
	CAMERAPROJMODE(camera, 2)
	CAMERAZOOM(camera, 1/mousezoom)
	positionentity camera, 0, 0, -10

'var player1 = createpivot
	'entityBOX player1,0, 0, 0, 1, 1, 1 ' not working ?
var player1 = createsphere(16)
	entityalpha player1, 0.5
	var cone = createcone(8, 1, player1)
	scaleentity cone, 2, 2, 2
	entityalpha cone, 0.5
	
var light=createlight()
	turnentity light, -45, -45, 0
	lightcolor light, 255, 127, 255
	
var earth = createsphere(16)
	positionentity earth, -5, -1, 0
	scaleentity earth, 4, 4, 4
	ENTITYRADIUS earth, 4
	entitycolor earth, 0, 255, 0
	EntityAlpha earth, 0.5
	

var collision_player1 = 1
var collision_world = 2
ENTITYTYPE player1, collision_player1 '1= set collision to childs (also not working?) crashing
ENTITYTYPE earth, collision_world


var pivot1 = createpivot(player1)
TRANSLATEENTITY pivot1, 0, 0, -2
var pivot2 = createpivot(player1)
TRANSLATEENTITY pivot2, 0, 0, 2
var pivot3 = createpivot(player1)
TRANSLATEENTITY pivot2, 0, 2,0

var feetL = createcube(player1)        'left feet
TRANSLATEENTITY feetL, -2, -2, 0
scaleentity feetL, 0.2, 0.2, 0.2

var feetR = createcube(player1)       'right feet
TRANSLATEENTITY feetR, 2, -2, 0
scaleentity feetR, 0.2, 0.2, 0.2

entityradius feetL, 0.2
ENTITYTYPE feetL, collision_player1 
entityradius feetR, 0.2
ENTITYTYPE feetR, collision_player1 



var conL1 = CREATECONSTRAINT (feetL, pivot1, ENTITYDISTANCE(feetL, pivot1))
var conL2 = CREATECONSTRAINT (feetL, pivot2, ENTITYDISTANCE(feetL, pivot2))
var conL3 = CREATECONSTRAINT (feetL, pivot3, ENTITYDISTANCE(feetL, pivot3))

var conR1 = CREATECONSTRAINT (feetR, pivot1, ENTITYDISTANCE(feetR, pivot1))
var conR2 = CREATECONSTRAINT (feetR, pivot2, ENTITYDISTANCE(feetR, pivot2))
var conR3 = CREATECONSTRAINT (feetR, pivot3, ENTITYDISTANCE(feetR, pivot3))


'var con1 = CREATECONSTRAINT (box, cyl, 0)
'var con2 = CREATECONSTRAINT (cone, cyl, 0)
'var con3 = CREATECONSTRAINT (cyl, player1, 1)


ENTITYTYPE player1, collision_player1 '1= set collision to childs (also not working?) crashing
ENTITYTYPE earth, collision_world

collisions collision_player1, collision_world, 2, 2 'set to 3,2 for entitybox (also not working?)






dim as double xx,yy
var addStars = true                   'add the stars
if addStars then
	var numberOfStars = 95 'max = 97
	dim as any ptr star(numberOfStars)
	dim as any ptr shadow(numberOfStars)
	for i as ulong = 1 to numberOfStars
		star(i) = createsphere(6,earth)
		'star(i) = createcube(earth)
		scaleentity star(i), 0.05, 0.05, 0.05
		do
			xx = (rnd-.5) * 8
			yy = (rnd-.5) * 8
		loop until sqr(xx^2+yy^2) > 1.2
		positionentity star(i), xx, yy, 0
		
		'shadow(i) = CREATESHADOW(star(i),false)
		ENTITYRADIUS star(i), i
		ENTITYTYPE star(i), collision_world
		collisions collision_player1, i, 2, 2
	next i
end if

var gravity = ACTTRACKBYPOINT (player1, earth, 0, 0, 0, 0.01)
var moment = ACTNEWTONIAN(player1, 0.95)
var rotPlayerToGrav = ACTTURNto(player1, -5, -1, 0, 0.1)


dim key as string
dim as long mx, my, mousewheel
wireframe 0
var ani_speed = 0
var jumpActive = 0

do
	rotateentity player1, 0,0,0
	dim As Long res = getmouse(mx, my, mousewheel)
	mousezoom = mousewheel*10 +200
	CAMERAZOOM(camera, 1/mousezoom)
	
	PRINTf (chr(13))
	'printf (str(countCollisions(player1))+"   "+str(ENTITYCOLLIDED(player1, collision_world))+"             ")
	
	'var getroll = getEntityRoll(EntityX(earth)-EntityX(player1), EntityY(player1)-EntityY(earth))
	'rotateentity player1, 0, 0, getroll
	
	positionentity player1, entityX(player1), entityY(player1), 0 'keep the player1 z at 0
	positionentity cone, entityX(cone), entityY(cone), 0
	'positionentity box, entityX(box), entityY(box), 0
	
	key=inkey
		if key=chr(255)+"H" then moveentity camera,0, 0, 0.5
		if key=chr(255)+"P" then moveentity camera,0, 0,-0.5
		if key=chr(255)+"M" then turnentity camera,0,-5, 0, 0
		if key=chr(255)+"K" then turnentity camera,0, 5, 0, 0
		
		
		if multikey(SC_A) or multikey(SC_D) or multikey(SC_S) or multikey(SC_W) then
			collisions collision_player1, collision_world, 2, 2 ' set sliding on
			ani_speed = 10
			if multikey(SC_A) then moveentity player1, -0.02, 0, 0':rotateentity spr, 270, 0, 180
			if multikey(SC_D) then moveentity player1,  0.02, 0, 0':rotateentity spr, 90,  0, 0
			if multikey(SC_S) then moveentity player1, 0, -0.02, 0
			if multikey(SC_W) then moveentity player1, 0, 0.02, 0
		else
			collisions collision_player1, collision_world, 2, 1 ' set to no slide
			ani_speed = 0
		end if
		
		if multikey(SC_SPACE) then 'jump test
			if jumpActive = 0 then moveentity player1, 0, 0.4, 0
			jumpActive = 1
		else
			jumpActive = 0 ' work around fix becouse countCollisions(player1) not working with stars
			if countCollisions(player1) <> 0 then JumpActive = 0
		end if

		
	updateworld 1
	renderworld
	'sleep 10
	screensync
	flip
loop until key=chr(27)
Post Reply