Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 18 Oct 2007
Offline Last Active Aug 31 2015 10:55 PM

Topics I've Started

Find parabola velocity needed to reach a point with air resistance

22 July 2015 - 07:50 PM

Hi, I'm having a very difficult time figuring out how to find the velocity needed to reach a point for a projectile (shot from a cannon) to reach a point with air resistance.


time step is constant: 0.02

air resistance is constant: 1 / (1 + 0.02 * drag), where drag is a constant: 2

gravity is constant: -9.81

initial velocity is: 7, 20

mass is constant: 1


integration is like so:


velocity += gravity * 0.02
velocity *= airResistance

position += velocity * 0.02


I'm not much of a math guy, any help would be greatly appreciated :)

Direct Compute, extracting bounds of pixels

11 May 2015 - 07:06 PM

Hi, I'm trying to create a Compute Shader that looks at a texture and finds the bounds of all black pixels in that texture.


Like in this image:




I'm new to the whole concept of compute shaders, and struggling with this. Especially with threads and thread groups.


My initial attempt was to create a RWStructuredBuffer that contains 1 element, this element is a custom struct containing 4 floats: (minX, maxX, minY, maxY). The image is 512x512. I thought I could just get the thread position, color of the pixel at this position and compare with that in the buffer using min/max functions. This does not work. And I don't have any idea why. Here's my simple shader:

#pragma kernel CSMain

//custom struct to hold the bounds info

struct Bound {
	float minX;
	float minY;
	float maxX;
	float maxY;

Texture2D<float4> tex;
RWStructuredBuffer<Bound> bounds : register(u0);

//thread groups, not sure how many I need?
[numthreads(1, 1, 1)]
void CSMain (uint2 id : SV_DispatchThreadID) {	

	//find the color at this position, image is 512x512

	float4 color = tex[id];

	//test if the color is black, if so, process

	if (color.r == 0 && color.g == 0 && color.b == 0) {

		//get the Bound object, there should only be 1
		//compare the positions stored in this object with the current pixel position (id)

		Bound b = bounds[0];
		b.minX = min(b.minX, id.x);
		b.minY = min(b.minY, id.y);
		b.maxX = max(b.maxX, id.x);
		b.maxY = max(b.maxY, id.y);

		//assign the Bound object back to the buffer so we can compare it later on, and also grab this data outside of the shader.

		bounds[0] = b;

What happens is the result I get is very jittery, the bounds are jumping around and numbers are never the same, which is strange because the image is static.


From all the examples I've seen, the size of RWStructuredBuffer has some relationship with the thread count. However, I don't want this / know how I could use a buffer with that size. I need to be able to get data out of the shader telling my the min/max coordinates. If I have a buffer that's larger than 1 in size, how can I get these coordinates out?

Picking a random value from array with priority

09 March 2015 - 01:15 PM

Hi, I'd like to pick a value from an array which is sorted using a priority value, 0-100.


However, I'm looking for a way that returns a value in the array with a higher priority more frequently.

Say my array has 5 values:


[100, 50, 10, 3, 1]


I would like the value "100" to be returned more often than "50". The value of "1" should rarely be chosen.


Two ways I think I can solve it, but not exactly ideal or efficient:


1. Put more of the higher priority values in the array: [100, 100, 100, 100, 100, 50, 50, 50, 50, 10, 10, 10, 3, 3, 1]

2. Multiply a random number (t) by a curve: http://easings.net/#easeInQuint and use that as the index (x).


Just wondering if there's a better way?



Calculate number sequence

12 August 2014 - 05:44 PM

Hi, was wondering if there's a more mathematical approach to calculating a sequence of numbers where each number multiplies itself by 2. so starting with the number 5, and running 6 levels you would end up with


5, 10, 20, 40, 80, 160


I've got this and it works, but wondering if there's a more efficient way of doing it?

int n = 5;
int level = 6;

for (int i = 1; i < level; i++) {

	n *= 2;

return n;


Rotate character spine so weapon aims at target

24 April 2014 - 07:21 PM

Hi, I'm trying to rotate my characters spine so that his weapon aims at a specific point.

I'm close, but something is off. Here's an image of my problem:

Red line is weapons direction, blue line is from barrel to target



and here's my code so far:

Transform a = GameObject.Find("swat").transform;

Transform s = a.FindChild("Hips/Spine"); 

Transform w = a.FindChild("Hips/Spine/Spine1/Spine2/RightShoulder/RightArm/RightForeArm/RightHand/ak47");

Vector3 aimOrigin = w.TransformPoint(weapon.attackPoint);
Vector3 aimForward = target - aimOrigin;
Vector3 weaponForward = w.up;

//find the spine rotation required for weapon to point at the target

aimRotation = Quaternion.FromToRotation(weaponForward, aimForward) * s.rotation;

//apply the rotation back to spine

s.rotation = aimRotation;

any ideas?