Jump to content

  • Log In with Google      Sign In   
  • Create Account


BUnzaga

Member Since 13 Aug 2006
Offline Last Active Sep 26 2014 10:47 AM

Posts I've Made

In Topic: Ammo.js Collision Separation

26 September 2014 - 08:58 AM

Well I went with what I posted above, only instead of handling the callback during the collision iteration, I handle it afterward, because removing the rigidbody in the middle of iterating them was causing the browser to lock up biggrin.png

 

Here is what I ended up with for those of you who are interested in Ammo.js and this topic.  Please, if someone knows a better way, let me know.

 

Demo: https://goote.ch/d3a10e52808241ada183928d966fadd8.project (webGL).

 

Code:

for(var key in AmmoUtil.collision){
	if(AmmoUtil.collision.hasOwnProperty(key)){
		AmmoUtil.collision[key].separated ++;
	}
}
		
var dp = this.dispatcher;
for(var i = 0, ilen = dp.getNumManifolds(); i < ilen; i++){
	var manifold = dp.getManifoldByIndexInternal(i);
	var num_contacts = manifold.getNumContacts();
    if(num_contacts === 0){
		continue;
	}
    var bodyA = AmmoUtil.rigidBodies[manifold.getBody0()];
	var bodyB = AmmoUtil.rigidBodies[manifold.getBody1()];

	for (var j = 0; j < num_contacts; j++){
		var pt = manifold.getContactPoint(j);
		if(pt.getDistance() < 0.0){
			var colID = bodyA.entity.id+"_"+bodyB.entity.id;
			if(AmmoUtil.collision[colID] === undefined){
				pt.getPositionWorldOnA(pvec);
    				pt.getPositionWorldOnB(pvec2);
				var normalOnB = pt.get_m_normalWorldOnB();
				var pointOnA = new goo.Vector3(pvec.x(), pvec.y(), pvec.z());
				var pointOnB = new goo.Vector3(pvec2.x(), pvec2.y(), pvec2.z());
				var normalOnB = new goo.Vector3(normalOnB.x(), normalOnB.y(), normalOnB.z());
				
				var info = {
					first:true,
					entityA:bodyA.entity,
					entityB:bodyB.entity,
					dataA:{other:bodyB.entity, pointA:pointOnA, pointB:pointOnB, normal:normalOnB},
					dataB:{other:bodyA.entity, pointA:pointOnB, pointB:pointOnA, normal:normalOnB}};
				
				AmmoUtil.collision[colID] = info;
			}
			AmmoUtil.collision[colID].separated = 0;
			break;
		}
	}
}

for(var key in AmmoUtil.collision){
	if(AmmoUtil.collision.hasOwnProperty(key)){
		if(true === AmmoUtil.collision[key].first){
			AmmoUtil.collision[key].first = false;
			var entA = AmmoUtil.collision[key].entityA;
			if(entA){
				var rbc = entA.getComponent('RigidBodyComponent');
				if(rbc){
					if(rbc.collisionBegin){
						rbc.collisionBegin(AmmoUtil.collision[key].dataA);
					}
				}
			}
			var entB = AmmoUtil.collision[key].entityB;
			if(entB){
				var rbc = entB.getComponent('RigidBodyComponent');
				if(rbc){
					if(rbc.collisionBegin){
						rbc.collisionBegin(AmmoUtil.collision[key].dataB);
					}
				}
			}
		}
		else{
			if(AmmoUtil.collision[key].separated > 1){
				var entA = AmmoUtil.collision[key].entityA;
				var entB = AmmoUtil.collision[key].entityB;
				if(entA){
					var rbc = entA.getComponent("RigidBodyComponent");
					if(rbc){
						if(rbc.collisionEnd){
							rbc.collisionEnd(entB);
						}
					}
				}
				if(entB){
					var rbc = entB.getComponent("RigidBodyComponent");
					if(rbc){
						if(rbc.collisionEnd){
							rbc.collisionEnd(entA);
						}
					}
				}
				delete AmmoUtil.collision[key];
			}
		}
	}
}

In Topic: 'Fixed Timestep' wrap around problem

08 May 2013 - 07:40 AM

....teleport both the new position and the old one to the other side of the screen. In this way the ship seem to enter from the edge. Alternatively you can implement the wraparound logic after the interpolation step, i.e. storing an "absolute" ship position and then computing the modulus when computing the ship position on the screen

 

I currently have implemented the first suggestion, but would like more information on how I would go about doing your second suggestion properly. biggrin.png  I don't expect you guys to do the work for me, but just a keyword or two to look up in google would be highly appreciated.

 

So in my demo, I have 'previous', 'current', and 'rendered'(a better word would be interpolated).  In your second idea, I would use the interpolated position AFTER the physics calculation, not DURING it.  I like how that sounds.  Something like...

 

 

 while (accumulated >= TIC) {
  interpolated = oldPos = newPos
  newPos += TIC * Velocity
  accumulated -= TIC
 }
 ticLeft = accumulated / TIC

 smooth = 1.0 - ticLeft
 interpolated = (newPos * ticLeft) + (oldPos * smooth)
// handle the wrap around logic here?
// if interpolated > screen extents, set interpolated & oldPos & newPos to the teleported position?

In Topic: 'Fixed Timestep' wrap around problem

04 May 2013 - 09:08 PM

Hey thank you for the reply Paradigm Shifter.  Yes I wanted a similar effect, such as in Asteroids, only I don't need it to show the remaining ship as it wraps.  Just a simple...

 if > screen width, put back to 0.  The problem is that due to the interpolation, you can actually SEE the re-positioning, rather than it just appearing instantly on the other side.

 

In my fixed update, it just checks...

 

if arrowPos.x > screen width, set it to 0.  If arrowPos.x < 0 set it to screen width.

 

I am new to using an interpolation such as this, so I haven't had to deal with this sort of behavior before.

 

When the ship gets to the screen width, do I set ALL of them to 0?  Current Position, Previous Position, and Rendered(interpolated) Position?


In Topic: I want your opinions on Mobile controls and general game play.

02 February 2013 - 03:49 AM

Thank you for the feedback guys.

 

I did want to do something like a rogue-like, so I'll check it out for sure.


In Topic: Javascript - Better to instantiate variables, or leave them undefined?

15 October 2012 - 07:25 AM

Thanks slicer4ever, that is a good idea about setting them to null for error checking.

PARTNERS