Jump to content

Random movement with maths in javascript


Heppell08
 Share

Recommended Posts

Hey everyone, this is my first post in the coding section. I'm normally rather active in the phaser section. But back to the point in question. I've been coding quite a lot recently on my current project and I'm loving it. What's bothering me is making the enemy AI have some sort of random but within certain areas not too random type of movement. What I'm mainly looking for is a type of left and right movement that is if the enemy walks so many X left or x right. I have an idea but sometimes when I'm coding I feel like I go the long way around it when there is simpler methods to it. Like for example, can the enemy be told how many X left or right it can go before turning around and walking in the opposite direction. I have my enemy coded to walk towards the player, that's about it up to now. I can make the enemy walk opposite to the right if the enemy gets within so many X of the player. Any ideas on this is greatly appreciated! Thanks for any and all responses :)

Link to comment
Share on other sites

  • 2 weeks later...

Hello, you could use Steering Behavior from Programming Game AI by Example. The Wander behaviors and other and combine them. We develop a game that we have an fly insect that flying around a certain area, and user wander behaviors. Ofcourse simplify all process, adding force and all the rest, only use the movement. View my "Super Bola and Tasilany" topic, and look the game Tasilany on the main menu.

 

 

post-5903-0-73171600-1391615795.png

Link to comment
Share on other sites

You can use Math.cos to control horizontal oscillation around a point, and Math.sin to do the same on a vertical axis. If you give each of these 3 randomly chosen input values that control amplitude, start value and rate of increment then you get quite a nicely random seeming effect. If you make this into an oscillator object with an update method and public x & y values then you have the option of chaining two or more together

 

This code is untested and probably full of typos, but may give you the idea of what I'm on about...

var startX = 10;var startY = 10;var oscillator1 = createOscillator();var oscillator2 = createOscillator();function Oscillator(amp, startTime, inc){  this.amp = amp;  this.x = 0;  this.y  = 0;  this.inc = inc;  this.time = startTime;  this.update = function(deltaTime){     this.time += deltaTime;     this.x = Math.cos(this.time) * this.amp;     this.y = Math.sin(this.time) * this.amp;  }}function createOscillator(){  var randomAmplitude = 10 + (Math.random() * 50);  var randomStartTime = Math.random() * 100;  var randomInc = 1 + (Math.random() * 5);  var osc = new Oscillator(randomAmplitude, randomStartTime, randomInc );  return osc;}function update(deltaTime){  var newX = startX;  var newY = startY;  oscillator1.update(deltaTime);  oscillator2.update(deltaTime);​  newX + oscillator1.x;  newY + oscillator1.y;  newX + oscillator2.x;  newY + oscillator2.y;  //  enemyPlayer.x = newX;  enemyPlayer.y = newY;}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...