I'm working on some fairly basic object physics on Maloria, where hitting an object will make it move. I'm at the part where I need it to kind of bounce off a wall when it collides with it a bit, but I'm having trouble figuring out how I would do that. Here's what I have:
PHP Code:
function onCreated() {
if (!("function_domove" in this.joinedclasses))
join("function_domove");
this.moving = false;
this.canhurt = false;
this.distgone = 0;
this.lastpos = {this.x, this.y};
}
function onActionHit(damage, accuracy, effects, acc, px, py) {
this.dist = 0;
this.distgone = 0;
this.speed = 0;
this.canhurt = false;
this.angle = getangle(px - this.x, py - this.y);
if (this.angle > degtorad(180))
this.angle -= degtorad(180);
else this.angle += degtorad(180);
this += degtorad(int(random(-10, 11)));
temp.friction = this.weight / damage;
if (temp.friction >= 1) this.dist = 0;
else this.dist = .75 / temp.friction;
this.moving = true;
this.speed = damage/5;
if (this.speed > 5) this.canhurt = true;
onTimeOut();
}
function onTimeOut() {
if (this.moving) {
if (this.distgone < this.dist) {
temp.dir = getDir(this.lastpos[0]-this.x,this.lastpos[1]-this.y);
this.dir = temp.dir;
this.lastpos = {this.x, this.y};
temp.move = doMove(this.angle, this.dist/this.speed);
if (temp.move) this.distgone += this.dist/this.speed;
else {
//collision stuff here
}
if (this.canhurt)
triggeraction(this.x+1+vecx(temp.dir)*(this.dist/this.speed), this.y+1+vecy(temp.dir)*(temp.dist/this.speed),"Hit",this.dist/2,40);
setTimer(0.05);
}
else this.moving = false;
}
}
The class function_domove would be Zero's movement system. ("doMove(angle, speed)")...I need it to move "this.dist/this.speed" distance...but the problem if figuring out how to get the angle at which it would bounce off...