0
\$\begingroup\$

I'm having a problem bouncing a projectile off a sprite, we've been asked to move the projectile using the equations of motions which makes things a little more difficult but as far as I can see what I have should work.

What I'm trying to do is change the angle of the collided projectile depending on which direction it is coming from.

Here is a video that is hopefully not too laggy for you to see what is happening:

Link

When the projectile collides with the left or right hand side of the sprite everything works as expected, it just switches X direction.

When it hit's the top or bottom of the sprite however it doesn't change, it just sort of rolls along the top and the shoots off.

Here is the movement code:

float nX = get_x() + cos(nGetAngle() * 3.14 / 180) * getU() * getT();
float nY = get_y() - sin(nGetAngle() * 3.14 / 180) * getU() * getT() + 0.5 * 9.8 * getT() * getT();
set_world_position(nX, nY);

Where U is initial velocity, T is time and nGetAngle() is the angle in degrees (which is set to radians whenever the angle is set).

Here is my collision for the top of the player:

//if the projectile is colliding in any way with the player sprite

if (projectiles[currProj]->get_y() < player->get_y()) // top of player
{
    float vx = cos(projectiles[currProj]->nGetAngle());
    float vy = sin(projectiles[currProj]->nGetAngle());

    float newAngle = atan2(-vy, vx) * 180 / 3.14;

    projectiles[currProj]->nSetAngle(newAngle);
    projectiles[currProj]->set_world_position_y(player->get_y() - projectiles[currProj]->get_height() - 1);
}

and here is my collision for the left of the player:

else if (projectiles[currProj]->get_x() < player->get_x()) // left of player
{
    projectiles[currProj]->set_world_position_x(player->get_x() - projectiles[currProj]->get_width());

    float vx = cos(projectiles[currProj]->nGetAngle());
    float vy = sin(projectiles[currProj]->nGetAngle());

    float newAngle = atan2(vy, -vx) * 180 / 3.14;

    projectiles[currProj]->nSetAngle(newAngle);
}

The left side collision works, the top does not and I have no idea why.

If necessary I can post the entire project somewhere.

\$\endgroup\$
1
\$\begingroup\$

Let me put this simply. (Or I will try to)

First of all, you should read up on Vectors, find out what these are:

Vector Magnitude; Vector X,Y coordinates; Normalised Vector;

After this, you should read up a bit on physics, especially Newtons three laws; kinetics; momentum; and collisions.

After a week or so you should start to understand these things.

You will need to assign a velocity vector to the "ball"(upon shooting it), on every frame you will add the Gravity vector to this "ball". Then you will set up a collision detection function, that will return the collision normal, and the collision point.

Using this data, you will remove both objects from penetration. Then you will need to calculate the relative velocity of both objects = vA - vB = vAB. you will then use the dotProduct to project the Relative Velocity Vector onto the collision Normal. This will Give you the Impulse scalar. You will Scale this by -(elasticity (value between 0-1) + 1). You will then Scale the collision normal vector by the scaled Impulse * 2 (because you just want the ball to move).

After doing all this. You will have learned a lot about physics, expanded your knowledge of many usefull things, (that will be very usefull to you in game programming and many other programming fields).

best of luck!!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.