• Create Account

We're offering banner ads on our site from just \$5!

# mr_malee

Member Since 18 Oct 2007
Offline Last Active Aug 22 2014 02:08 PM

### 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;
```

thanks

### 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:

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

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

//weapon
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?

### Moving to a point with angular constraints

23 April 2014 - 01:53 PM

Hello, I'm trying to figure out how to move something to a point given some angular constraints.

Almost like a missile attempting to hit a target. It can't turn on a dime, so must rotate towards its target. However, because it's moving forward and rotating at the same time, if the distance between the target is less than the missiles turning speed, the missile may never reach its goal and simply rotate forever around the target point.

Now imagine instead of a missile you're a car. The same rules apply, you have a limited maximum turning circle which will prevent you from turning on a dime.

If the car was to try and reach a point beside itself, it could potentially:

• reverse, then move forward to the target.
• rotate on a larger arc around the point increasing the distance and allowing the car to align itself to the target point and move forward.
• say to itself, I'm close enough. No one will notice.

In my situation, the second one would be ideal. So, does anyone know how to calculate:

• will an object miss its target because it can't turn sharply enough?
• if so, calculate a larger arc/path giving the object enough distance to turn correctly.

or know of any helpful articles?

Thanks

### Calculate best car gear

23 September 2013 - 08:34 PM

Hi. I'm having some trouble trying to determine the best gear a car should be in to maximize acceleration.

I'm not looking for the perfect point at which to change gears to maximize acceleration.

I can get the current rpm of the car, the current speed, the current torque, optimal RPM's for gear ratios, engine torque for rpm, rpm from speed, and gear ratios.

I'm just not sure how to figure out what gear I should be in to get the best acceleration possible.

for example:

say I'm driving my car, I manage to shift all the way to 5th gear, going very fast. I suddenly crash, my speed reduces (but not to 0), I'm still in 5th gear. My acceleration now is very small, I should change gears, but to which gear in order to accelerate the fastest?

another example:

say I'm in 5th gear travelling very fast, I accidentally shift into 2nd, the rpm's rise quickly to the redline, my speed drops. What gear should I change into so that my engine doesn't explode and my acceleration will continue to increase?

### Easing to target with delta time

09 May 2012 - 04:53 PM

Ok, so I'm having some problem figuring out how to ease (slide to a point) an object with a delta time.

in my simulation "dt" represents delta time as a difference in frame rate. So with a target frame rate of 60 and a simulation rate of 60, the dt will be 1. Simulation running at 30 will return a dt of 2.

the integration for an object is:

```_invMass = 1.0; //assume mass is always 1 for now

_acceleration.x = _force.x * _invMass;
_acceleration.y = _force.y * _invMass;

_velocity.x += _acceleration.x * dt;
_velocity.y += _acceleration.y * dt;

limitVelocity(maxSpeed);

x += _velocity.x * dt;
y += _velocity.y * dt;

//clear force

_force.x = _force.y = 0;
```

before integrating the object, it's easing function is called:

```
//find distance from target (tx, ty) to position (x, y)

var dx:Number = tx - x;
var dy:Number = ty - y;
var len:Number = Math.sqrt(dx * dx + dy * dy);

if (len > 0) {

// make sure the object eases to the position
// I think the problem is here, the force needs to make sure the object won't overshoot the target, but I'm not sure how to use delta time in this equation

var speed:Number = Math.min(len, maxSpeed);
var force:Number = speed / len;

//find desired velocity, could also multiply by another scalar for smoother easing

var dvx:Number = dx * force;
var dvy:Number = dy * force;

//add the force as the difference from desired to current velocity

_force.x += dvx - _velocity.x;
_force.y += dvy - _velocity.y;
}
```

as you can see I think the issue and solution lies in calculating the force, but I'm not sure how to plug "dt" in there.
The issue that happens is that the object will overshoot the target and continually jitter back and forth never reaching the position.

any ideas?

Thanks.

PARTNERS