Mason 入门例子3 --- 让学生动起来
来源:互联网 发布:看韩剧用什么软件 编辑:程序博客网 时间:2024/06/10 01:46
现在我们给学生增加 forceToSchoolMultiplier 和 randomMultiplier 两种力。
学生会有往操场中心行走的趋势和随机漫游的趋势。
package com.mason.learn;import sim.engine.*;import sim.util.*;import sim.field.continuous.*;public class Students extends SimState {private static final long serialVersionUID = 1L;public Continuous2D yard = new Continuous2D(1.0, 100, 100);public int numStudents = 50;double forceToSchoolMultiplier = 0.01;double randomMultiplier = 0.1;public Students(long seed) {super(seed);}public void start() {super.start();// clear the yardyard.clear();// add some students to the yardfor (int i = 0; i < numStudents; i++) {Student student = new Student();yard.setObjectLocation(student, new Double2D(yard.getWidth() * 0.5+ random.nextDouble() - 0.5, yard.getHeight() * 0.5+ random.nextDouble() - 0.5));schedule.scheduleRepeating(student);}}public static void main(String[] args) {doLoop(Students.class, args);System.exit(0);}}
学生类实现steppable接口,每个时间步长点会按照随机顺序调用所有Student的step方法。
package com.mason.learn;import sim.engine.*;import sim.field.continuous.Continuous2D;import sim.util.*;public class Student implements Steppable {private static final long serialVersionUID = 1L;@Overridepublic void step(SimState state) {Students students = (Students) state;Continuous2D yard = students.yard;Double2D me = students.yard.getObjectLocation(this);MutableDouble2D sumForces = new MutableDouble2D();// add in a vector to the "teacher" -- the center of the yard, so we// don’t go too far awaysumForces.addIn(new Double2D((yard.width * 0.5 - me.x)* students.forceToSchoolMultiplier, (yard.height * 0.5 - me.y)* students.forceToSchoolMultiplier));// add a bit of randomnesssumForces.addIn(new Double2D(students.randomMultiplier* (students.random.nextDouble() * 1.0 - 0.5),students.randomMultiplier* (students.random.nextDouble() * 1.0 - 0.5)));sumForces.addIn(me);students.yard.setObjectLocation(this, new Double2D(sumForces));}}
我们从State中抽取yard,然后找到当前Student对象的位置。
我们使用MutableDouble来计算综合的位移,最后设置Student的新位置。
0 0
- Mason 入门例子3 --- 让学生动起来
- Mason 入门例子2 --- 增加操场空间和静态学生
- Mason 入门例子5 --- 增加学生之间的联系
- Mason 入门例子4 --- 增加UI
- 让下划线动起来
- 让位图动起来!
- 让精灵动起来
- 让大脑动起来
- 让动画动起来
- 让cube动起来
- 让图片动起来
- 【 D3.js 入门系列 --- 6 】 如何让图表动起来
- 【 D3.js 入门系列 — 6 】 让图表动起来
- DEM:让ERP动起来
- 让照片真正动起来
- 让google map 动起来
- 线程------让画面动起来
- Flex 让组件动起来
- Java虚拟机学习之多态性实现机制——静态分派与动态分派
- Java 回顾笔记_集合框架之_linkedList_arrayList
- 你在编程的时候浪费了多少时间?
- App Components --2.0
- 【USACO题库】2.3.4 Money Systems货币系统
- Mason 入门例子3 --- 让学生动起来
- Android 大杂烩工程之ListView的开发详解
- javascript date类型用法
- 动态规划---01背包与记忆化搜索
- Guice学习之Bindings
- Howto: Backup and restore your system!
- UIScrollView的属性总结
- 第五周项目2-游戏中的角色类(1)
- Object-C基础(2)—— NSLog函数