Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperLicht
Friendly WSAD Key JS Modification for Tutorial2

kkelly
Registered User
Quote
2011-01-19 07:07:39

I discovered Copperlicht yesterday and thought the movement in the tutorials could be helped with a little easier WSAD rather than mouselook and modified the Javascript to allow friendlier movement.

This Javascript is modified from the JS out of Tutorial2. Post size restrictions made me remove comments.

Hope someone may find this useful.

W / S Forward/Back
A / D Turn Left / Right

Replace the Javascript in tutorial2 to use it.

Remove all ***'s before use

[code]

<script type="text/javascript">
<!--
var engine = startCopperLichtFromFile('3darea', 'copperlichtdata/index.ccbjs');
var cubeSceneNode = null;
var rootSceneNode = null;
var animator = null;
var cam = null;
var angle = 0;
var deltaX = 1;
var deltaY = 1;
var twoPI = Math.PI * 2;
var cam_x = 500;
var cam_z = 0;

engine.On***LoadingComplete = function()
{
var scene = engine.getScene();
if (scene)
{
cam = new CL3D.CameraSceneNode();
cam.Pos.X = cam_x;
cam.Pos.Z = cam_z;

scene.setRedrawMode(CL3D.Scene.REDRAW_EVERY_FRAME);

animator = new CL3D.AnimatorCameraFPS(cam, engine);
cam.addAnimator(animator);
animator.lookAt(new CL3D.Vect3d(1000,0,0));

scene.getRootSceneNode().addChild(cam);
scene.setActiveCamera(cam);

rootSceneNode = scene.getRootSceneNode();
rootSceneNode.Pos.X = cam_x;
rootSceneNode.Pos.Z = cam_z;
cubeSceneNode = scene.getSceneNodeFromName('cubeMesh1');
scene.setRedrawMode(CL3D.Scene.REDRAW_EVERY_FRAME);

var sphereSceneNode = scene.getSceneNodeFromName('sphereMesh1');
if (sphereSceneNode)
sphereSceneNode.addAnimator(new CL3D.AnimatorRotation(new CL3D.Vect3d(0, 1.6, 0.8)));
}
}

document.on***key***down = function(event)
{ = function(event)
{
var key = String.fromCharCode(event.keyCode);

// when pressed 'L', move the cube scene node a bit up
if (key == 'F' && cubeSceneNode)
cubeSceneNode.Pos.Y += 5;

// when pressed 'G', move the cube scene node a bit down
if (key == 'G' && cubeSceneNode)
cubeSceneNode.Pos.Y -= 5;

if (key == 'W')
{
rootSceneNode.Pos.X = cam_x = cam_x - Math.cos(angle) * 3;
rootSceneNode.Pos.Z = cam_z = cam_z - Math.sin(angle) * 3;
}

if (key == 'S')
{
rootSceneNode.Pos.X = cam_x = cam_x + Math.cos(angle) * 3;
rootSceneNode.Pos.Z = cam_z = cam_z + Math.sin(angle) * 3;
}

if (key == 'D')
{
angle -= 0.1;
deltaX = 3000 * Math.cos (angle);
deltaY = 3000 * Math.sin (angle);
animator.lookAt(new CL3D.Vect3d(deltaX*10000,0,deltaY*10000));
}

if (key == 'A')
{
angle += 0.1;
deltaX = 3000 * Math.cos (angle);
deltaY = 3000 * Math.sin (angle);
animator.lookAt(new CL3D.Vect3d(deltaX*10000,0,deltaY*10000));
}

if(angle > twoPI)angle -= twoPI;
if(angle < 0)angle += twoPI;

//engine.handleKeyDown(event);
};
-->
</script>

[/


niko
Moderator
Quote
2011-01-19 08:29:41

Thanks for posting! I guess I need to update the forum software, parts of your code have been removed obviously.


kkelly
Registered User
Quote
2011-01-19 17:40:33

Eep Forum seems to remove keywords. I added *** in between keywords


kkelly
Registered User
Quote
2011-01-19 23:48:28

I was working on this again today after downloading the CL latest update and modified CL itself to give me the keyboard movements I was looking for.

I use a laptop almost 100% of the time for my development work so using "mouselook" doesn't work for me very well.

In the function,


"CL3D.AnimatorCameraFPS.prototype.animateNode=function(e,d)"


I removed,


if(this.MayMove&&(this.leftKeyDown||this.rightKeyDown))
{
var a=this.Camera.Pos.substract(this.Camera.getTarget()).crossProduct(this.Camera.getUpVector());
a.normalize();
if(this.leftKeyDown)
{
a=a.multiplyWithScal(this.MoveSpeed*-b);
m.addToThis(a);
this.Camera.setTarget(this.Camera.getTarget().add(a))
}
if(this.rightKeyDown){
a=a.multiplyWithScal(this.MoveSpeed*b);
m.addToThis(a);
this.Camera.setTarget(this.Camera.getTarget().add(a))
}
}


and replaced it with


if(this.MayMove&&(this.leftKeyDown||this.rightKeyDown))
{
if(this.leftKeyDown)
{
h = -this.RotateSpeed;
}
else if(this.rightKeyDown)
{
h = this.RotateSpeed;
}
}


down below


if(h<-g)
{
h=-g
}


leaving the final results to be set on the bottom by


this.relativeRotationY+=h*(b*(this.RotateSpeed*p));
this.Camera.setTarget(this.Camera.Pos.add(l));


I'll add a setter to simply apply this to a prototype later on rather than messing with copperlicht.js itself, but just wanted to share the results.

Thanks for making CooperLicht, it's an excellent work.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact