Commit 111b38fd authored by Gaurav Arora (gaa721)'s avatar Gaurav Arora (gaa721)
Browse files

Complete

parent 81cbb35a
0.png

2.9 KB

1.png

2.82 KB

10.png

2.75 KB

11.png

2.6 KB

2.png

2.81 KB

3.png

2.82 KB

4.png

2.84 KB

5.png

2.81 KB

6.png

2.76 KB

7.png

2.62 KB

8.png

2.74 KB

9.png

2.84 KB

File added
fish.gif

240 KB

function createFish(x,y,angle,id){
this.id=id;
this.x = x,
this.y = y ,
this.angle=radians(angle),
this.vangle=0,
this.fin=0,
this.alive=true;
this.velocity=0,
this.prevFin=0,
this.vx=0;
this.k=0,
this.vy=0;
this.polySd = new b2BodyDef();
this.polySd.type = b2Body.b2_dynamicBody;
this.polySd.position.Set(this.x,this.y);
// this.polySd.angle=angle;
this.body = world.CreateBody(this.polySd);
// var dynamicBox = new b2CircleShape(15);
var dynamicBox = new b2PolygonShape();
dynamicBox.SetAsArray(pointsA,pointsA.length);
// dynamicBox.SetAsBox(25,90);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density = 0.8;
fixtureDef.friction = 0.5;
this.body.CreateFixture(fixtureDef);
this.body.SetAngle(radians(angle));
this.body.SetUserData(this.id);
this.body.SetLinearDamping(0.4);
}
createFish.prototype.moveBoat= function(gp)
{
// find right oar
var tangle;
//find left oar
text(gp.axes[0]+" "+gp.axes[1],50,180);
if((gp.axes[0])>0.8 || abs(gp.axes[1])>0.8)
{
tangle=atan2(gp.axes[1],-gp.axes[0]);
if(tangle>0)
this.k=0;
else this.k=11;
if(tangle.toString()!="NaN")
{
this.angle=tangle;
this.body.SetAngularVelocity(tangle);
}
// this.add_velocityang(-this.angle/4);
// this.body.SetPositionAndAngle(new b2Vec2(this.x,this.y),this.angle);
}
text(degrees(this.angle),50,210);
// gp.axes[0];// - 100;
this.fin = 100 * gp.axes[3];
// find right oar
// this.rightOarX = 80 * gp.axes[3] + 100;
// calculate movement
dRight = -abs(-this.prevFin + this.fin);
// var v1=createVector(this.prevFin,119);
// var v2=createVector(this.fin,119);
// var temp=p5.Vector.angleBetween(v2,v1); // store the angle to be changed by boat
// if(temp.toString()!="NaN")
// {
// if(this.prevFin>50)
// {
// (this.angle)+=temp;
// }
// if(this.prevFin<-50)
// {
// (this.angle)-=temp;
// }
// }
// text(this.prevFin ,50,50);
// text(this.fin ,50,100);
// dDown = this.prevRightOarX - this.rightOarX;
// var v1=createVector(this.prevRightOarX,this.prevRightOarY);
// var v2=createVector(this.rightOarX,this.rightOarY);
// var temp=p5.Vector.angleBetween(v1,v2);
// if(temp.toString()!="NaN")
// {
// if(this.prevRightOarY >this.rightOarY)
// {
// (this.angle)-=temp;
// }
// else
// {
// (this.angle)+=temp;
// }
// }
this.velocity += dRight/100 ;
// this.velocity *=damping;
// this.prevLeftOarY = this.leftOarY;
this.prevFin = this.fin;
// this.prevRightOarX = this.rightOarX
// this.prevLeftOarX = this.leftOarX
this.x=this.body.GetPosition().x;
this.y=this.body.GetPosition().y;
this.vangle=this.body.GetAngle();
text(this.x+" " +this.y+" "+this.angle+" ",50,150);
this.velocity *= damping;
//moving=velocityY;
this.vy = ((this.velocity*(cos((this.vangle)))));
this.vx = ((this.velocity*(sin((-this.vangle)))));
this.add_velocity(new b2Vec2(this.vx*25,this.vy*25));
//y+=sSpeed;
// this.y+=this.velocity ;
if(gp.buttons[1].pressed)
this.add_velocity(new b2Vec2(this.vx*50,this.vy*50));
}
createFish.prototype.drawBoat= function(yo)
{
// push();
// translate(width/2,height/2);
// beginShape();
// for(var i=0;i<points1.length;i++)
// vertex(points1[i][0],points1[i][1]);
// vertex(points1[0][0],points1[0][1]);
// endShape(CLOSE);
// pop();
// push();
// translate(width/2,height/2);
// beginShape();
// for(var i=0;i<points2.length;i++)
// vertex(points2[i][0],points2[i][1]);
// vertex(points2[0][0],points2[0][1]);
// endShape(CLOSE);
// pop();
// push();
// translate(width/2,height/2);
// beginShape();
// for(var i=0;i<points3.length;i++)
// vertex(points3[i][0],points3[i][1]);
// vertex(points3[0][0],points3[0][1]);
// endShape(CLOSE);
// pop();
// push();
// translate(width/2,height/2);
// beginShape();
// for(var i=0;i<points4.length;i++)
// vertex(points4[i][0],points4[i][1]);
// vertex(points4[0][0],points4[0][1]);
// endShape(CLOSE);
// pop();
push();
// text(this.body.GetLinearVelocity().y.toFixed(0),50,500+this.id*100);
text(this.velocity.toFixed(3),50,500+this.id*100);
translate(this.x,this.y);
rotate(this.vangle);
// if((yo/20)>=abs(0.6))
if(abs(this.velocity)>=0.02)
this.k++;
if(this.k==12)
this.k=0;
// k=abs((yo*10000)%11).toFixed(0);
// text(this.k,0,i*200);
// ellipse(0,0,30,80);
// ellipse(points[4][0],points[4][1],10,10);
// rectMode(CENTER);
// rect(0,0,50,180);
// for(var i=0;i<12;i++)
image(fimg[this.k],-60,-90,fimg[this.k].width*0.5,fimg[this.k].height*0.5);
text(this.id+1,-15,0);
// beginShape();
// for(var i=0;i<points.length;i++)
// vertex(points[i][0],points[i][1]);
// vertex(points[0][0],points[0][1]);
// endShape();
push();
// triangle(0,39 ,yo,59+50, yo,19+20);
pop();
// rotate(-(this.angle)/4); //rotating the boat
// image(bimg, (-95)*smallFactor,-170*smallFactor, bimg.width*smallFactor, bimg.height*smallFactor);
// bimg.resize(527/smallFactor,336/smallFactor);
// pop();
// push();
// translate(this.x,(-viewYU+this.y)*smallFactor);
// rotate(-(this.angle)/4);
// strokeWeight(5);
// if (this.leftOarX*smallFactor > -100*smallFactor) {
// stroke(100);
// this.leftIn = false;
// } else {
// stroke(0);
// this.leftIn = true;
// }
// line(-75*smallFactor, 0, this.leftOarX*smallFactor, this.leftOarY*smallFactor);
// if (this.rightOarX*smallFactor < 101*smallFactor) {
// stroke(100);
// this.rightIn = false;
// } else {
// stroke(0);
// this.rightIn = true;
// }
// line(40*smallFactor, 0, this.rightOarX*smallFactor, this.rightOarY*smallFactor);
pop();
// this.body.SetLinearVelocity(new b2Vec2(this.body.GetLinearVelocity().x*0.0001,this.body.GetLinearVelocity().y*0.0001));
this.body.SetAngularVelocity(0);
}
createFish.prototype.add_velocity = function(vel)
{
var b = this.body;
var v = b.GetLinearVelocity();
v.Add(vel);
//check for max horizontal and vertical velocities and then set
// if(Math.abs(v.y) > this.max_ver_vel)
// {
// v.y = this.max_ver_vel * v.y/Math.abs(v.y);
// }
// if(Math.abs(v.x) > this.max_hor_vel)
// {
// v.x = this.max_hor_vel * v.x/Math.abs(v.x);
// }
//set the new velocity
b.SetLinearVelocity(v);
// b.ApplyForce(v,b.GetPosition());
// b.ApplyImpulse(v,b.GetPosition());
}
createFish.prototype.add_velocityang= function(x){
var body = this.body;
var v =body.GetLinearVelocity();
var bodyAngle = body.GetAngle();
var desiredAngle=x;
var nextAngle = bodyAngle + body.GetAngularVelocity() / 60.0;
var totalRotation = desiredAngle - nextAngle;
body.SetAngularVelocity(((v.Length())*(tan(totalRotation)))/15);
}
fish.png

