Wierd Velocity problems
by Chris · in Torque Game Builder · 03/12/2005 (10:44 am) · 1 replies
I have begun work on my first t2d game but find that if I spawn the enemys on the left side and apply a velocity then check to make sure that the velocity is greater then 10 that they just stop. EDIT: Meaning that if their velocity isnt greater then 10, then apply that velocity to them. For somereason even after applying it, the object doesnt recieve the velocity and just stops.
Ill give you guys the code
That is my code for the enemy. I could not find a onDelete/onKill function for the enemy so I have made a makeshift replacement for it. For some reason also, the script creates more enemys then my set maximum also.
Any ideas?
Ill give you guys the code
function EnemyObject::spawnEnemy(%this) {
if(%this.currentEnemyCount < %this.maxEnemyCount) {
%this.currentEnemyCount++;
// This is gonna work by speed, pick a random speed from there pick a x position then a y position
if(getRandom() >= 0.5) {
%speed = -60 * getRandom() - 15; // xmax spawn position
%xpos = %this.enemySpawnPosXmax;
}
else {
%speed = 60 * getRandom() + 15; // xmin spawn position
%xpos = %this.enemySpawnPosXmin;
}
%ypos = (mabs(%this.enemySpawnPosYmax)+mabs(%this.enemySpawnPosYmin)) * getRandom();
%ypos = %ypos - mabs(%this.enemySpawnPosYmin);
%this.enemy[%this.currentEnemyCount] = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%this.enemy[%this.currentEnemyCount].setPosition(%xpos SPC %ypos);
%this.enemy[%this.currentEnemyCount].setImageMap(fish1);
%this.enemy[%this.currentEnemyCount].setGroup(2);
%this.enemy[%this.currentEnemyCount].setLayer(2);
%this.enemy[%this.currentEnemyCount].setLinearVelocityX(%speed);
%this.enemy[%this.currentEnemyCount].setSize("4 4");
%this.enemy[%this.currentEnemyCount].setWorldLimit(kill, "-60 -50 60 50");
%this.enemy[%this.currentEnemyCount].setCollisionActive(true, true);
%this.enemy[%this.currentEnemyCount].setCollisionMaterial(standardMaterial);
%this.enemy[%this.currentEnemyCount].setCollisionScale("0.9 0.5");
%this.enemy[%this.currentEnemyCount].setCollisionMasks(BIT(1), BIT(2));
%this.enemy[%this.currentEnemyCount].setCollisionCallback(true);
%this.enemy[%this.currentEnemyCount].setName("fish");
%this.enemy[%this.currentEnemyCount].setMinLinearVelocity(%speed);
}
else
echo("currently have max number of enemies spawned");
}
function EnemyObject::dropEnemy(%this, %drop) {
%this.currentEnemyCount--;
for(%i = %drop; %i < %this.maxEnemyCount; %i++)
%this.enemy[%i] = %this.enemy[%i+1];
}
function EnemyObject::onTick(%this) {
for(%i = 0; %i < %this.currentEnemyCount; %i++) {
if(%this.enemy[%i].getName() !$= "fish") { // no longer alive
%this.dropEnemy(%i);
%i--; // Need to check the same one again to make sure that it is alive
}
}
for(%i = 0; %i < %this.currentEnemyCount; %i++)
if(%this.enemy[%i].getMinLinearVelocity() != %this.enemy[%i].getLinearVelocityX()) {
%this.enemy[%i].setLinearVelocityX(%this.enemy[%i].getMinLinearVelocity());
echo("Enemy Velocity Error " @ %this.enemy[%i]);
}
%spawnvar = mabs(%this.enemySpawnTimeVar)* 2 * getRandom();
%spawnvar = %spawnvar - 2000;
%this.schedule(%spawnvar+%this.enemySpawnTimeBase, "spawnEnemy");
}That is my code for the enemy. I could not find a onDelete/onKill function for the enemy so I have made a makeshift replacement for it. For some reason also, the script creates more enemys then my set maximum also.
Any ideas?
About the author
Associate Melv May
The naming of "setMinLinearVelocity()" and "setMinAngularVelocity()" are a little misleading here. The min velocities control the minimum velocity at which point an object will reset that particular velocity. This stops your object from drifting very slowly.
This is vital as things like damping take away a percentage of the velocity which means they would never come to rest until the float-precision failed and stopped them, maybe not even then.
Currently, there isn't a T2D so tell an object to maintain at least a certain linear/angular velocity.
Sorry for the confusion this has caused.
You mention no "onkill", look for the "fxSceneGraph2D::onSafeDelete(%this)" in the ref doco.
- Melv.