How can I spawn multiples spheres at random positions [a-frame] - random

In aframe I want to spawn a sphere 100 times, each at a random position
First, each value in position= "x y z" should a number between -100 and 100
<a-sphere
position="x y z"
radius="0.5">
</a-sphere>
Second, the code above should be repeated 100 times, the leaving the previous sphere unaffected.
This is the desired end result, ignoring the motion.
How can this be achieved?

See:
http://ngokevin.github.io/kframe/components/entity-generator/
https://ngokevin.github.io/kframe/components/randomizer/
Uses entity-generator + randomizer components.
<a-mixin id="sphere" geometry="primitive: sphere" material random-position="min: -100 -100 -100; max: 100 100 100"></a-mixin>
<a-entity entity-generator="mixin: sphere; num: 100"></a-entity>

Related

Three.js - Inconsistency in updating plane size after its created has me confused

Im setting the height and width to 100 when setting the PlaneGemoetry. Then I am scaling to the exact same size later using plane.scale(100, 100, 1);. But the scaled plane is much bigger. Why is the exact same dimentions so diffirent? Am I doing something wrong, or is the scale values really completley diffirent?
My expectation is that when I set the scale to 100, 100 it should not change size because thats the same size I created it at.
var geometry = new THREE.PlaneGeometry(100, 100, 2, 2 ); //<--- sets scale here
var plane = new THREE.Mesh( geometry, material );
plane.scale.set( 100, 100, 1 ); //< -- why does 100, 100 suddenly turn into a completley
Heres a codepen for the issue:
https://codepen.io/carelesscourage/pen/zYLbZBv
Scaling is multiplicative. So your original size * scale is the equivalent of doing
100 * 100 = 10000
You’re getting a plane that’s ten thousand units wide. If you want to return to its initial size, just set the scale to 1, since 100 * 1 = 100

Transforming an image coordinate to circle coordinates

So I'm trying to transform an image coordinate which is the ordinary square x,y coordinate to a circular coordinate as shown below.
In order to do so, the center of the square image must be the origin which is 0 in the circular coordinate system.
In Matlab they have a function called 'cart2pol' where:
cart2pol(x,y)
However, the x,y argument are the circular coordinates hence before using cart2pol, how do i convert the ordinary square coordinate system to a circular one?
I think you should be able to use cart2pol(x,y), which gives you the polar (2d) or cylinder (3d) coordinates for some cartesian inputs x and y (and z for cylindrical).
Coordinates in your 1st image: i, j. Coordinates in your 2nd image: theta, rho.
N = 400; % example: 400x400 pixels
% shift origin into center
% Matlab uses 1 to N indexing not 0 to N-1
xo = (N)/2; % center of image
yo = (N)/2;
% Define some sample points in the image (coords from top-left of image)
% 0 deg, 90 deg, 180 deg, 270 deg
i = [350 200 50 200];
j = [200 1 200 350];
% get polar coordinates from cartesian ones (theta in radians)
% -(j-yo) due to opposite direction of j to mathematical positive direction of coord.system
[theta, rho] = cart2pol(i-xo, -(j-yo));
rho = rho/(N/2); % scaling for unit circle
theta is in the range -pi to pi, so if you need 0 to 2pi or 0 to 360 you still need to do a mapping.

Netlogo: calculate time using distance traveled and speed

