OK I just started out on getting my own version of a game going you could say. I took a lot of concepts from the flag rush tutorial and put in my own code where I felt it was needed. Right now I have a player that is being loaded onto my terrain that I built and I have a box beside the player. I want the player to collide with the box, so that she doesn't go through it. Right now if I walk forward it will stop me going through the box, sometimes I can get out by going forward (think it depends on how fast I hit it) but always I can just back up and go through the box. What am I missing here? I realize that I took out back/front wheel from the Vehicle, because it's actually a model of a human but is that a problem ?
Source code : buildPlayer()
private void buildPlayer() {
URL folder= DarkRune.class.getClassLoader().getResource("simplegame/");
URL model = DarkRune.class.getClassLoader().getResource("simplegame/female.obj");
FormatConverter converter=new ObjToJme();
converter.setProperty("mtllib", folder);
converter.setProperty("texdir", folder);
ByteArrayOutputStream BO=new ByteArrayOutputStream();
try {
converter.convert(model.openStream(), BO);
fem=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
fem.setLocalScale(1f);
fem.setModelBound(new BoundingBox());
fem.updateModelBound();
} catch (Exception e) {
System.out.println("Damn exceptions! O_o n" + e);
e.printStackTrace();
System.exit(0);
}
/*final ShadeState smoothShadeState = display.getRenderer().createShadeState();
smoothShadeState.setShadeMode( ShadeState.ShadeMode.Smooth );
fem.setRenderState( smoothShadeState );*/
player = new Vehicle("Player", fem);
player.setAcceleration(15);
player.setBraking(25);
player.setTurnSpeed(5);
player.setWeight(25);
player.setMaxSpeed(25);
player.setMinSpeed(15);
player.setLocalTranslation(new Vector3f(115,tb.getHeight(115,115), 115));
scene.attachChild(player);
scene.updateGeometricState(0, true);
// player.attachChild(fem);
// player.updateWorldBound();
}
Source code : CollisionDetection.java
/*
* Copyright (c) 2003-2008 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DarkRune;
import jmetest.effects.RenParticleEditor;
import DarkRune.Vehicle;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.effects.particles.ParticleSystem;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.SimpleParticleInfluenceFactory;
import java.util.logging.Logger;
public class CollisionDetection{
private static final Logger logger = Logger.getLogger(DarkRune.class
.getName());
public static final int BounceOff = 0;
public static final int Stop = 1;
private Vehicle player;
private Node target;
private int NextMove;
private ParticleSystem particleGeom;
private float Zextent;
public CollisionDetection(Vehicle Player, Node Target,int NextMove) {
this.player = Player;
this.target = Target;
this.NextMove = NextMove;
this.Zextent =((BoundingBox)player.getWorldBound()).zExtent;
createParticles();
}
public CollisionDetection(Vehicle Player, Spatial Target,int NextMove) {
this.player = Player;
this.target = (Node)Target;
this.NextMove = NextMove;
this.Zextent =((BoundingBox)player.getWorldBound()).zExtent;
createParticles();
}
public void ProcessCollisions() {
if (NextMove == BounceOff){
if (player.hasCollision(target, false)){
System.out.println("Coll 1");
if (player.hasCollision(target, false)){
System.out.println("Coll 2");
player.setVelocity(-Math.abs(player.getVelocity()));
if (player.getVelocity() > -0.5)
player.setVelocity(-0.5f);
particleGeom.setLocalTranslation(0F,player.getLocalTranslation().y*0.0025f, Zextent);
particleGeom.forceRespawn();
} else if(player.hasCollision(target, false)){
System.out.println("Coll 3");
player.setVelocity(Math.abs(player.getVelocity()));
if (player.getVelocity() < 0.5)
player.setVelocity(0.5f);
particleGeom.setLocalTranslation(0F,player.getLocalTranslation().y*0.0025f, -Zextent);
particleGeom.forceRespawn();
}
}
}else if (NextMove == Stop){
if (player.hasCollision(target, false)){
if (player.hasCollision(target, false)){
player.setMaxSpeed(0f);
player.setMinSpeed(15f);
} else if(player.hasCollision(target, false)){
System.out.println("Coll 5");
player.setMaxSpeed(25f);
player.setMinSpeed(0f);
}
} else {
player.setMaxSpeed(25f);
player.setMinSpeed(15f);
}
}
}
private void createParticles(){
particleGeom = ParticleFactory.buildParticles("collsion", 300);
particleGeom.addInfluence(SimpleParticleInfluenceFactory
.createBasicGravity(new Vector3f(0, -0.01f, 0), true));
particleGeom.setEmissionDirection(new Vector3f(0.0f, 1.0f, 0.0f));
particleGeom.setMaximumAngle(360f * FastMath.DEG_TO_RAD);
particleGeom.setMinimumLifeTime(2000.0f);
particleGeom.setMaximumLifeTime(4000.0f);
particleGeom.setInitialVelocity(.004f);
particleGeom.recreate(FastMath.nextRandomInt(6, 20));
particleGeom.setStartSize(.05f);
particleGeom.setEndSize(.02f);
particleGeom.setStartColor(new ColorRGBA(1.0f, 0.796875f, 0.1992f, 1.0f));
particleGeom.setEndColor(new ColorRGBA(1.0f, 1.0f, 0.5976f, 1.0f));
//particleGeom.warmUp(120);
particleGeom.getParticleController().setRepeatType(Controller.RT_CLAMP);
BlendState as = (BlendState) particleGeom
.getRenderState(RenderState.RS_BLEND);
if (as == null) {
as = DisplaySystem.getDisplaySystem().getRenderer()
.createBlendState();
as.setBlendEnabled(true);
as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
as.setTestEnabled(true);
as.setTestFunction(BlendState.TestFunction.GreaterThan);
particleGeom.setRenderState(as);
particleGeom.updateRenderState();
}
as.setDestinationFunction(BlendState.DestinationFunction.One);
TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
ts.setTexture(TextureManager.loadTexture(RenParticleEditor.class
.getClassLoader().getResource(
"jmetest/data/texture/flaresmall.jpg"),
Texture.MinificationFilter.BilinearNearestMipMap, Texture.MagnificationFilter.Bilinear));
particleGeom.setRenderState(ts);
player.attachChild(particleGeom);
particleGeom.updateRenderState();
}
}
Source code : Vehicle.java
/*
* Copyright (c) 2003-2009 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DarkRune;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
/**
* Vehicle will be a node that handles the movement of a vehicle in the
* game. It has parameters that define its acceleration and speed as well
* as braking. The turn speed defines what kind of handling it has, and the
* weight will define things such as friction for drifting, how fast it falls
* etc.
* @author Mark Powell
*
*/
/**
* @author Mark Powell
*
*/
public class Vehicle extends Node {
private static final long serialVersionUID = 1L;
private Spatial model;
private float weight;
private float velocity;
private float acceleration;
private float braking;
private float turnSpeed;
private float maxSpeed = 30;
private float minSpeed = 10;
// temporary vector for the rotation
private static final Vector3f tempVa = new Vector3f();
/**
* Basic constructor takes the model that represents the graphical
* aspects of this Vehicle.
* @param id the id of the vehicle
* @param model the model representing the graphical aspects.
*/
public Vehicle(String id, Spatial model) {
super(id);
setModel(model);
}
/**
* Constructor takes all performance attributes of the vehicle during
* creation.
* @param id the id of the vehicle
* @param model the model representing the graphical apsects.
* @param maxSpeed the maximum speed this vehicle can reach. (Unit/sec)
* @param minSpeed the maximum speed this vehicle can reach while traveling in reverse. (Unit/sec)
* @param weight the weight of the vehicle.
* @param acceleration how fast this vehicle can reach max speed
* @param braking how fast this vehicle can slow down and if held long enough reverse
* @param turnSpeed how quickly this vehicle can rotate.
*/
public Vehicle(String id, Spatial model, float maxSpeed, float minSpeed,
float weight, float acceleration, float braking, float turnSpeed) {
super(id);
setModel(model);
this.maxSpeed = maxSpeed;
this.minSpeed = minSpeed;
this.weight = weight;
this.acceleration = acceleration;
this.braking = braking;
this.turnSpeed = turnSpeed;
}
/**
* update applies the translation to the vehicle based on the time passed.
* @param time the time between frames
*/
public void update(float time) {
this.localTranslation.addLocal(this.localRotation.getRotationColumn(2, tempVa)
.multLocal(velocity * time));
}
/**
* set the weight of this vehicle
* @param weight the weight of this vehicle
*/
public void setWeight(float weight) {
this.weight = weight;
}
/**
* retrieves the weight of this vehicle.
* @return the weight of this vehicle.
*/
public float getWeight() {
return weight;
}
/**
* retrieves the acceleration of this vehicle.
* @return the acceleration of this vehicle.
*/
public float getAcceleration() {
return acceleration;
}
/**
* set the acceleration rate of this vehicle
* @param acceleration the acceleration rate of this vehicle
*/
public void setAcceleration(float acceleration) {
this.acceleration = acceleration;
}
/**
* retrieves the braking speed of this vehicle.
* @return the braking speed of this vehicle.
*/
public float getBraking() {
return braking;
}
/**
* set the braking speed of this vehicle
* @param braking the braking speed of this vehicle
*/
public void setBraking(float braking) {
this.braking = braking;
}
/**
* retrieves the model Spatial of this vehicle.
* @return the model Spatial of this vehicle.
*/
public Spatial getModel() {
return model;
}
/**
* sets the model spatial of this vehicle. It first
* detaches any previously attached models.
* @param model the model to attach to this vehicle.
*/
public void setModel(Spatial model) {
this.detachChild(this.model);
this.model = model;
this.attachChild(this.model);
}
/**
* retrieves the velocity of this vehicle.
* @return the velocity of this vehicle.
*/
public float getVelocity() {
return velocity;
}
/**
* set the velocity of this vehicle
* @param velocity the velocity of this vehicle
*/
public void setVelocity(float velocity) {
this.velocity = velocity;
}
/**
* retrieves the turn speed of this vehicle.
* @return the turn speed of this vehicle.
*/
public float getTurnSpeed() {
return turnSpeed;
}
/**
* set the turn speed of this vehicle
* @param turnSpeed the turn speed of this vehicle
*/
public void setTurnSpeed(float turnSpeed) {
this.turnSpeed = turnSpeed;
}
/**
* retrieves the maximum speed of this vehicle.
* @return the maximum speed of this vehicle.
*/
public float getMaxSpeed() {
return maxSpeed;
}
/**
* sets the maximum speed of this vehicle.
* @param maxSpeed the maximum speed of this vehicle.
*/
public void setMaxSpeed(float maxSpeed) {
this.maxSpeed = maxSpeed;
}
/**
* retrieves the minimum speed of this vehicle.
* @return the minimum speed of this vehicle.
*/
public float getMinSpeed() {
return minSpeed;
}
/**
* sets the minimum speed of this vehicle.
* @param minSpeed the minimum speed of this vehicle.
*/
public void setMinSpeed(float minSpeed) {
this.minSpeed = minSpeed;
}
/**
* brake adjusts the velocity of the vehicle based on the braking speed. If the
* velocity reaches 0, braking will put the vehicle in reverse up to the minimum
* speed.
* @param time the time between frames.
*/
public void brake(float time) {
velocity -= time * braking;
if(velocity < -minSpeed) {
velocity = -minSpeed;
}
}
/**
* accelerate adjusts the velocity of the vehicle based on the acceleration. The velocity
* will continue to raise until maxSpeed is reached, at which point it will stop.
* @param time the time between frames.
*/
public void accelerate(float time) {
velocity += time * acceleration;
if(velocity > maxSpeed) {
velocity = maxSpeed;
}
}
/**
* drift calculates what happens when the vehicle is neither braking or accelerating.
* The vehicle will slow down based on its weight.
* @param time the time between frames.
*/
public void drift(float time) {
if(velocity < -FastMath.FLT_EPSILON) {
velocity += ((weight/5) * time);
//we are drifting to a stop, so we shouldn't go
//above 0
if(velocity > 0) {
velocity = 0;
}
} else if(velocity > FastMath.FLT_EPSILON){
velocity -= ((weight/5) * time);
//we are drifting to a stop, so we shouldn't go
//below 0
if(velocity < 0) {
velocity = 0;
}
}
}
}