74 KB

fish1.png

73.2 KB

var haveEvents = 'GamepadEvent' in window;
var controllers = {};
console.log("start script");
function connecthandler(e) {
console.log("Start add gamepad");
addgamepad(e.gamepad);
console.log("Added gamepad");
}
function addgamepad(gamepad) {
console.log("Gamepad index = " + gamepad.index);
controllers[gamepad.index] = gamepad;
console.log(gamepad.axes);
console.log("Finished add gamepad");
console.log("Controllers = " + controllers[0]);
}
function disconnecthandler(e) {
removegamepad(e.gamepad);
}
function removegamepad(gamepad) {
delete controllers[gamepad.index];
console.log("Finished remove gamepad");
}
function scangamepads() {
console.log("start scan");
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
for (var i = 0; i < gamepads.length; i++) {
if (gamepads[i]) {
if (!(gamepads[i].index in controllers)) {
addgamepad(gamepads[i]);
} else {
controllers[gamepads[i].index] = gamepads[i];
}
}
}
console.log("finished scan");
}
console.log("end script 1");
if (haveEvents) {
window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);
} else {
scangamepads();
setInterval(scangamepads, 500);
}
console.log("end script");
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="lib/prototype-1.6.0.2.js"></script>
<!-- box2djs -->
<script src="javascript/misc/extend.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2BoundValues.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2PairManager.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2TimeStep.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2Controller.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2GravityController.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2DestructionListener.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactEdge.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2EdgeChainDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Vec2.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Vec3.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DistanceProxy.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactFactory.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2ConstantAccelController.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2SeparationFunction.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DynamicTreePair.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactConstraintPoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2ControllerEdge.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DistanceInput.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/b2Settings.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Proxy.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Point.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2WorldManifold.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2RayCastOutput.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2ConstantForceController.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2MassData.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DynamicTree.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2JointEdge.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2RayCastInput.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Features.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2FilterData.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2AABB.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2Jacobian.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Bound.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2SimplexVertex.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Mat22.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2SimplexCache.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2Shape.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Segment.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactRegister.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2DebugDraw.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Sweep.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DistanceOutput.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Mat33.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2PositionSolverManifold.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2OBB.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Pair.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2FixtureDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2ContactID.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Transform.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2EdgeShape.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2BuoyancyController.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2Body.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2ContactImpulse.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Controllers/b2TensorDampingController.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2ManifoldPoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2PolygonShape.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2Fixture.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DynamicTreeNode.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2BodyDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2DynamicTreeBroadPhase.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2BroadPhase.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Manifold.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/Shapes/b2CircleShape.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2Joint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2LineJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactSolver.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Simplex.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2WeldJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/Math/b2Math.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2PulleyJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2PrismaticJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2RevoluteJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2JointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2LineJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2DistanceJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2PulleyJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2DistanceJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2FrictionJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2WeldJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2GearJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Common/b2Color.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2FrictionJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Distance.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2MouseJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2PrismaticJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2TimeOfImpact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2GearJoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2TOIInput.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2RevoluteJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Joints/b2MouseJointDef.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2Contact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactConstraint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2ContactResult.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2PolygonContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/ClipVertex.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2ContactFilter.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2NullContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2ContactListener.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2Island.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2PolyAndEdgeContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2Collision.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2PolyAndCircleContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Collision/b2ContactPoint.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2CircleContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2ContactManager.js" type="text/javascript"></script>
<script src="javascript/Box2D/Dynamics/b2World.js" type="text/javascript"></script>
<script language="javascript" src="SAT.js"></script>
<script language="javascript" src="p5.js"></script>
<script src = "gamepad.js"></script>
<script language="javascript" src="touch.js"></script>
<script language="javascript" src="fish.js "></script>
<script language="javascript" src="scenario.js"></script>
<!-- uncomment lines below to include extra p5 libraries -->
<!--<script language="javascript" src="../addons/p5.dom.js"></script>-->
<!--<script language="javascript" src="../addons/p5.sound.js"></script>-->
<script>
</script>
<!-- <script language="javascript" src="scenario.js"></script>-->
<!-- this line removes any default padding and style. you might only need one of these values set. -->
<style>
body {
padding: 0;
margin: 0;
overflow: hidden
}
</style>
</head>
<body>
</body>
</html>
var ClipVertex = function() {
this.__varz();
this.__constructor.apply(this, arguments);
}
ClipVertex.prototype.__constructor = function(){}
ClipVertex.prototype.__varz = function(){
this.v = new b2Vec2();
this.id = new b2ContactID();
}
// static methods
// static attributes
// methods
ClipVertex.prototype.Set = function (other) {
this.v.SetV(other.v);
this.id.Set(other.id);
}
// attributes
ClipVertex.prototype.v = new b2Vec2();
ClipVertex.prototype.id = new b2ContactID();
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment