Ok, I am using structs to handle my animations. Right now I am unable to animate both my projectile and my character. As soon as I shoot my projectile my character won't move anymore.
I am using the same variables for character starting position and projectile starting position because I need the projectile to start at the character.
I am wondering on how to use the same variables, but separate them, so when the projectile's coordinates are altered... the characters are not.
I would recommend using separate variables to store the position of your character and your projectile, and initially setting them to the same value.
The following should be something like what you have now:
pos = (0, 0)
def animate():
pos = add(pos, (1, 1))
And the following is an example of using separate variables to store positions; it allows the character to take a path different from the projectile's.
characterposition = (0, 0)
projectileposition = (0, 0)
def animate():
characterposition = add(characterposition, (1, 0))
projectileposition = add(projectileposition, (1, 1))
If you absolutely must use the same variable for both of their positions, you will need to incorporate character and projectile position into that variable separately (i.e. pos = [[0, 0], [0, 0]]).
Related
I have an instance of Object3D and visually it's at (0, 0, 0) but its actual position is shifted (and positions of this object's internals are shifted as well). So, basically geometry of this object is shifted in such a way that it compensates for position shift. So, I want to keep it visuallyy at (0, 0, 0) and change its position to (0, 0, 0).
In simple words, I want to shift object's position vector to this object's geometry position.
How can I do it?
I can understand the explanation in tutorial 6, which is:
// Func gradient("gradient");
// Var x("x"), y("y");
// gradient(x, y) = x + y;
// gradient.realize(8, 8);
//
// This does three things internally:
// 1) Generates code than can evaluate gradient over an arbitrary
// rectangle.
// 2) Allocates a new 8 x 8 image.
// 3) Runs the generated code to evaluate gradient for all x, y
// from (0, 0) to (7, 7) and puts the result into the image.
// 4) Returns the new image as the result of the realize call.
However, follow the description, I can't figure it out how such a example works:
Func histogram("hist_serial");
histogram(i) = 0;
RDom r(0, input.width(), 0, input.height());
histogram(input(r.x, r.y) / 32) += 1;
histogram.vectorize(i, 8);
histogram.realize(8);
What I am confusing is: in the "gradient" example, evaluating gradient for all x, y from (0,0) to (7,7) can give us a result, like gradient(1,1)=1+1=2. But in the second example, evaluating histogram for i from 0 to 7 looks strange to me, as I think that we are trying to calculate the result from back to front. A more natural way is to evaluate the input first, then calculate histogram.
So, how the "realize" in the second example works?
Halide automatically infers all of the values which need to be computed to produce a requested region of output. realize just asks the pipeline to compute the requested region of the output Func(s). Halide then automatically infers what regions of which earlier Funcs are required, and recursively evaluates all of the those, up to the inputs, before producing the requested region of output.
I have solved an ODE in Julia describing the motion of a particle, and I have saved the coordinates and respective time in an array. I would like to create an animated gif image of a plot with the particle along the solved trajectory, but to do that (the only way that I have come up) is to plot the position of the particle using scatter, and erase the previous position of the particle every moment. However I only know about scatter! which will add more particles to the plot rather than showing the change of particle position. So how can I erase the previous plot every iteration, or are there more clever ways to do this? And what about if I want to mark down the trajectory of the particle in earlier moments using plots?
Erasing previous data is not possible with Plots.jl. The previous plot can be erased by using plot or scatter commands instead of plot! and scatter!. Here are some examples how animations can be created using the #gif macro (http://docs.juliaplots.org/latest/animations/)
Create some dummy data:
using Plots
t = range(0, 4π, length = 100)
r = range(1, 0, length = 100)
x = cos.(t) .* r
y = sin.(t) .* r
Plot only the last current point in each step:
#gif for i in eachindex(x)
scatter((x[i], y[i]), lims = (-1, 1), label = "")
end
Plot all previous steps with a marker at the current position:
#gif for i in eachindex(x)
plot(x[1:i], y[1:i], lims = (-1, 1), label = "")
scatter!((x[i], y[i]), color = 1, label = "")
end
The same as above with decreasing alpha for older steps (only showing the newest 10 steps):
#gif for i in eachindex(x)
plot(x[1:i], y[1:i], alpha = max.((1:i) .+ 10 .- i, 0) / 10, lims = (-1, 1), label = "")
scatter!((x[i], y[i]), color = 1, label = "")
end
Lets assume we have a zig zag line or something with multiple angles. How exactly do you catch if a point is touching the line? For example, lets assume I have a scenario like this: ]
The following python code works for me to compute the shortest distance from a point to a sequence of line segments:
from math import sqrt
def point_distance_to_line_segment((x,y),(lx1, ly1),(lx2, ly2)):
line_length = sqrt((lx1-lx2)**2+(ly1-ly2)**2)
dot_product = (x-lx1)*(lx2-lx1)+(y-ly1)*(ly2-ly1)
proj_line = dot_product/line_length
if proj_line < 0:
# close to (x1,y1)
return sqrt((x-lx1)**2+(y-ly1)**2)
elif proj_line > line_length:
# close to (x2,y2)
return sqrt((x-lx2)**2+(y-ly2)**2)
else:
# in the middle
cross_product = abs((x-lx1)*(ly2-ly1)-(y-ly1)*(lx2-lx1))
return cross_product/line_length
line_pts = [(-1, 0), (0, 1), (1, 0), (2,0)]
test_p = (-1, 0)
print min([point_distance_to_line_segment(test_p,lp1,lp2)
for (lp1,lp2) in zip(line_pts[0::2], line_pts[1::2])])
Not sure if there is a way to avoid iterating through all segments. Then you can simply compare this shortest distance to your circle radius.
I'm trying to replace a single line of code with another line in the same place but not sure where to put the (i-1) part back in...
In the actual code this tip is commented out below
-- in a real app you would use a smooth dot image, circles at this size look pixellated. I've already created the smooth dot image to use ("images/slidedot.png").
Here are the original code lines:
for i=1, pageTotal do
local dot = display.newCircle((i-1)*50, 0, 5)
dotGroup:insert(dot)
pageDots[i]=dot
end
I'm trying to replace display.newCircle((i-1)*50, 0, 5) with display.newImageRect ( "images/slidedot.png", 10, 10) at the line that starts as local dot =.
I'm just not sure where to put the (i-1) part back in...
So the final line should look like this: local dot = display.newImageRect ( "images/slidedot.png", 10, 10) with (i-1) somewhere.
display.newCircle((i-1)*50, 0, 5)
That line creates a circle with radius 5 centred at (i-1) * 50, 0.
ImageRect takes the width and the height as arguments, for the same size circle they are twice the radius (10). To set the centre point of the rectangle we can change the reference point to be the centre, and then set the x and y values to be the same as before.
local dot = display.newImageRect("images/sliderdot.png", 10, 10)
dot:setReferencePoint(display.CenterReferencePoint)
dot.x = (i - 1) * 50
dot.y = 0