Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Wait for DirectMedia Xtra Sprite v. 2.0
Generic behavior to get Key State.
Drop Shadow Follow Mouse or Sprite-Alphamania
Convert Decimal to Comma (EU)
Menu bar emulator
Graphic Puzzle Dragger
Happy Hour Fonts
SplashScreenX
ProMix
27
 

 

 

Behavior Gravitational Force

Added on 6/8/1999

 

Compatibilities:
behavior D6_5 Mac PC

This item has not yet been rated

Author: RamanPfaff

Gravitational Forces Behavior

property x,y,vx,vy,ax,ay,mx,my,bx,by,dt,mass,time,coef_rest,bounding_box,bounding_Rect

on getPropertyDescriptionList me
  set description = [:]
  addProp description, #x, [#default:0.0,#format:#float,#comment:"Initial X position:"]
  addProp description, #y, [#default:0.0,#format:#float,#comment:"Initial y position:"]
  addProp description, #vx, [#default:50.0,#format:#float,#comment:"Initial X vel:"]
  addProp description, #vy, [#default:50.0,#format:#float,#comment:"Initial y vel:"]
  addProp description, #ax, [#default:0.0,#format:#float,#comment:"Initial X acc:"]
  addProp description, #ay, [#default:-9.8,#format:#float,#comment:"Initial y acc:"]
  addProp description, #mx, [#default:1.0,#format:#float,#comment:"Initial X slope:"]
  addProp description, #my, [#default:1.0,#format:#float,#comment:"Initial y slope:"]
  addProp description, #bx, [#default:0.0,#format:#float,#comment:"Initial X int:"]
  addProp description, #by, [#default:0.0,#format:#float,#comment:"Initial y int:"]
  addProp description, #dt, [#default:0.2,#format:#float,#comment:"Initial delta T:"]
  addProp description, #bounding_Rect, [#default:1,#format:#integer,#comment:"Bounding Rect Sprite:"]
  addProp description, #coef_rest, [#default:1.0,#format:#float,#comment:"Coef Restitution (0-1):"]
  return description
end getPropertyDescriptionList

on getBehaviorDescription
  -- this script doesn't bother to check for errors of any sort when
  -- colliding with a "wall." Also doesn't take into account the size of the
  -- bitmap so it will bounce from it's registration point.
  -- The formula's use a "small delta T" approximation, and y points upward  (not down!)
  return "This enables object to feel a force. Default setting are for basic gravity. Smaller dt's are more accurate, but larger ones increase rate of motion. To get started just put a bitmap ball (sprite 2) with this behavior, and have a rect as sprite 1. Be sure the ball is inside the rect at the start. Loop in the frame script and watch the ball bounce."&RETURN&RETURN&"You can see lots of examples which use variations of this behavior at http://ExploreScience.com/"&RETURN&RETURN&"Raman (pfaff@ExploreScience.com)"
end getBehaviorDescription

on beginSprite me
  -- can set lots of initial stuff here (user adjustable fields)
  set bounding_box = the rect of sprite bounding_Rect
  set time = 0.0  
  set x = the locH of sprite the spriteNum of me
  set y = (the bottom of bounding_box - the top of bounding_box) -the locV of sprite the spriteNum of me
end

on exitframe me
  findPos
  findVel
  findAcc
  moveObject me
  -- sometimes useful to keep track of time
  set time = time + dt
end

on findPos me
  set x to x+vx*dt+ax*dt*dt
  set y to y+vy*dt+ay*dt*dt
end

on findVel me
  set vx to vx+ax*dt
  set vy to vy+ay*dt
end

on findAcc me
  --  this is where you can put the controlled force formula, where you may need to use masses.
  -- can have velocity dependant forces, etc.
  -- could just call a more complex movie script
  set ax = ax
  set ay = ay
  
  --  an mediocre example of air drag would be
  --  set ax = -.05*vx
  --  set ay = min(ay - .05*vy,0.0)
end

on moveObject me
  -- has a slope and an intercept to have things scaled when necessary
  set xs to integer(mx*x+bx)
  set ys to 300-integer(my*y+bx)
  set ys to (the bottom of bounding_box - the top of bounding_box) -integer(my*y+by)
  if xs >= the right of bounding_box or xs <= the left of bounding_box then
    set vx to -vx*coef_rest
  else if ys >= the bottom of bounding_box or ys <= the top of bounding_box then
    set vy to -vy*coef_rest
  end if
  set the loc of sprite the spriteNum of me to point(xs,ys)
end



-- can put various handlers here to tell the sprite to change itself
on setVx me, value
  set vx = value
end

on setVy me, value
  set vy = value
end

on tellVs me
  set temp = []
  set temp = [vx,vy]
  return temp
end  

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail