From: Eleanor McHugh Date: 2009-11-15T08:22:48+09:00 Subject: Re: Trajectories On 13 Nov 2009, at 23:33, Thijs Leeflang wrote: > hello, > > im having a problem with trajectories, > im trying to make an arrow like movement > i did find this but it's way over my head > > http://www.gamedev.net/reference/programming/features/physicsch6/ch06.pdf This is mostly for people who want to model the real-world movement of projectiles, i.e. parabolic ballistic curves resulting from the action of gravity. The math isn't hugely complicated, but it does assume a basic familiarity with trigonometry. > i have these values, > @x and @y is the x and y the image is drawn > > @targetx = targetx > @targety = targety > @fromx = fromx > @fromy = fromx > @x = fromx > @y = fromy > > can anybody help me with this? I'm going to assume you want an OO solution that wraps stuff up in a nice reusable fashion, so here's an example for what such a solution might look like (it's not tested so treat with caution). Point = Struct.new(:x, :y) ComponentVector = Struct.new(:x, :y) # Assume bearing stored in radians (360 degrees = 2 * Pi radians) BearingVector = Struct.new(:bearing, :magnitude) class Entity attr_accessor :location, :forces def initialize location = nil @location = location || Point.new(0, 0) @forces = {} end def move timeslices = 1 @forces.each do |f| case when f.bearing @location.x += Math.cos(f.bearing) * f.magnitude * timeslices @location.y += Math.sin(f.bearing) * f.magnitude * timeslices when f.x && f.y @location.x += f.x * timeslices @location.y += f.y * timeslices end end end end The solution is generalised to allow more than one force to be applied to an individual entity, and to allow the force vector to be defined as either (x, y) components or as an angle and a magnitude. football = Entity.new(Point.new(100, 100) football.forces[:gravity] = ComponentVector.new(0, -9.81) football.forces[:kick] = BearingVector(0.5, 27) football.forces[:wind] = BearingVector(5, 3) football.move 3 Here I've set up a simple football scenario combining gravity, a kick and a prevailing wind. The scenario for an arrow would look very similar. Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason