Start work on collision update and virtual sim

This commit is contained in:
satellite 2021-07-10 19:43:00 -04:00
parent 3cf8a45c63
commit f585af6fad
13 changed files with 598 additions and 2 deletions

View File

@ -1,5 +1,18 @@
package electrosphere.game.collision;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
@ -15,9 +28,31 @@ import org.joml.Vector3f;
*/
public class CollisionEngine {
BroadphaseInterface broadphase;
DefaultCollisionConfiguration collisionConfiguration;
CollisionDispatcher dispatcher;
List<Entity> collisionEntities = new ArrayList();
public CollisionEngine(){
broadphase = new DbvtBroadphase();
collisionConfiguration = new DefaultCollisionConfiguration();
dispatcher = new CollisionDispatcher(collisionConfiguration);
SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
DiscreteDynamicsWorld world = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
CollisionShape boxShape = new BoxShape(new javax.vecmath.Vector3f(1,1,1));
float mass = 1.0f;
DefaultMotionState fallMotionState = new DefaultMotionState(new Transform(new javax.vecmath.Matrix4f(new javax.vecmath.Quat4f(0,0,0,1),new javax.vecmath.Vector3f(0,50,0),1.0f)));
RigidBodyConstructionInfo boxShapeRigidBodyCI = new RigidBodyConstructionInfo(mass, fallMotionState, boxShape);
RigidBody boxRigidBody = new RigidBody(boxShapeRigidBodyCI);
world.addRigidBody(boxRigidBody);
//https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=11507
//https://www.google.com/search?client=firefox-b-1-d&q=bullet+set+position+and+check+if+collision
//https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=11399
}

View File

@ -0,0 +1,12 @@
package electrosphere.game.collision.collidable;
/**
*
* @author satellite
*/
public class Collidable {
}

View File

@ -1,9 +1,96 @@
package electrosphere.game.server.world.datacell;
import electrosphere.entity.Entity;
import electrosphere.net.server.Player;
import electrosphere.game.server.character.Character;
import electrosphere.game.server.world.virtualcell.VirtualCell;
import electrosphere.main.Globals;
import electrosphere.net.parser.net.message.NetworkMessage;
import java.util.List;
/**
*
* @author amaterasu
* Container for entities loaded into memory. This isn't intended to be in charge
* of simulation. It just acts as an object to relate players and entities by location.
* This SHOULD be used for networking purposes. This is the mechanism to scope
* network messages by location.
*
*/
public class DataCell {
List<Entity> loadedEntities;
List<Player> activePlayers;
/**
* Constructs a datacell based on a virtual cell. Should be used when a player
* first comes into range of the cell.
* @param virtualCell
*/
public DataCell(VirtualCell virtualCell, Player p){
}
/**
* Add a player to the current datacell. This should be done if a player moves
* into close enough proximity with the cell that they are concerned with
* the microlevel simulation of the cell. This should _not_ be called if
* this is the first player coming into range.
* @param p
*/
public void addPlayer(Player p){
if(!activePlayers.contains(p)){
activePlayers.add(p);
}
}
/**
* Removes a player from the current datacell. Basically the player has moved
* far enough away that this cell doesn't concern them anymore.
* If the number of players for this cell is zero, we should prepare it to be
* unloaded from memory and converted back into a virtual cell.
* @param p
*/
public void removePlayer(Player p){
activePlayers.remove(p);
}
/**
* This should be used to translate a character from macrolevel simulation to
* microlevel, datacell based simulation.
* @param character
*/
public void addCharacter(Character character){
Entity newEntity = new Entity();
loadedEntities.add(newEntity);
}
/**
* This should be used to translate an entity from one data cell to another.
* @param e
*/
public void addEntity(Entity e){
loadedEntities.add(e);
}
/**
* Intention of this function is to entirely remove the entity from microlevel simulation.
* This should be conbined with code to convert the entity into a tracked macrolevel character.
* The previous should not be true if you're tracking something inconsequential like
* a rabbit or something. Then just delete it.
* @param e
*/
public void removeEntity(Entity e){
loadedEntities.remove(e);
}
/**
* Broadcast a message to all players within range of this cell.
* @param message
*/
public void broadcastNetworkMessage(NetworkMessage message){
for(Player player : activePlayers){
player.setMessage(message);
}
}
}

View File

@ -0,0 +1,16 @@
package electrosphere.game.server.world.datacell;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author satellite
*/
public class DataCellManager {
List<DataCell> loadedDataCells = new LinkedList();
public DataCellManager() {
}
}

View File

@ -0,0 +1,31 @@
package electrosphere.game.server.world.virtualcell;
import java.util.List;
import electrosphere.game.server.character.Character;
import java.util.LinkedList;
/**
* Container of virtual character data related to a position. The intention isn't
* to have it perform simulation itself.
*/
public class VirtualCell {
List<Character> characterList = new LinkedList();
public void addCharacter(Character c){
if(!characterList.contains(c)){
characterList.add(c);
}
}
public void removeCharacter(Character c){
characterList.remove(c);
}
public List<Character> getCharacters(){
return characterList;
}
}

View File

@ -162,6 +162,7 @@ public class Globals {
//
//keeps track of all entities in game
public static EntityManager entityManager;
public static EntityManager serverEntityManager;
//terrain manager
public static boolean LOAD_TERRAIN = true;

View File

@ -0,0 +1,23 @@
package electrosphere.net.server;
import electrosphere.net.parser.net.message.NetworkMessage;
/**
*
* @author satellite
*/
public class Player {
ServerConnectionHandler connectionHandler;
int id;
public int getId() {
return id;
}
public void setMessage(NetworkMessage message){
connectionHandler.addMessagetoOutgoingQueue(message);
}
}

View File

@ -0,0 +1,69 @@
package electrosphere.renderer.light;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class ItsBroken_DirectionalLight {
Vector3f direction;
Vector3f ambient;
Vector3f diffuse;
Vector3f specular;
public void setDirection(Vector3f direction) {
this.direction = direction;
}
public void setAmbient(Vector3f ambient) {
this.ambient = ambient;
}
public void setDiffuse(Vector3f diffuse) {
this.diffuse = diffuse;
}
public void setSpecular(Vector3f specular) {
this.specular = specular;
}
public Vector3f getDirection() {
return direction;
}
public Vector3f getAmbient() {
return ambient;
}
public Vector3f getDiffuse() {
return diffuse;
}
public Vector3f getSpecular() {
return specular;
}
public ItsBroken_DirectionalLight(Vector3f direction){
this.direction = direction;
ambient = new Vector3f(0.05f, 0.05f, 0.05f);
diffuse = new Vector3f(0.4f, 0.4f, 0.4f);
specular = new Vector3f(0.5f, 0.5f, 0.5f);
this.direction.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
public ItsBroken_DirectionalLight(Vector3f direction, Vector3f color){
this.direction = direction;
ambient = new Vector3f( color.x * 0.05f, color.y * 0.05f, color.z * 0.05f);
diffuse = new Vector3f( color.x * 0.4f, color.y * 0.4f, color.z * 0.4f);
specular = new Vector3f(color.x * 0.5f, color.y * 0.5f, color.z * 0.5f);
this.direction.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
}

View File

@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package electrosphere.renderer.light;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL31.GL_UNIFORM_BUFFER;
/**
*
* @author amaterasu
*/
public class LightBuffer {
int light_uniform_buffer_address;
public LightBuffer(){
light_uniform_buffer_address = glGenBuffers();
glBindBuffer(GL_UNIFORM_BUFFER,light_uniform_buffer_address);
//glBufferData(GL_UNIFORM_BUFFER, <my_data>, GL_STATIC_DRAW);
}
}

View File

@ -0,0 +1,41 @@
package electrosphere.renderer.light;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.main.Globals;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class LightEntityUtils {
public static Entity createDirectionalLight(Vector3f position, Vector3f ambient, Vector3f diffuse, Vector3f specular){
Entity rVal = new Entity();
Globals.entityManager.registerEntity(rVal);
Globals.entityManager.registerLightEntity(rVal);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_TYPE, EntityDataStrings.DATA_STRING_LIGHT_TYPE_DIRECTIONAL);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, position);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_AMBIENT, ambient);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_DIFFUSE, diffuse);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_SPECULAR, specular);
return rVal;
}
public static Entity createPointLight(Vector3f position, Vector3f ambient, Vector3f diffuse, Vector3f specular, float constant, float linear, float quadratic){
Entity rVal = new Entity();
Globals.entityManager.registerEntity(rVal);
Globals.entityManager.registerLightEntity(rVal);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_TYPE, EntityDataStrings.DATA_STRING_LIGHT_TYPE_POINT);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, position);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_AMBIENT, ambient);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_DIFFUSE, diffuse);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_SPECULAR, specular);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_CONSTANT, constant);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_LINEAR, linear);
rVal.putData(EntityDataStrings.DATA_STRING_LIGHT_QUADRATIC, quadratic);
return rVal;
}
}

View File

@ -0,0 +1,9 @@
package electrosphere.renderer.light;
/**
*
* @author amaterasu
*/
public class LightManager {
}

View File

@ -0,0 +1,106 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package electrosphere.renderer.light;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class PointLight {
Vector3f position;
float constant;
float linear;
float quadratic;
Vector3f ambient;
Vector3f diffuse;
Vector3f specular;
public void setPosition(Vector3f position) {
this.position = position;
}
public void setConstant(float constant) {
this.constant = constant;
}
public void setLinear(float linear) {
this.linear = linear;
}
public void setQuadratic(float quadratic) {
this.quadratic = quadratic;
}
public void setAmbient(Vector3f ambient) {
this.ambient = ambient;
}
public void setDiffuse(Vector3f diffuse) {
this.diffuse = diffuse;
}
public void setSpecular(Vector3f specular) {
this.specular = specular;
}
public Vector3f getPosition() {
return position;
}
public float getConstant() {
return constant;
}
public float getLinear() {
return linear;
}
public float getQuadratic() {
return quadratic;
}
public Vector3f getAmbient() {
return ambient;
}
public Vector3f getDiffuse() {
return diffuse;
}
public Vector3f getSpecular() {
return specular;
}
public PointLight(Vector3f position){
this.position = position;
constant = 1.0f;
linear = 0.01f;
quadratic = 0.01f;
ambient = new Vector3f(0.05f, 0.05f, 0.05f);
diffuse = new Vector3f(0.8f, 0.8f, 0.8f);
specular = new Vector3f(1.0f, 1.0f, 1.0f);
this.position.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
public PointLight(Vector3f position, Vector3f color){
this.position = position;
constant = 1.0f;
linear = 0.01f;
quadratic = 0.01f;
ambient = new Vector3f(color.x * 0.05f, color.y * 0.05f, color.z * 0.05f);
diffuse = new Vector3f(color.x * 0.8f, color.y * 0.8f, color.z * 0.8f);
specular = new Vector3f(color.x, color.y, color.z);
this.position.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
}

View File

@ -0,0 +1,143 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package electrosphere.renderer.light;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class SpotLight {
Vector3f position;
Vector3f direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
Vector3f ambient;
Vector3f diffuse;
Vector3f specular;
public void setPosition(Vector3f position) {
this.position = position;
}
public void setDirection(Vector3f direction) {
this.direction = direction;
}
public void setCutOff(float cutOff) {
this.cutOff = cutOff;
}
public void setOuterCutOff(float outerCutOff) {
this.outerCutOff = outerCutOff;
}
public void setConstant(float constant) {
this.constant = constant;
}
public void setLinear(float linear) {
this.linear = linear;
}
public void setQuadratic(float quadratic) {
this.quadratic = quadratic;
}
public void setAmbient(Vector3f ambient) {
this.ambient = ambient;
}
public void setDiffuse(Vector3f diffuse) {
this.diffuse = diffuse;
}
public void setSpecular(Vector3f specular) {
this.specular = specular;
}
public Vector3f getPosition() {
return position;
}
public Vector3f getDirection() {
return direction;
}
public float getCutOff() {
return cutOff;
}
public float getOuterCutOff() {
return outerCutOff;
}
public float getConstant() {
return constant;
}
public float getLinear() {
return linear;
}
public float getQuadratic() {
return quadratic;
}
public Vector3f getAmbient() {
return ambient;
}
public Vector3f getDiffuse() {
return diffuse;
}
public Vector3f getSpecular() {
return specular;
}
public SpotLight(Vector3f position, Vector3f direction){
this.position = position;
this.direction = direction;
cutOff = (float)Math.toRadians(12.5f);
outerCutOff = (float)Math.toRadians(15.0f);
constant = 1.0f;
linear = 0.01f;
quadratic = 0.01f;
ambient = new Vector3f(0.05f, 0.05f, 0.05f);
diffuse = new Vector3f(0.8f, 0.8f, 0.8f);
specular = new Vector3f(1.0f, 1.0f, 1.0f);
this.position.normalize();
this.direction.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
public SpotLight(Vector3f position, Vector3f direction, Vector3f color){
this.position = position;
this.direction = direction;
constant = 1.0f;
linear = 0.01f;
quadratic = 0.01f;
ambient = new Vector3f(color.x * 0.05f, color.y * 0.05f, color.z * 0.05f);
diffuse = new Vector3f(color.x * 0.8f, color.y * 0.8f, color.z * 0.8f);
specular = new Vector3f(color.x, color.y, color.z);
this.position.normalize();
this.direction.normalize();
ambient.normalize();
diffuse.normalize();
specular.normalize();
}
}