Jump to content

Collision Detection (Reposition Point) Code not working


RyanJP

Recommended Posts

Hi I wonder if someone can explain why the following code won't work? Basically just trying to take an emitted particle and reposition it if it overlaps with existing geo. The code runs fine but doesn't reposition the point at all, wondering why. Thanks!

v@ptemp = @P;
int npts[] = nearpoints(1, @ptemp, @pscale);
int lent = len(npts);
float seed = 1;

while(lent){
    @Cd = {1, 0, 0};
    @ptemp = set(random(seed), random(seed+1), random(seed+2));
    seed++;
}

@P = @ptemp;

 

Link to comment
Share on other sites

I'm not sure that this is the right method to achieve what you want. First, you have an infinite loop (and I believe the vex compiler detects it and erases it from the code, that might be why it does nothing). Second, you never check the nearpoints again, and moreover, nearpoints can return the current point too, so you should be careful with that. Third, your @ptemp will always be in the range (0.0f, 1.0f) because you set it to be a random number, and not your initial position plus some random value on each axis.

You might want to write something like this :

vector pTemp = v@P;

int npts[] = nearpoints(0, pTemp, f@pscale);
int lenNpts = len(npts);

int seed = 0;

while(lenNpts > 0):
	vector randToAdd = fit01(vector(rand(seed)), vector(set(-1.0f)), vector(set(1.0f)));
	pTemp = pTemp + randToAdd;
	
	npts = nearpoints(0, pTemp, f@pscale);
	lenNpts = len(npts);

	seed++;

v@P = pTemp;

Hope this can help.

 

Cheers,

Link to comment
Share on other sites

12 hours ago, DonRomano said:

 


vector pTemp = v@P;

int npts[] = nearpoints(0, pTemp, f@pscale);
int lenNpts = len(npts);

int seed = 0;

while(lenNpts > 0):
	vector randToAdd = fit01(vector(rand(seed)), vector(set(-1.0f)), vector(set(1.0f)));
	pTemp = pTemp + randToAdd;
	
	npts = nearpoints(0, pTemp, f@pscale);
	lenNpts = len(npts);

	seed++;

v@P = pTemp;

 

Hi thanks for your reply! Yeah my random vector was just for testing purposes. I am curious about your "randToAdd" variable though- I understand the intention but what is the vector() function? I can't find it in the documentation. And why run a set() on the y and z?

Overall though this really helps me understand the code better, thank you again. Curious as to how you would approach it? I'm trying to learn more Vex functions and also more of "how to think like a programmer" so any advice is very welcome

Link to comment
Share on other sites

The vector() is just a cast, i.e in this case helping the compiler get the right function overload : because rand has multiple overloads as it can return either a float, a vector2..., so it helps the compiler finding which overload I need, here the vector one, and same for the set() function (as it has overloads for vector2, arrays, matrices...).

I remap the random vector because the rand() function always return random numbers in the range 0.0,1.0 and you want to have a more random position by having either a positive or negative jitter to add, it's better because if you only add positive random numbers to your position, it will end up going in the same direction.

I've made a one liner for the randToAdd but it can be decomposed in that way to help understand what it does :

while(lenNpts > 0):
	vector random = vector(rand(seed)); 		// create a random vector in the range (0.0f, 1.0f); ex {0.34f, 0.85f, 0.14f}
	vector min = set(-1.0f); 					// set a minimum vector; similar to {-1.0f, -1.0f, -1.0f}
	vector max = set(1.0f);	 					// set a maximum vector; similar to {1.0f, 1.0f, 1.0f}
	vector randToAdd = fit01(random, min, max); // remap our random vector to the min and max range
	pTemp = pTemp + randToAdd;					// add our new remaped random vector to the position

The best I could recommand is learning algorithms and data structures, I have a few books that helped me on this side, but you might find this channel useful : https://www.youtube.com/c/JunichiroHorikawa

The language is just a tool (here vex, the same can be done with python, just slower), the most important is the algorithms you use and design, and the more you know the more tools you'll have to solve problems.

Edited by DonRomano
Link to comment
Share on other sites

Ahhhh vector1 got it got it, never would think to use that. I actually am considering doing some computer science classes in the fall to learn algorithmic/systemic principles. Could I ask the names of those books? Maybe they can save me some $$$ on school. I do watch Junichiro also but I feel like I need more info into what he talks about, also the videos are very long.

Thanks for your time and I really appreciate the breakdown, that does clarify it for me, and makes perfect sense.

Link to comment
Share on other sites

There you go for maths and algorithms : 

https://www.amazon.com/Introduction-Algorithms-Leiserson-Charles-Clifford/dp/B0839JW93F

https://www.amazon.com/Mathematics-Computer-Graphics-Undergraduate-Science/dp/1447173341

You can have a look at gpu gems from nvidia (edition 1 is there : https://www.nvidia.com/en-us/drivers/gpu-gems-home/), there are out of date but there are some very nice algorithms and techniques you can try to implement in Houdini to practice.

 

Cheers and good luck in your learning path !

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.

×
×
  • Create New...