I have a model where I want to measure the time it takes for a turtle to travel at a given speed from one point to another, using the scale of the map and speed/fd movement to quantify elapsed real time.
I have speed set to a random number (global variable input on UI) for all ships and a list for each turtle with the coordinates of the path. I use the formula of time = distance / speed but cannot figure out how to count time accurately.
The speed is set to a number around 30, which I would like to use as km/h. The scale of the map is 1000m x 1000m per patch so I have tried using factors of 1000 to fix the calculation to no avail. below is my calculation for time.
to move
tick
ask ships with [length current-path != 0]
[
let x1 xcor
let x2 [pxcor] of first current-path
let y1 ycor
let y2 [pycor] of first current-path
let distance-traveled sqrt ((x2 - x1)^ 2 + (y2 - y1)^ 2)
set time ((distance-traveled * meters-per-patch) / (speed)
set timelist lput time timelist
set totaltime sum timelist
go-to-next-patch-in-current-path
]
end
This potion calls the movement. I am divding speed/1000 since the number for speed is in the 10's and I want the turtles to move much less than 1 patch each tick to allow it to land directly on the patch coordinates in the list.
to go-to-next-patch-in-current-path
face first current-path
ifelse distance first current-path < .1
[
move-to first current-path
set current-path remove-item 0 current-path
]
[
fd speed / 1000
set heading towards first current-path
]
end
Any help would be greatly appreciated, thanks a lot.
Regardless. If you want someone to move from x1,y1 to x2,y2. You need to calculate the distance to x2 y2. Suppose you want it to take 4 seconds to move from x1,y1 to x2,y2
For a turtle:
on your setup or when you arrive at x1,y1
let t 3
let d distancexy x2 y2
let velocity d / t
facexy x2 y2
while you're not at x2,y2
fd velocity
I think your problem is at every iteration you're recalculating your velocity. You're saying if you're 5 meters away, and want to take 5 seconds, move 1 meter. Then in the next time unit, you'll be 4 meters away and want to take 5 seconds, move .8 meters. etc

understanding three.js ray projection

I want to understand how ray projection works. Almost where ever I see, I get a code like this:
new THREE.Vector3(mouseX, mouseY, 0.5);
I'm confused about the 0.5 in the z position. As per my understanding we are projecting on to z=1 plane (not z=0.5 plane). So shouldn't we be using it like this instead?
new THREE.Vector3(mouseX, mouseY, 1.0);
The pattern often used is like this:
var vector = new THREE.Vector3(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5,
);
This is a point in Normalized Device Coordinate (NDC) Space.
The function
projector.unprojectVector( vector, camera );
is mapping from NDC space to world space.
The value 0.5 for vector.z can be set to any value between -1 and 1.
Why? Because these points are all on a line parallel to the z-axis in NDC space, and will all map to the same ray emanating from the camera in world space.
Setting z = -1 will map to a point on the near plane; z = 1 the far plane.
So the short answer is, it doesn't matter what the value of z is, as long as it is between -1 and 1. For numerical reasons, we stay away from the endpoints, and 0.5 is often used.

circle - rectangle intersection

I have problem with circle-rectangle intersection.Though A number
of discussion i found about it ,i could not get my answer.My problem is -I have a rectangle lower portion(100-200,0-50) of my view/window(320 X 480).And a ball is moving here and there.And
sometimes it collides with the rectangle and bounce back.And my problem is how will i know in which axis circle collide with the
rectangle, in x-axis or y axis,means in which line(x=100 or x=200 or y==50) circle intersect with rectangle.
*Ball comes from outside of rectangle.
To see if it hits one of the lines full on is easy: just check for a collision between the bounding box of the circle and each of the lines in turn.
The problem is if it hits a corner... then you have to intersect the circle with each line. This can be done, but requires a bit more mathematics. It will also bounce off at an unusual angle.
Say the ball's center is moving along a time trajectory that can be described as x = a t + b and y = c t + d -- any linear, uniform-speed motion can be described this way. Since you say that it's initially (say at t=0) outside the rectangle, we know that at that time x < 100 or x > 200, or y < 0 or y > 50 (one of the conditions of x, and one of the conditions of y, can both be true, but at least one must be -- if they were all false we'd be inside the rectangle).
So check "at what time and exactly where will that point intersect each of the four lines that make up the rectangle"; i.e., solve for t when x = 100 (which gives t = (100 - b) / a, and therefore y = c (100 - b) / a + d), x = 200, y = 0, y = 50. Discard the solutions where t < 0 (those were things that happened in the past), as well as ones where the other variable falls outside of the rectangle's boundaries (for example, for the t = 100 case I just mentioned, you can ignore the apparent solution if (100 - b) / a < 0, or c (100 - b) / a + v < 0, or c (100 - b) / a + v > 50). If none of the four is left, this means the ball (with a radius of 0...) will not hit the rectangle along its current trajectory (it may if and when it bounces and thus changes trajectory, but those will be separate computations). If one or more are left, the one with the minimum value of t is the one you want. Once you know where and when the center would hit the rectangle, taking account of the radius can be done separately, but won't change the issue of which rectangle side the ball hits.
The cases where the ball "glances" (hits the rectangle just because it does have a radius greater than zero) are harder, but one approach is, if the normal computation shows the ball "not hitting", repeat it after shifting the ball (by the amount of its radius) to both side of the trajectory-line it's following -- this will tell you if the ball IS in fact going to hit, and, if so, which side (assuming hits on corners can be counted as hits on one of the sides converging on that corner;-).
How about:
Let centre of circle have coordinates cx, cy, radius cr.
if cx > 100 - cr and cx <= 100 and cy <= 50
collision with left upright
else if cy >= 50 and cy < 50 + cr and cx > 100 and cx < 200
collision with top
else if cx < 200 + cr and cx >= 200 and cy <= 50
collision with right upright
else if ( cx - 100 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
collision with top left corner
else if ( cx - 200 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
collision with top right corner
else
no collision
Corner collisions will need special treatment to work out how bounce will work based on exact point of contact and direction of travel. This also leaves a large part of the screen where collisions will not be detected (inside the rectangle), which I'm sure you could add to the above algorithm.
Doing a quick search seems to indicate that this problem has been asked before...
decrease rectange by size of radius on each side and move circle as point.

Resources