<commit_before>package;

import nape.space.Space;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Circle;
import nape.shape.Polygon;
import nape.geom.Vec2;
import nape.util.BitmapDebug;
import nape.dynamics.InteractionFilter;
import nape.phys.Compound;
import nape.callbacks.CbType;
import nape.callbacks.CbEvent;
import nape.callbacks.InteractionType;
import nape.callbacks.InteractionListener;
import nape.util.Debug;
import nape.constraint.MotorJoint;
import nape.constraint.PivotJoint;

import FixedStep;

class Viewport extends FixedStep {
	static function main() new Viewport()
	function new() {
		super(1/60);

		var space = new Space(new Vec2(0,0));
		var debug = new BitmapDebug(stage.stageWidth, stage.stageHeight, 0x333333);
		addChild(debug.display);

		//=================================================================================
		//viewport compound (we can have multiple 'cameras')
		var viewport = new Compound();
		var VIEWPORT = new CbType();
		viewport.cbTypes.add(VIEWPORT);
		viewport.space = space;

		//objects we want to interact with viewport
		var VIEWABLE = new CbType();

		//called when a body has entered any part of viewport.
		space.listeners.add(
		new InteractionListener(CbEvent.BEGIN, InteractionType.SENSOR, VIEWPORT, VIEWABLE, function (cb) {
			var new_body = cb.int2.castBody;
			new_body.graphic.alpha = 1.0;
		}));

		//called when a body has left any part of viewport.
		space.listeners.add(
		new InteractionListener(CbEvent.END, InteractionType.SENSOR, VIEWPORT, VIEWABLE, function (cb) {
			var old_body = cb.int2.castBody;
			old_body.graphic.alpha = 0.25;
		}));

		//------------------------------------------------------
		//first camera.
		var camera = new Body();
		camera.position.setxy(150,100);
		camera.shapes.add(new Polygon(Polygon.box(200,150)));
		camera.setShapeFilters(new InteractionFilter(0,0,-1,1,0,0)); //only sense with everything!
		camera.compound = viewport;

		function apply_friction(camera:Body) {
			//abuse PivotJoint + MotorJoint for friction
			var friction = new PivotJoint(space.world, camera, new Vec2(), new Vec2());
			friction.compound = viewport;
			friction.stiff = false;
			friction.maxError = 0;
			friction.maxForce = 1e+6;

			var angfric = new MotorJoint(space.world, camera, 0);
			angfric.compound = viewport;
			angfric.maxForce = 1e+7;
		}
		apply_friction(camera);

		//------------------------------------------------------
		//second camera

		var camera = new Body();
		camera.position.setxy(450,300);
		camera.shapes.add(new Polygon(Polygon.regular(200,200,5)));
		camera.setShapeFilters(new InteractionFilter(0,0,-1,1,0,0)); //only sense with everything!
		camera.compound = viewport;

		apply_friction(camera);

		//=================================================================================
		//border body
		
		var border = new Body(BodyType.STATIC);
		border.shapes.add(new Polygon(Polygon.rect(0,0,-50,stage.stageHeight)));
		border.shapes.add(new Polygon(Polygon.rect(stage.stageWidth,0,50,stage.stageHeight)));
		border.shapes.add(new Polygon(Polygon.rect(0,0,stage.stageWidth,-50)));
		border.shapes.add(new Polygon(Polygon.rect(0,stage.stageHeight,stage.stageWidth,50)));
		border.space = space;

		//=================================================================================
		//viewable bodies

		for(i in 0...100) {
			var b = new Body();
			b.position.setxy(Math.random()*800,Math.random()*600);
			b.shapes.add(new Polygon(Polygon.regular(Math.random()*20+20,Math.random()*20+20,Std.int(Math.random()*2)+3)));
			b.space = space;
			b.cbTypes.add(VIEWABLE);

			b.graphic = Debug.createGraphic(b);
			addChild(b.graphic);
			b.graphic.alpha = 0.25;<commit_msg>update viewport demo to use ANY_BODY<commit_after>package;

import nape.space.Space;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Circle;
import nape.shape.Polygon;
import nape.geom.Vec2;
import nape.util.BitmapDebug;
import nape.dynamics.InteractionFilter;
import nape.phys.Compound;
import nape.callbacks.CbType;
import nape.callbacks.CbEvent;
import nape.callbacks.InteractionType;
import nape.callbacks.InteractionListener;
import nape.util.Debug;
import nape.callbacks.OptionType;
import nape.constraint.MotorJoint;
import nape.constraint.PivotJoint;

import FixedStep;

class Viewport extends FixedStep {
	static function main() new Viewport()
	function new() {
		super(1/60);

		var space = new Space(new Vec2(0,0));
		var debug = new BitmapDebug(stage.stageWidth, stage.stageHeight, 0x333333);
		addChild(debug.display);

		//=================================================================================
		//viewport compound (we can have multiple 'cameras')
		var viewport = new Compound();
		var VIEWPORT = new CbType();
		viewport.cbTypes.add(VIEWPORT);
		viewport.space = space;

		//called when a body has entered any part of viewport.
		space.listeners.add(
		new InteractionListener(CbEvent.BEGIN, InteractionType.SENSOR, VIEWPORT, OptionType.ANY_BODY, function (cb) {
			var new_body = cb.int2.castBody;
			if(new_body.compound==viewport) return; //ignore viewport bodies.

			new_body.graphic.alpha = 1.0;
		}));

		//called when a body has left any part of viewport.
		space.listeners.add(
		new InteractionListener(CbEvent.END, InteractionType.SENSOR, VIEWPORT, OptionType.ANY_BODY, function (cb) {
			var old_body = cb.int2.castBody;
			if(old_body.compound==viewport) return; //ignore viewport bodies.

			old_body.graphic.alpha = 0.25;
		}));

		//------------------------------------------------------
		//first camera.
		var camera = new Body();
		camera.position.setxy(150,100);
		camera.shapes.add(new Polygon(Polygon.box(200,150)));
		camera.setShapeFilters(new InteractionFilter(0,0,-1,1,0,0)); //only sense with everything!
		camera.compound = viewport;

		function apply_friction(camera:Body) {
			//abuse PivotJoint + MotorJoint for friction
			var friction = new PivotJoint(space.world, camera, new Vec2(), new Vec2());
			friction.compound = viewport;
			friction.stiff = false;
			friction.maxError = 0;
			friction.maxForce = 1e+6;

			var angfric = new MotorJoint(space.world, camera, 0);
			angfric.compound = viewport;
			angfric.maxForce = 1e+7;
		}
		apply_friction(camera);

		//------------------------------------------------------
		//second camera

		var camera = new Body();
		camera.position.setxy(450,300);
		camera.shapes.add(new Polygon(Polygon.regular(200,200,5)));
		camera.setShapeFilters(new InteractionFilter(0,0,-1,1,0,0)); //only sense with everything!
		camera.compound = viewport;

		apply_friction(camera);

		//=================================================================================
		//border body
		
		var border = new Body(BodyType.STATIC);
		border.shapes.add(new Polygon(Polygon.rect(0,0,-50,stage.stageHeight)));
		border.shapes.add(new Polygon(Polygon.rect(stage.stageWidth,0,50,stage.stageHeight)));
		border.shapes.add(new Polygon(Polygon.rect(0,0,stage.stageWidth,-50)));
		border.shapes.add(new Polygon(Polygon.rect(0,stage.stageHeight,stage.stageWidth,50)));
		border.space = space;

		//=================================================================================
		//viewable bodies

		for(i in 0...100) {
			var b = new Body();
			b.position.setxy(Math.random()*800,Math.random()*600);
			b.shapes.add(new Polygon(Polygon.regular(Math.random()*20+20,Math.random()*20+20,Std.int(Math.random()*2)+3)));
			b.space = space;

			b.graphic = Debug.createGraphic(b);
			addChild(b.graphic);
			b.graphic.alpha = 0.25;
{'fraction_non_alphanumeric': 0.21815772034459907, 'fraction_numerical': 0.02730284956925116, 'mean_word_length': 4.088334457181389, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5969988', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2428, 'n_words': 484}