Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: knockback to collision  (Read 2546 times)

0 Members and 1 Guest are viewing this topic.

AzkaIsHere

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
knockback to collision
« on: February 17, 2018, 10:04:12 pm »
hey guys I am making a game where two sprite have to collide and when they do they have to take a knockback to the opposite position of the collision point. can someone tell me an example on how to do that or maybe just explain or link some tutorial?

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: knockback to collision
« Reply #1 on: February 18, 2018, 08:19:39 am »
Sure, knockback 'can' be done quite simply in the traditional platformer/rpg sense. However objects such as cars can be more difficult due to wheel motion and the shape.

On a side note. I recommend reading up on vector maths/motion physics, as it will help with all games you may work on.
Read part 1-3: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

What kind of objects are needing to collide? Are you requiring knockback like in the platformer Mario, pong bat & ball, cars, perhaps something else?

The simplest form is probably by moving one object in the opposite direction of the other object. You're going to need a motion system for smooth knockback (acceleration, friction, optional gravity):
Code: [Select]
//Non-code example
directionRadians = atan2(y1 - y2, x1 - x2) // Angle between objects, negate the value for reverse direction
directionDegrees = (directionRadians * 180) / PI // If your game uses degrees instead of radians, convert

object1.setDirection(-directionDegrees) // Imaginary motion function for setting the current direction of the force
object1.setAcceleration(30) // Imaginary motion function to set the force's magnitude
object2.setDirection(directionDegrees)
object2.setAcceleration(30)

If you want to reverse from the collision point, simply work out the positions of each object relative to the collision point, and then use those coordinates as the position.
« Last Edit: February 18, 2018, 08:42:30 am by Turbine »

AzkaIsHere

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: knockback to collision
« Reply #2 on: February 20, 2018, 08:38:55 pm »
what I am doing is a beyblade game, when the beys hit each other, they get bounced back away but I don`t know how to set a direction that is opposite to the other beyblade

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: knockback to collision
« Reply #3 on: February 20, 2018, 10:58:03 pm »
atan2 will work out the direction in radians, and the conversion to degrees is also provided above. Just work it out twice, one object may be x2 - x1, y2 - y1 and the other will be x1 - x2, y1 - y2.